sonalurfrnd 0 Report post Posted August 9, 2007 help me with this: - void main(){ int i=0; fork(); printf("%d", i++); fork(); printf("%d", i++) fork(); wait();} please help mw with output of this program Notice from Plenoptic: Added code tags. Any code, or copied content not in your own words must be placed in the proper BBCode tags. Please review the Xisto ReadMe for further rules and guidelines. Share this post Link to post Share on other sites
rajibbd 0 Report post Posted August 9, 2007 Though i am not good at C/C++. But seeing this code what i can get that, the main function have a inter value of 'i' assigning to '0', after then there was a function called "fork()" my question is what did its do. then the printf function should print 1[because 'i' is increasing]. Then again and again printf function which should print 2 for same case and there is another fork. and a wait function[with out giving how far we should wait]. Share this post Link to post Share on other sites
osknockout 0 Report post Posted August 10, 2007 @rajibbd: fork() creates a process running the program. If you had two cpu's, you'd theoretically fork a program once to have parts of it running on both.@sonalufrnd: Ok, what exactly are you trying to do? It looks like you're trying to get the result "1 1 2 2 2 2".It'd help if you could tell us what the aim of the code is.- and you're missing a semicolon on the second printf().I'm under the impression that you need to get only the parent process to wait() otherwise all processes wil be waiting for other processes to finish that don't exist- I haven't tested the code or played with processes for a while, so I'm not exactly sure. However, if that is the case, you need to be able to distinguish the parent process from the others. This page should help a good bit. Share this post Link to post Share on other sites
rajibbd 0 Report post Posted August 10, 2007 Dear osknockout, Is frok() is smiler to "Thread" [in java]. You told that fork() creates a process running the program. If you had two cpu's, you'd theoretically fork a program once to have parts of it running on both.. I am very eager to know what is it. Share this post Link to post Share on other sites
osknockout 0 Report post Posted August 10, 2007 Yeah, fork() basically is Thread - if you're talking about the public class in Java.A process and a thread are technically different things, but the words are used almost interchangably at times.Technically, you have one process running with multiple threads, but with the fork() command, the aim is tomake threads using miniature processes. Tell me if that makes sense. Share this post Link to post Share on other sites
rajibbd 0 Report post Posted August 11, 2007 This is pretty much better. Thank you Mr. Osknockout. I have some idea about Thread in Java. Now Know similer use of Thread[though it is not similer]. But any way thank you again. Share this post Link to post Share on other sites
osknockout 0 Report post Posted August 12, 2007 @rajibbd: No problem, here to help. ^^@sonalurfrnd: Is the code still a problem or is this topic basically over...? Share this post Link to post Share on other sites
syncn21 0 Report post Posted September 26, 2007 Basically fork() is used to create a new process in UNIX shell Programming . Share this post Link to post Share on other sites