0% found this document useful (0 votes)
18 views

Os Lab

Os lab notes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Os Lab

Os lab notes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

OS PRATICALS

1. Write a program in C (using fork() and/or exec()


commands) where parent and child execute:
a) same program, same code.
b) same program, different code.
(a): Same Program, Same Code

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() {

pid_t pid = fork();

if (pid < 0) {

// Fork failed

fprintf(stderr, "Fork failed\n");

return 1;

} else if (pid == 0) {

// Child process

printf("Child process: PID = %d\n", getpid());

} else {

// Parent process

printf("Parent process: PID = %d, Child PID = %d\n", getpid(), pid);

// Common code executed by both parent and child


printf("This code is executed by both parent and child.\n");

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.

Part (b): Same Program, Different Code

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() {

pid_t pid = fork();

if (pid < 0) {

// Fork failed

fprintf(stderr, "Fork failed\n");

return 1;

} else if (pid == 0) {
// Child process: Executes a different code

printf("Child process: PID = %d\n", getpid());

printf("Child process is executing different code.\n");

// Example: Child can execute another program using exec()

execlp("/bin/ls", "ls", NULL); // Executes the 'ls' command

// If execlp fails:

perror("execlp failed");

exit(1);

} else {

// Parent process: Executes its own code

printf("Parent process: PID = %d, Child PID = %d\n", getpid(), pid);

printf("Parent process is executing its own code.\n");

// Parent can wait for child to finish if needed

wait(NULL);

printf("Parent process: Child finished execution.\n");

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.

2. Write a program IN C to report behavior of Linux


kernel including kernel version, CPU type
and model. (CPU information)
You can write a C program to gather and report Linux kernel information, CPU type, and model
by reading data from system files located in /proc. This directory contains information about the
system and processes, which is exposed by the Linux kernel. Here’s how you can do it:

#include <stdio.h>

#include <stdlib.h>

int main() {

// Variables to store file paths

char kernel_version[128];

char cpu_info[128];

// Read and display the kernel version

FILE *fp = fopen("/proc/version", "r");

if (fp == NULL) {
perror("Failed to open /proc/version");

return 1;

if (fgets(kernel_version, sizeof(kernel_version), fp) != NULL) {

printf("Kernel Version: %s", kernel_version);

fclose(fp);

// Read and display the CPU information

fp = fopen("/proc/cpuinfo", "r");

if (fp == NULL) {

perror("Failed to open /proc/cpuinfo");

return 1;

printf("\nCPU Information:\n");

// Read through the /proc/cpuinfo file to find CPU type and model

while (fgets(cpu_info, sizeof(cpu_info), fp) != NULL) {

if (strncmp(cpu_info, "model name", 10) == 0 ||

strncmp(cpu_info, "cpu cores", 9) == 0 ||

strncmp(cpu_info, "cpu MHz", 7) == 0 ||


strncmp(cpu_info, "vendor_id", 9) == 0) {

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.

How to Compile and Run:

1. Save the program to a file, for example, cpu_info.c.


2. Compile it using GCC:

gcc -o cpu_info cpu_info.c

3. Run the compiled program


./cpu_info

Output Example:

The program will output something similar to:

Kernel Version: Linux version 5.15.0-41-generic (buildd@lcy02-amd64-070) (gcc (Ubuntu 9.3.0-


17ubuntu1~20.04) 9.3.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #44-Ubuntu SMP Tue Jul 12
10:24:31 UTC 2022
CPU Information:
vendor_id : GenuineIntel
model name : Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
cpu MHz : 2600.000
cpu cores : 6

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.

You might also like