Question: fork not printing pid, current _ pid and parent _ pid please help #include #include #include #include int main ( ) { pid _ t

fork not printing pid, current_pid and parent_pid please help #include
#include
#include
#include
int main()
{
pid_t pid;
char *message;
int n;
printf("fork program starting
");
pid = fork();
switch (pid)
{
case -1:
perror("fork failed");
exit(1);
case 0:
message = "This is the child";
n =5;
break;
default:
message = "This is the parent";
n =3;
waitpid(pid, NULL, 0);
break;
}
for (; n >0; n--)
{
printf("%s; pid=%d; current_pid=%d; parent_pid=%d;
",
message, getpid(),(int)getpid(),(int)getppid());
sleep(1);
}
exit(0);
}
ouput should look like this fork program starting
This is the parent; pid=9774; current_pid=9773; parent_pid=365;
This is the child; pid=0; current_pid=9774; parent_pid=9773;
This is the parent; pid=9774; current_pid=9773; parent_pid=365;
This is the child; pid=0; current_pid=9774; parent_pid=9773;
This is the parent; pid=9774; current_pid=9773; parent_pid=365;
This is the child; pid=0; current_pid=9774; parent_pid=9773;
This is the child; pid=0; current_pid=9774; parent_pid=9773;
$ This is the child; pid=0; current_pid=9774; parent_pid=1;
Once your output looks similar to what is shown above change the code to include two more children. This may require additional conditionals. Now, one parent and three children should be executing.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!