edited by
14,008 views
5 5 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

3 Answers

Best answer
10 10 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
18 18 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
Answer:
Position:
Show:

Related questions

1 1 vote
3 answers 3 answers
1.3k
1.3k views
Tesla! asked Apr 22, 2018
1,305 views
void main(){ fork(); fork(); printf("Hello"); }How many time print function will execute
3 3 votes
3 answers 3 answers
3.1k
3.1k views
go_editor asked Jun 12, 2016
3,126 views
Fork isthe creation of a new jobthe dispatching of a taskincreasing the priority of a taskthe creation of a new process
4 4 votes
3 3 answers
5.2k
5.2k views
Arjun asked Apr 22, 2018
5,182 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...
2 2 votes
2 answers 2 answers
1.7k
1.7k views
Arjun asked Apr 22, 2018
1,684 views
Determine the number of page faults when references to pages occur in the order - $1, 2, 4, 5, 2, 1, 2, 4$. Assume that the main memory can accommodate $3$ pages and the ...