Write a program in C that creates (spawns or forks) five children. Each time the
ID: 3581900 • Letter: W
Question
Write a program in C that creates (spawns or forks) five children. Each time the parent process creates a child, it will print out a line that:
Identifies the process that is printing the line as the parent process
Provides the process identifier (PID) of the printing process (the parent)
States which of the five children it just forked
Provides the process identifier (PID) of the new child process that it just created
The parent does not need to wait for child completion before it (the parent) continues on.
The child process has to print out a line that:
Identifies the process that is printing the line as a child process.
Indicates which child it is.
Provides its (the child process's) process identifier (PID).
Provides its parent's process identifier (PID).
(20 marks)
Guidelines:
The fork system call returns the process identifier (PID) of the new child to the parent (returns it as an integer value, in fact), but returns 0 to the child itself
The process determines its own process identifier (PID) by using system call named, getpid.
How does the child process know which child of its parent it is? Hint, since it can't be a system service, the parent itself must communicate that information to the child somehow. How? This is going to be easier than you might think at first glance; but you'll probably have to think about it for a bit.
Sample output :
Parent, my pid is 5251: Spawned child #1 whose pid is 5252
Parent, my pid is 5251: Spawned child #2 whose pid is 5253
I am child #1, my pid is 5252; my parent's pid is 5251
I am child #2, my pid is 5253; my parent's pid is 5251
Parent, my pid is 5251: Spawned child #3 whose pid is 5254
Parent, my pid is 5251: Spawned child #4 whose pid is 5255
Parent, my pid is 5251: Spawned child #5 whose pid is 5256
I am child #3, my pid is 5254; my parent's pid is 5251
I am child #4, my pid is 5255; my parent's pid is 5251
I am child #5, my pid is 5256; my parent's pid is 5251
In the sample output, above, some of those lines could appear in any order relative to any of the other lines.
Explanation / Answer
#include <stdio.h>
void create_child(int i,int ppid);
int main()
{
int pid ;
//set number of child to spawn
int n = 5,i;
int ppid = getpid();
for(i = 1; i <= n ; i++)
{
create_child(i,ppid);
}
//parnt process waits for all children
if( ppid == getpid())
{
//wait for all child process
wait(NULL);
}
return 0;
}
void create_child(int i,int ppid)
{
int pid;
pid = fork();
if( getpid()== ppid)
{
printf("Parent, my pid is %d: Spawned child #%d whose pid is %d ",ppid,i,pid);
}
//to check we create child of same parent
switch(i)
{
case 1:
if( pid == 0 && ppid == getppid())
{
printf("I am child #1, my pid is %d; my parent's pid is %d ",getpid(),ppid);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 2:
if( pid == 0 && ppid == getppid())
{
printf("I am child #2, my pid is %d; my parent's pid is %d ",getpid(),ppid);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 3:
if( pid == 0 && ppid == getppid())
{
printf("I am child #3, my pid is %d; my parent's pid is %d ",getpid(),ppid);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 4:
if( pid == 0 && ppid == getppid())
{
printf("I am child #4, my pid is %d; my parent's pid is %d ",getpid(),ppid);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 5:
if( pid == 0 && ppid == getppid())
{
printf("I am child #5, my pid is %d; my parent's pid is %d ",getpid(),ppid);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
default:
break;
}
}
----------------------------------------------------------------------------------------------------------------
output:
Parent, my pid is 200: Spawned child #1 whose pid is 201
I am child #1, my pid is 201; my parent's pid is 200
Parent, my pid is 200: Spawned child #2 whose pid is 217
I am child #2, my pid is 217; my parent's pid is 200
Parent, my pid is 200: Spawned child #3 whose pid is 225
I am child #3, my pid is 225; my parent's pid is 200
Parent, my pid is 200: Spawned child #4 whose pid is 229
I am child #4, my pid is 229; my parent's pid is 200
Parent, my pid is 200: Spawned child #5 whose pid is 231
I am child #5, my pid is 231; my parent's pid is 200
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.