Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Task 1 G20 points) A child process can call fork0to create a child process of it

ID: 3571212 • Letter: T

Question

Task 1 G20 points) A child process can call fork0to create a child process of its own (a grand child process). For this task, you will need to modify fork.c to create a grand child process. In the grand child, prompt the user to enter an integer number in the range of o-9. Use exito and waitO to relay that number back to the original parent. Name your source file grandchild.c. Your program should work like this. hboll 7 uxb4: hbl170uxb4 gcc -Wall grandchild.c o grandchild hb117 uxb4 ./grandchild PID 13202 Enter a number between 0 and 9 20 PID 13202 Enter a number between 0 and 9 -1 PID 13202 Enter a number between 0 and 9 4 PID 13202 The number is 4 PID 13201 The number is 4 PID 13200 The number is 4

Explanation / Answer

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

int main(int argc ,char **argv)
{
int status;
pid_t mypid;
pid_t myparentId = getpid();
int n;
  
//take input n which is to be between 0-9
do
{
printf("PID %d:Enter a number between 0-9 ",myparentId);
scanf("%d",&n);
}while(n>9 || n< 0);
  
pid_t result = fork();
if( result > 0)
{
mypid = getpid();
printf("Pid %d:The number is %d ",mypid,n);
//printf("PARENT: I am %d ,the parent of (child_id = %d) ",mypid,result);
wait(NULL);

}
else if(result == 0)
{
int ret;
mypid =getpid();
pid_t myparent_id = getppid();
  
//create child which will be grandchild
ret = fork();

//child procecss
if( ret > 0 )
{
//wait for child
wait(&status);
printf("Pid %d:The number is %d ",getpid(),n);
}

//grand child
if( ret == 0)
{
  
pid_t pid = getpid();
//grand child
printf("Pid %d:The number is %d ",pid,n);
  
}
else
{
wait(&status);
}
exit(0);
  
}
else
{
fprintf(stderr,"fork() failed:%s ",strerror(errno));
return 1;
}
  
return 0;
}

-----------------------------------------------------------------

output

PID 34:Enter a number between 0-9 20                                                                                                                                            

PID 34:Enter a number between 0-9 -1                                                                                                                                            

PID 34:Enter a number between 0-9 4                                                                                                                                             

Pid 34:The number is 4                                                                                                                                                          

Pid 36:The number is 4                                                                                                                                                          

Pid 35:The number is 4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote