edited by
8,670 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

Best answer
25 votes
25 votes

System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call.

In given question, for parent process fork call will return pid which will make if condition false. Hence parent process will print 10 

Child process will execute next instruction i.e. a++ because in child process if-condition is not tested. Execution starts from next instruction. So it will print 11

Answer should be A

selected by
7 votes
7 votes

option a

 if(fork() == 0) statement executes, fork() executes and a child process is created. So, At this point of time 2 processes are in the system (one is child process and other is the parent process). Return type of child process is 0, but Process ID(123) of child is returned to parent.now parent will become false then it print 10. and child process become true and it will print 11.

4 votes
4 votes
Fork will create two copies we know that ...and those two copies will be exactly the same with the same virtual addresses but obviously their physical addresses are different

once the control moves inside the if condition...the fork() call is executed which creates a copy of the parent in which the parent gets the id of the child which is a non zero value and the child gets a 0 value...now the process will fail the if condition while the child will pass

 

So the value printed by child is 11 while that by parent is 10.

Note:The order of execution of the child and the parent can't be decided so any one of them can end up last.So the o/p may be 10  11 or 11 10.Only option a matches so it's the correct answer
2 votes
2 votes

The return value of fork() is:-

  • 0 for child processes
  • Positive for parent processes
  • Negative, then it signifies some error.

You can think of it as their ages. Parents would definitely be aged 0+, and newborn children would be aged 0. There can't be valid negative age.

 

So, when the process executes

    if(fork()) == 0))

A child process is created. Since the current process is the parent process, fork() $\neq0$ so the if-condition is False. => Print 10.

The child process would start running from the NEXT instruction. So, a = 11 now. Then Print 11.

 

Option A

Answer:

Related questions

7 votes
7 votes
2 answers
1
sh!va asked May 7, 2017
6,411 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,447 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,453 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...