Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fork() command

Status
Not open for further replies.

saphiroth

Programmer
Dec 15, 2004
34
0
0
US
Hi,
How does fork() work in c? I mean how does it execute? Does child process finish first then goes to parent process or do they perform operations at the same time?

my other question is, what if I want to have 3 differnet processes like process 1 is the parent so it has a child process 2, then process 2 uses fork() and gets a child process, process 3. How would I code this kind of thing...maybe use if thens.

Thank you
 
When you fork you create another process... They execute at the same time... sorta. Since processors in typical computers only execute one instruction at a time, only one process really has the CPU at any given nano second. But the OS is in charge of who gets the CPU when and tried to flip between them fast enough that it seems like each is happening at the same time. Which happens when is dependent on a number of things including load, how long the current process has been executing, how often a process blocks for I/O... and a host of other things. So there is no telling what instruction in each of the processes will occur first -- hence the need for sychronization.
 

The child is an independent process and runs in its own space. You have to coordinate these carefully because this is what causes orphans in a Unix system. Just because a parent program terminates does not mean that cleanup occurs and the children are terminated.

See the link for examples.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top