edited by
8,805 views
15 votes
15 votes

Wha is the output of the following program?

main()
{
    int a = 10;
    if(fork()) == 0))
        a++;
    printf("%d\n",a);  
}
  1. 10 and 11
  2. 10
  3. 11
  4. 11 and 11
edited by

5 Answers

1 votes
1 votes

when parent process come across fork(),it creates a child processes.

in the multiprogramming environment,there is no order of execution defined.

a child process can be executed first or a parent process can be executed first.

so,the answer could be either  10 and 11 

                                              or 11 and 10.

 

if the question is msq type,then select both 10 and 11 ,11 and 10.

 


is my intution correct for following?

what is the value printed by child process ? 

will the “if condition” will be evaluated by child process or

 child process directly executes line 3?

main()
{
   1. int a = 10;
   2. if(fork()) == 5)) //not fork()==0
   3.    a++;
   4. printf("%d\n",a);  
}

since,a single line is converted into multiple machine level instructions,

so, the if condition will be evaluated and “if condition” fails.so,the value printed by child process is 10.

 

 

 

 

Answer:

Related questions

7 votes
7 votes
2 answers
1
sh!va asked May 7, 2017
6,475 views
Which of the following statement is true?Hard real time OS has less jitter than soft real time OSHard real time OS has more jitter than soft real time OSHard real time OS...
5 votes
5 votes
1 answer
2
sh!va asked May 7, 2017
7,491 views
The Linux command mknod myfifo b 4 16will create a character device if user is rootwill create a named pipe FIFO if user is rootwill create a block device if user is root...
6 votes
6 votes
4 answers
3
sh!va asked May 7, 2017
5,530 views
A critical regionis a piece of code which only one process executes at a timeis a region prone to deadlockis a piece of code which only a finite number of processes execu...