Operating System - Lab Manual # 9
Operating System - Lab Manual # 9
LAB MANUAL # 9
(SPRING 2023)
Objective(s) :
To write a program to create a process in LINUX. To create child with sleep
command. To understand getpid( ) and getppid( ).
Lab Tasks :
Task 1 : Write the output of program for process creation using fork command.
Task 2: Write the output of a program for execution of ls command using exec.
Task 3 : Write the output of a program illustrating the sleep command during process creation.
Task 4 : Write the output of the program for getting the pid and ppid while using the sleep
command.
Total 40 Signature
Note : Attempt all tasks and get them checked by your Instructor
Objective(s):
To write a program to create a process in LINUX.
To create child with sleep command.
To understand getpid( ) and getppid( ).
Tool(s) used:
Task 1 Write the output of a program for process creation using fork command.
Algorithm
STEP 4: Check pid is less than 0 then print error else if pid is equal to 0 then execute
command else parent process wait for child process.
Program
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
int id;
id=fork();
if(id<0){
printf ("Cannot Create the file");
exit(-1);
}
if(id==0){
printf ("Child Process");
exit(0);
}
else{
printf ("Parent Process");
exit(1);
}
return 0;
}
Program Execution
$gcc pc.c –o pc
$./pc
OUTPUT
Task 2 Write the output of a program for execution of ls command using exec.
Algorithm
STEP 2: Execute the command in the shell program using exec ls.
Program
OUTPUT
Task 3 Write the output of a program illustrating the sleep command during process creation.
Algorithm
STEP 3: If the value of variable is < zero print not create and > 0 process create and else
print child create.
STEP 4: Create child with sleep of 2.
Program
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
int main( ){
pid_t id;
id=fork( );
if (id==-1){
printf ("Cannot Create the file");
exit(1);
}
if (id==0){
sleep(20);
printf ("This is child Process");
}
else{
printf ("Parent Process");
exit(1);
}
return 0;
}
OUTPUT
Task 4 Write the output of the program for getting the pid and ppid while using the sleep
command.
Algorithm
STEP 1: Start the execution and create a process using fork( ) command.
STEP 6: After making the sleep for the parent process for 10 seconds print it pid.
Program
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
pid_t pid;
pid=fork();
if(pid==0){
printf("\nChild Process");
printf("\nChild Process ID is %d", getpid());
printf("\nIts Parent Process ID is %d", getppid());
sleep(5);
printf("\nChild Process after sleep=5");
printf("\nChild Process ID is %d", getpid());
printf("\nParent Process ID is %d", getppid());
}
else{
printf("\n\nParent Process\n");
sleep(5);
OUTPUT