[algogeeks] Re: question on fork()
@Ankuj: Sorry. I read the getch() as a call to green(). Please ignore the 20 green(). It should be 10 green() instead. -- DK http://twitter.com/divyekapoor http://www.divye.in http://gplus.to/divyekapoor -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/hUgNWEGwH5MJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: question on fork()
6 red 10 greens On Wed, Aug 24, 2011 at 9:25 AM, Ankuj Gupta wrote: > how can be there 20 greens ? > > On Aug 24, 12:12 am, DK wrote: > > The standard library is neither multithread safe nor multiprocess safe. > > If you want the correct answer, use shared memory and maintain a shared > > counter. Alternatively, as a quick hack, insert a fflush(stdout) after > the > > printf statements. > > > > Answer: > > red() - 6 > > green() - 20 > > > > -- > > DK > > > > http://twitter.com/divyekapoorhttp://gplus.to/divyekapoor > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- *Shashi Kant * ***"Think positive and find fuel in failure"* *+919002943948 * *R&D engineer , Tejas Networks Ltd Banglore. * -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: question on fork()
how can be there 20 greens ? On Aug 24, 12:12 am, DK wrote: > The standard library is neither multithread safe nor multiprocess safe. > If you want the correct answer, use shared memory and maintain a shared > counter. Alternatively, as a quick hack, insert a fflush(stdout) after the > printf statements. > > Answer: > red() - 6 > green() - 20 > > -- > DK > > http://twitter.com/divyekapoorhttp://gplus.to/divyekapoor -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: question on fork()
The standard library is neither multithread safe nor multiprocess safe. If you want the correct answer, use shared memory and maintain a shared counter. Alternatively, as a quick hack, insert a fflush(stdout) after the printf statements. Answer: red() - 6 green() - 20 -- DK http://twitter.com/divyekapoor http://gplus.to/divyekapoor -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/KlDAxxIM-uoJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: question on fork()
I Can See 10 Green and 10 Red Process.. Can Someone Compile to verify this... Prem On Tue, Aug 23, 2011 at 1:24 AM, gmagog...@gmail.com wrote: > Infinite times > Yanan Cao > > > > On Mon, Aug 22, 2011 at 2:43 PM, Don wrote: > >> // DO NOT RUN THIS! By inspection, how many times will it print "Hello >> world"? >> // If you find out by running it, that is cheating. Don't do it! >> int main() >> { >> int i=0, j=0; >> for(i = 0; i*j < 20; ++i) >> { >>if (fork() > 0) ++j; >>else i = j = 0; >>printf("Hello world\n"); >> } >> return 0; >> } >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to algogeeks@googlegroups.com. >> To unsubscribe from this group, send email to >> algogeeks+unsubscr...@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> >> > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: question on fork()
Infinite times Yanan Cao On Mon, Aug 22, 2011 at 2:43 PM, Don wrote: > // DO NOT RUN THIS! By inspection, how many times will it print "Hello > world"? > // If you find out by running it, that is cheating. Don't do it! > int main() > { > int i=0, j=0; > for(i = 0; i*j < 20; ++i) > { >if (fork() > 0) ++j; >else i = j = 0; >printf("Hello world\n"); > } > return 0; > } > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
Re: [algogeeks] Re: question on fork()
I am getting 6 red and 8 green as expected using the original code Yanan Cao On Mon, Aug 22, 2011 at 2:38 PM, Yasir wrote: > Surprisingly, if I comment the last if condition ( which is AFTER red() > call ), it is printing red only 6 times as expected.. > http://ideone.com/XMHzC > > main() > { > fork(); > int color=fork(); > if(color==0) > fork(); > red(); > //if(color==0) > // fork(); > green(); > getch(); > > } > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/algogeeks/-/1o7KQKelcswJ. > > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: question on fork()
// DO NOT RUN THIS! By inspection, how many times will it print "Hello world"? // If you find out by running it, that is cheating. Don't do it! int main() { int i=0, j=0; for(i = 0; i*j < 20; ++i) { if (fork() > 0) ++j; else i = j = 0; printf("Hello world\n"); } return 0; } -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: question on fork()
Surprisingly, if I comment the last if condition ( which is AFTER red() call ), it is printing red only 6 times as expected.. http://ideone.com/XMHzC main() { fork(); int color=fork(); if(color==0) fork(); red(); //if(color==0) // fork(); green(); getch(); } -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/algogeeks/-/1o7KQKelcswJ. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
[algogeeks] Re: question on fork()
I am getting 6 calls to red and 8 calls to green when i built parent child tree but when i ran this code http://ideone.com/UBaBB I got 10 calls to red and 10 calls to green. Can some explain this ? On Aug 22, 9:31 pm, ghsjgl k wrote: > i saw this question in one of DREAM companies > > i dont know the answer > > > > > > > > On Mon, Aug 22, 2011 at 11:43 AM, dexter does wrote: > > void red() > > { > > > } > > void green() > > { > > > } > > main() > > { > > fork(); > > int color=fork(); > > if(color==0) > > fork(); > > red(); > > if(color==0) > > fork(); > > green(); > > getch(); > > > } > > > How many times red and green are called and pls give an explanation for > > your answer > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.