Jump to content
xisto Community
Sign in to follow this  
sonalurfrnd

Help With Fork()!

Recommended Posts

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

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

@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

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

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

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

@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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.