Program to create four processes (1 parent and 3 children) where they terminates in a sequence as follows :
(a) Parent process terminates at last
(b) First child terminates before parent and after second child.
(c) Second child terminates after last and before first child.
(d) Third child terminates first.
Prerequisite : fork(),
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int pid, pid1, pid2;
pid = fork();
if (pid == 0) {
sleep(3);
printf ( "child[1] --> pid = %d and ppid = %d\n" ,
getpid(), getppid());
}
else {
pid1 = fork();
if (pid1 == 0) {
sleep(2);
printf ( "child[2] --> pid = %d and ppid = %d\n" ,
getpid(), getppid());
}
else {
pid2 = fork();
if (pid2 == 0) {
printf ( "child[3] --> pid = %d and ppid = %d\n" ,
getpid(), getppid());
}
else {
sleep(3);
printf ( "parent --> pid = %d\n" , getpid());
}
}
}
return 0;
}
|
Output:
child[3]-->pid=50 and ppid=47
child[2]-->pid=49 and ppid=47
child[1]-->pid=48 and ppid=47
parent-->pid=47
This code runs on Linux platform.