CODE
void main()
{
int i=0;
fork();
printf("%d", i++);
fork();
printf("%d", i++)
fork();
wait();
}
{
int i=0;
fork();
printf("%d", i++);
fork();
printf("%d", i++)
fork();
wait();
}
please help mw with output of this program
|
|
|
CODE void main() { int i=0; fork(); printf("%d", i++); fork(); printf("%d", i++) fork(); wait(); } please help mw with output of this program
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].
@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.
Dear osknockout,
Is frok() is smiler to "Thread" [in java]. You told that QUOTE 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.
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 to make threads using miniature processes. Tell me if that makes sense.
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.
@rajibbd: No problem, here to help. ^^
@sonalurfrnd: Is the code still a problem or is this topic basically over...?
Basically fork() is used to create a new process in UNIX shell Programming .
|
|
![]() Help With Fork()! |
| ADD REPLY / Got an Opinion! | Remove these ADs! | RAPID SEARCH! | Free Web Hosting | [X] |
|
Express your Opinions, Thoughts or Contribute more info. to help others. Ask your Doubts & Queries to get answers, So that "Together We can help others!" |
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP. | 500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE |
|