Os Lab
Os Lab
In this scenario, both the parent and child processes will execute the same piece of code.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
if (pid < 0) {
// Fork failed
return 1;
} else if (pid == 0) {
// Child process
} else {
// Parent process
return 0;
OUTPUT:
fork() creates a new process by duplicating the calling process. The new process is called the
child process.
After fork(), both the parent and child processes continue executing from the next instruction.
The pid value is 0 in the child process and the child's PID in the parent process.
The printf statement after the fork() call is executed by both the parent and child
processes.
In this scenario, the parent and child processes will execute different pieces of code after the
fork() call.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
int main() {
if (pid < 0) {
// Fork failed
return 1;
} else if (pid == 0) {
// Child process: Executes a different code
// If execlp fails:
perror("execlp failed");
exit(1);
} else {
wait(NULL);
return 0;
OUTPUT:
The parent and child processes follow different execution paths after the fork() call.
In the child process (pid == 0), execlp() is used to replace the child process's code
with the code of another program (in this case, the ls command).
If execlp() succeeds, the child process will execute the ls command; otherwise, it will
print an error message.
The parent process executes its own code independently of the child process.
Notes:
execlp() is one of the exec family of functions that replaces the current process image
with a new one (i.e., runs a different program in the same process).
The execlp() call does not return unless there is an error, in which case it returns -1 and
sets errno.
The parent process can use wait() or waitpid() to wait for the child process to finish
execution before continuing its own execution.
#include <stdio.h>
#include <stdlib.h>
int main() {
char kernel_version[128];
char cpu_info[128];
if (fp == NULL) {
perror("Failed to open /proc/version");
return 1;
fclose(fp);
fp = fopen("/proc/cpuinfo", "r");
if (fp == NULL) {
return 1;
printf("\nCPU Information:\n");
// Read through the /proc/cpuinfo file to find CPU type and model
printf("%s", cpu_info);
fclose(fp);
return 0;
Explanation:
1. Kernel Version:
o The program opens /proc/version to read the kernel version.
o It uses fgets to read the kernel version string and prints it to the console.
2. CPU Information:
o The program opens /proc/cpuinfo to read CPU information.
o It then reads through the file line by line, looking for lines that start with "model
name", "cpu cores", "cpu MHz", and "vendor_id", which are key pieces of
CPU information.
o The relevant information is printed out, such as the model name, CPU cores, CPU
speed, and vendor ID.
Output Example:
Note:
The /proc/cpuinfo file contains detailed information about the CPU, including the
number of processors, their speed, the vendor ID, and the model name. The program can
be extended to extract and display more information as needed.
The program reads the data directly from the /proc filesystem, which is a virtual
filesystem in Linux that provides an interface to kernel data structures.