Sign in with
Sign up | Sign in
Your question

Fork system call

Last response: in Linux/Free BSD
Share
September 18, 2011 4:56:01 PM

How many processes are needed to run this program?

Main()
{
Fork();
Fork ();
}

help would be appreciated..

More about : fork system call

September 18, 2011 7:21:19 PM

I'm sorry, but this really, really sounds like a homework question. If it is, do your own work. If it's not a homework question, please accept my apology and explain why you need to know such a thing
m
0
l
September 21, 2011 5:00:50 PM

:) sir actually i 9 hw its answer calculated by formula 2^n whr n is no. of fork() system calls...
mistakenly i posted dis question,,,first of ol genuinely sorry 4 that....
my actual question is how the no. of processes are calculated when this system call is used wid the combination of && and || operators....
for eg.

Main()
{
Fork();
Fork() && fork () || fork ();
Fork ();
}

I hv tried it so many times by generating tree in order these r called by parent nd child processes....but cudn't go beyond 16 processes....but its answer is 19,,,plz help me outta this...!!
m
0
l
October 30, 2011 3:56:49 AM

I'm not sure what language you're using, but give some help based on C/C++/perl. When you run fork(), it generates 1 new process, which can be confusing because you have two processes running when you're done. The process that was already running is the parent, and the new process is the child. If this is the parent process, then fork() returns the process id of the child, but if this is the child, it returns 0. If the fork fails in C/C++ (not sure on perl), then a -1 is returned to the running process, which is still a result that is "true", and there is no child.

Are you trying to generate a program that uses 19 processes? That part sounds like homework, but I'll give you some hints that should help. You'll probably need some nested if conditions, and you should start by figuring out how to generate 3 and 6 processes. That will at least give you some understanding of how to do it. If you start to draw out what is happening, to count up the processes, make sure you're only counting 1 new process for each fork command run, not 2.
m
0
l
!