|
Home > Archive > Unix Programming > May 2005 > multiple fork's
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| Hi,
I have a simple program with multiple fork() calls.I am not able to
visualize how it works.The program is :
int main()
{
int ret;
ret=fork();
ret=fork();
ret=fork();
retr=fork();
if(!ret)
printf("child\n");
else
printf("parent\n");
return 0;
}
I can see that eash of the strings gets printed 8 times.Can anyone
explain how to visualize this ?
TIA
| |
| Pascal Bourguignon 2005-05-03, 2:49 am |
| grid <prohit99@gmail.com> writes:
> Hi,
> I have a simple program with multiple fork() calls.I am not able to
> visualize how it works.The program is :
>
> int main()
> {
> int ret;
> ret=fork();
> ret=fork();
> ret=fork();
> retr=fork();
> if(!ret)
> printf("child\n");
> else
> printf("parent\n");
> return 0;
> }
>
> I can see that eash of the strings gets printed 8 times.Can anyone
> explain how to visualize this ?
PID=100
|
v
(fork)----------------------->PID=101
| |
v v
res=101 res=0
| |
v v
(fork)----->PID=102 (fork)----->PID=103
| | | |
v v v v
res=102 res=0 res=103 res=0
etc with 2 more layers of forks.
--
__Pascal Bourguignon__ http://www.informatimago.com/
The mighty hunter
Returns with gifts of plump birds,
Your foot just squashed one.
| |
|
| >>I can see that eash of the strings gets printed 8 times.Can anyone
>
>
> PID=100
> |
> v
> (fork)----------------------->PID=101
> | |
> v v
> res=101 res=0
> | |
> v v
> (fork)----->PID=102 (fork)----->PID=103
> | | | |
> v v v v
> res=102 res=0 res=103 res=0
>
> etc with 2 more layers of forks.
>
Can i assume that the number of times it prints out is a function of n(2
to the power n), with half of the prints being that of parent and half
of the child's prints.So if n is 4 , I get 16 prints ( 8 from parent
process and 8 from child process).
Thanks,
| |
|
| > int main()
> {
> int ret;
> ret=fork();
> ret=fork();
> ret=fork();
> retr=fork();
> if(!ret)
> printf("child\n");
> else
> printf("parent\n");
> return 0;
> }
This almost similar program prints 7 times for parent and 7 times for
child(I have tried this on Solaris 8).
main (){
int pid;
int i;
for (i=0; i<3; i++){
if ((pid=fork()) <0) {
printf("Sorry, cannot fork\n");
} else if (pid == 0) {
printf("child %d\n", i);
} else {
printf("parent %d\n", i);}}
exit(0);}
Is these any difference between these two snippets that I get different
answers.
Thanks,
| |
|
| grid wrote:
>
> This almost similar program prints 7 times for parent and 7 times for
> child(I have tried this on Solaris 8).
> main (){
> int pid;
> int i;
> for (i=0; i<3; i++){
> if ((pid=fork()) <0) {
> printf("Sorry, cannot fork\n");
> } else if (pid == 0) {
> printf("child %d\n", i);
> } else {
> printf("parent %d\n", i);}}
> exit(0);}
>
> Is these any difference between these two snippets that I get different
> answers.
>
> Thanks,
Oops, sorry , didnt notice that the for loop I used was i < 3 and not
i <= 3.
For i<=3 , it prints 15 child's and 15 parents.
Iam confused ..
| |
| Chuck Dillon 2005-05-03, 6:00 pm |
| grid wrote:
> Hi,
> I have a simple program with multiple fork() calls.I am not able to
> visualize how it works.The program is :
>
> int main()
> {
> int ret;
> ret=fork();
> ret=fork();
> ret=fork();
> retr=fork();
> if(!ret)
> printf("child\n");
> else
> printf("parent\n");
> return 0;
> }
>
> I can see that eash of the strings gets printed 8 times.Can anyone
> explain how to visualize this ?
>
> TIA
Gen1 Gen2 Gen3 Gen4 Gen5
X
A
AA
AAA
AAAA <- child
AAB <- child
AB
ABA <- child
AC <- child
B
BA
BAA <- child
BB <- child
C
CA <- child
D <- child
View with monospaced font and tab size of at least 4.
-- ced
--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.
|
|
|
|
|