1 1 vote main() { printf(" * "); for(i=0;i<n;i++) fork(); printf(" * "); } how many times * is printed?? Operating System + – shivanisrivarshini 2.5k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
2 2 votes 2n +1 Try drawing a tree to understand how each fork works. http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html bahirNaik answered Jan 24, 2016 • edited Jan 27, 2016 by bahirNaik bahirNaik comment Share Follow See all 7 Comments 7 7 Comments reply Show 4 previous comments Abhishekcs10 commented Jan 27, 2016 reply Follow flag n for loops 2^n -1 child process would be created so 2^n-1 time * would be printed by child process + 2 times by parent ...in total 2^n+1 '*' would be printed. Does it make clear...?? 0 0 replyShare bahirNaik commented Jan 27, 2016 reply Follow flag Yes I got it.But you said gcc gave you 10 as answer? 1 1 replyShare Abhishekcs10 commented Jan 27, 2016 i edited by Abhishekcs10 Jan 27, 2016 reply Follow flag oh yes i didn't noticed it should be 9....:p actually its giving different number of '*' everytime I execute..... http://ideone.com/2hSoxN ---> it now printed 16 * and here https://code.hackerearth.com/7e4258A--->its 14 and I have no explanation for that.....may be its because printf buffers output and thus when child executes printf it gets displayed twice.... but i have no proper explanation for the behaviour.... 0 0 replyShare Please log in or register to add a comment.