recategorized by
10,798 views
3 votes
3 votes

The following $C$ program:

{
    fork(); fork(); printf("yes");
    
}

If we execute this core segment, how many times the string yes will be printed?

  1. Only once
  2. 2 times
  3. 4 times
  4. 8 times
recategorized by

3 Answers

Best answer
9 votes
9 votes
code can be re-written as

main {

fork();

fork();

printf("yes");

}

first fork() will going to create a child process,which is going to execute the next instruction i.e 2nd fork() and parent process also going to execute 2nd fork.

so now 2 more process will be created. so in system now there are 4 process and they are going to execute printf statement. so total no, of yes will be 4

++> Total number of times yes will be printed = 4
selected by
15 votes
15 votes

image

The FORK system call creates a child process & execution of FORK is in kernel mode. Its conversion takes place from user mode to system mode. A corresponding child process will be created in the program for every parent's process. so 4 times print statements are executed.

Ref: 1) GATE CSE 2012

       2) Understanding fork() system call

edited by
7 votes
7 votes

OPTION C.

total no of child process is created=2^n-1 so"yes" is printed 3 times  and one "yes" is printed by parent .

Answer:

Related questions

3 votes
3 votes
2 answers
1
Arjun asked Apr 22, 2018
3,728 views
The difference between a named pipe and a regular file in Unix is thatUnlike a regular file, named pipe is a special fileThe data in a pipe is transient, unlike the conte...
4 votes
4 votes
5 answers
3
Arjun asked Apr 22, 2018
4,868 views
Consider a system having $m$ resources of the same type. These resources are shared by $3$ processes $A, B, C,$ which have peak time demands of $3, 4, 6$ respectively. Th...