1 votes 1 votes main() { printf(" * "); for(i=0;i<n;i++) fork(); printf(" * "); } how many times * is printed?? shivanisrivarshini asked Jan 24, 2016 shivanisrivarshini 1.7k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 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 See all 7 7 Comments reply Show 4 previous comments Abhishekcs10 commented Jan 27, 2016 reply Follow Share 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 votes 0 votes bahirNaik commented Jan 27, 2016 reply Follow Share Yes I got it.But you said gcc gave you 10 as answer? 1 votes 1 votes Abhishekcs10 commented Jan 27, 2016 i edited by Abhishekcs10 Jan 27, 2016 reply Follow Share 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 votes 0 votes Please log in or register to add a comment.