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?Only once2 times4 times8 times Operating System isro2018 fork-system-call operating-system easy + – Arjun 14.0k views answer comment Share Follow Print See 1 comment 1 1 comment reply Tuhin Dutta commented Apr 26, 2018 reply Follow flag Ans C) #fork calls given in program = 2. Each fork call creates 2 processes (child + parent). #child processes created here = $2^n-1$ = 3 // here n = 2; all of them prints "Yes" #parent process = 1 //prints "Yes" Thus total 4 prints. 3 3 replyShare Please log in or register to add a comment.
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 sonveer tomar 1 answered Apr 22, 2018 • selected Apr 23, 2018 by ManojK sonveer tomar 1 comment Share Follow 0 reply Please log in or register to add a comment.
18 18 votes 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 Hira Thakur answered Apr 22, 2018 • edited Jun 23, 2022 by Hira Thakur Hira Thakur comment Share Follow See 1 comment 1 1 comment reply `JEET commented Jan 2, 2020 reply Follow flag This is a much better explanation. 1 1 replyShare Please log in or register to add a comment.
9 9 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 . abhishekmehta4u answered Apr 22, 2018 abhishekmehta4u comment Share Follow See 1 comment 1 1 comment reply `JEET commented Jan 2, 2020 reply Follow flag Nice explanation. 2 2 replyShare Please log in or register to add a comment.