Write a CC program that forks exactly five child processes T

Write a C/C++ program that forks exactly five child processes. These child processes must be siblings - their parents mst be the same person.

Solution

Dear Student,

following is a c program that forks exactly five child process. The child process are sibling of each other and their parent is same.

this program is tested on ubuntu 14.04 sytem.

Please compile program as \"gcc filename.c\" and then a \"a.out\" will be created in the working directory.

then run program as \"./a.out\" in command line.

here is the complete program.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>

int main()
{
int n;
int pid;
pid = fork();
if(pid == 0)
{
printf(\"Child process is started\ \");
n =5;
for(n=1;n<=5;n++)
printf(\"This is the child process %d pid of the child is %d and parent id is %d\ \",n,getpid(),getppid());
}
else if(pid == -1)
{
printf(\"call to fork failed\");
}
else
{
}
return 0;
}

The Output which you will get is as below

Child process is started
This is the child process 1 pid of the child is 7117 and parent id is 7116
This is the child process 2 pid of the child is 7117 and parent id is 1707
This is the child process 3 pid of the child is 7117 and parent id is 1707
This is the child process 4 pid of the child is 7117 and parent id is 1707
This is the child process 5 pid of the child is 7117 and parent id is 1707


Note1:The pid and ppid may vary in your system.

Note2: fork() is a sytem call which return a new child process and parent run as a seprate process with a separte pid.If the pid is 0 the child process will be created otherwise call to fork will be failed.

Write a C/C++ program that forks exactly five child processes. These child processes must be siblings - their parents mst be the same person.SolutionDear Studen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site