1,385 views
1 votes
1 votes

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

2 Answers

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

Hence all the options are equally likely.
1 votes
1 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.

Related questions

12 votes
12 votes
4 answers
1
admin asked Feb 15, 2023
11,457 views
Which one or more of the following options guarantee that a computer system will transition from user mode to kernel mode?Function Callmalloc CallPage FaultSystem Call