PDA

View Full Version : [SOLVED] need insight of fork()



piyush.neo
August 10th, 2010, 07:24 PM
As fork() is known to be a function which is called once but return twice....
What i know about the fork:
when fork is called it replicates the whole program and create a process and return with pid in parent.
The replicated program also runs from the point after which fork is called b'coz it copied parent's stack(PC etc) also and return 0.
Am i right?
if no plz correct n if yes plz en-light me by further giving some refence to study
Thanks..:p

Bachstelze
August 10th, 2010, 07:31 PM
Yes.

dwhitney67
August 10th, 2010, 08:10 PM
Your answer is here:


man fork

piyush.neo
August 11th, 2010, 01:52 PM
Your answer is here:


man fork

have already done that....i have 1 doubt-
do child process start from the instruction fork() or the instruction next to fork?
if it start form fork then why doesn't it create a new process too, and if from next instruction after fork() then how does 0 value is returned by fork to child process as in

pid=fork()
...
.....
....

dwhitney67
August 11th, 2010, 02:12 PM
Maybe I don't quite understand your question. In the example you provided, the value of pid is 0 for the child; but for the parent, it is the process-id of the child. Don't try to interpret more into this than what I have already described.

P.S. The child process begins after the fork(). Thus it does not invoke the fork() call; the parent does.

piyush.neo
August 11th, 2010, 03:04 PM
Maybe I don't quite understand your question. In the example you provided, the value of pid is 0 for the child; but for the parent, it is the process-id of the child. Don't try to interpret more into this than what I have already described.

P.S. The child process begins after the fork(). Thus it does not invoke the fork() call; the parent does.

i know i am getting more into it than need. Actully thats part of my assignment..
If fork in not invoked in child, then how it can return 0 to child ?

Bachstelze
August 11th, 2010, 03:18 PM
i know i am getting more into it than need. Actully thats part of my assignment..
If fork in not invoked in child, then how it can return 0 to child ?

It places the value 0 on the child's stack frame, where the value returned by fork() is supposed to be.

piyush.neo
August 12th, 2010, 09:34 AM
Thanks...