141k views
3 votes
Assuming that the actual process ids of the parent and child process are 2600 and 2650 respectively, what will be printed out at lines A, B, C, D, E, and F? Be sure to explain your answers. int main() { pid_t x, y; int value = 90; value += 30; /* fork a child process */ x = fork(); if (x < 0) { /* error occurred */ fprintf(stderr,"Fork failed"); return(1); } else if (x == 0) { /* child process */ y = getpid(); printf("child: x = %d",x); /* A */ printf("child: y = %d",y); /* B */ value += 20; printf("child: value = %d\\", value); /* C */ exit(0); } else { /* parent process */ y= getpid(); printf("parent: x = %d",x); /* D */ printf("parent: y = %d",y); /* E */ wait(NULL); printf("parent: value = %d\\", value); /* F */ } }

1 Answer

3 votes

Answer:

Output explanation to the given code can be defined as follows:

Step-by-step explanation:

In A the output is 0 , It will return fork value of the child process that is 0. so, 0 is printed during child process.

In B the output is 2650 , in which the getpid() method returns the child process id value that is 2650.

In C the output is 140, As it is declared in key, all process have their own "value" copies. 20 are inserted during childhood, so 140 are written.

In D the output is 2650, its fork() method returns the child ID to the parent process. so the value 2650 is printed.

In E the output is 2600, Its getpid() method will returns parent process id that is equal to 2600.

In F the output is 120 Since the value is declared in primary, all process so their own "value" copies. 120 will be printed during process.

User Haythem Farhat
by
6.8k points