3,091 views
1 1 vote

Consider the following C program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (void)
{
    pid_t pid;
    int x = 2;
    pid = fork ( );
    if (pid = = 0);
         x = 8;
    fork( );
    printf("%d", x);
    return 0;
}

Fork ( ) is a system call creates new child process on successful execution. Assuming all invocations of fork are successful, which of the following is a correct output when the program is executed on the unix OS?[MSQ]

(a) 8 2 2 8       (b) 8 8 2 2       (c) 2 8 8 2      (d) 2 2 8 8

3 Answers

3 3 votes

if (pid = = 0); statement is of no use in parent and child processes because of semicolon.

None of the options are correct. correct ans is 8 8 8 8. because before print statement value of x is changed from 2 to 8 in parent and child both processes.

2 2 votes
The order of execution between parent and child processes cannot be said.

Hence all the options are equally likely.
0 0 votes

if (pid = = 0);
There is a semicolon so all the forks will have x = 8;
Answer should be 8 8 8 8

Position:
Show:

No related questions found