1 votes 1 votes #include <stdio.h> int main(void) { int i=0; printf("HAI\n"); for(i=1;i<=3;i++) { if(fork()==0) printf("*"); } return 0; } How many times * will be printed? and How many times HAI will be printed? Operating System operating-system fork-system-call + – Shaik Masthan asked Jun 4, 2018 • edited Nov 26, 2018 by Mk Utkarsh Shaik Masthan 905 views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply Shaik Masthan commented Jun 4, 2018 reply Follow Share According to me 6 *'s will be printed and only 1 HAI will be printed.... and moreover there are 3 parent processes and 4 child processes at the end but none of the online compilers shouldn't print these values.... Correct me if i am wrong... and mention which compiler is to use for gate purpose... 0 votes 0 votes Shaik Masthan commented Oct 26, 2018 reply Follow Share @tusharp i just pinged you ! 0 votes 0 votes Please log in or register to add a comment.
Best answer 5 votes 5 votes The number of "HAI" printed will be 1 time which will be at the beginning The number of "*" printed will be 2n-1 = 23-1 = 7 The structure will be where the diamond represents if condition More information on fork can be found here- http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html Asutosh answered Jun 26, 2018 • selected Jun 26, 2018 by Shaik Masthan Asutosh comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes Total number of fork() call in parent process =3 (according to loop). Total number of child processes = 23-1 ; Thus "*" will be printed 7 times. HAI will be printed 1 time. Shiv Gaur answered Jul 4, 2018 Shiv Gaur comment Share Follow See all 0 reply Please log in or register to add a comment.