Small Fork Problem

  • Thread starter thrillseekersforever
  • Start date
T

thrillseekersforever

The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!
A]

void main() {
....
pid1 = fork();
if (pid1) pid2 = fork();
.....
}

Solution:

In the first two lines, the parent process forks a child (pid1), then
the same parent (the process holding a nonzero pid1) forks another
child (pid2). Therefore, the effect of the first two lines was to
multiply the number of processes by 3.


B]

void main() {
....
pid1 = fork(); pid2 = fork();
if (pid1 || pid2) execvp(...);
pid3 = fork(); pid4 = fork();
if (pid3 || pid4) execvp(...);
pid5 = fork(); pid6 = fork();
if (pid5 || pid6) execvp(...);
}


In the first line, the two unconditional forks bring the number of
processes to 4. Then, in the second line, 3 out of these 4 processes
are exec'd toward another program: they are the processes that were
playing the parent role either during fork1 or during fork2. So,
conversely, only the fork2-child of the fork1-child will continue
executing this code (i.e., the process with both pid1 and pid2 zero).
Therefore, the next two lines apply only to that remaining process,
which also creates 3 more processes, of which only one remains (the
fork4-child of the fork3-child). Then, the last two lines repeat
exactly the same pattern.
In conclusion, the total number of processes that were created
(whether exec'd or not) is:
1 + 3 + 3 + 3 = 10.
 
I

Ian Collins

The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!

Try comp.unix.programmer, fork() is OT here.
 
M

Martin Ambuhl

The questions(A&B) are to fine no# of process running from the below
codes. However, I couldn't decipher the solution. Will someone please
throw some light on this? Thanks a lot!!
A]

void main() {
...
pid1 = fork();
if (pid1) pid2 = fork();
....
}

Solution:

In the first two lines, the parent process forks a child (pid1), then
the same parent (the process holding a nonzero pid1) forks another
child (pid2). Therefore, the effect of the first two lines was to
multiply the number of processes by 3.


B]

void main() {
...
pid1 = fork(); pid2 = fork();
if (pid1 || pid2) execvp(...);
pid3 = fork(); pid4 = fork();
if (pid3 || pid4) execvp(...);
pid5 = fork(); pid6 = fork();
if (pid5 || pid6) execvp(...);
}


In the first line, the two unconditional forks bring the number of
processes to 4.

No, in the first line you misdeclare the return type of main(), making
everything that follows irrelevant.
Then, in the second line, 3 out of these 4 processes
are exec'd toward another program:

In the lines subsequent to the "..." syntax error, you use functions
that are not part of the standard library and which have no declaration
in scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fork() 5
Problem regarding process 1
fork/exec question 6
Clocking proccesses' time 3
Linux: using "clone3" and "waitid" 0
How to use shared memory with fork() ? 5
fork() 27
Forking into the background (Linux) 1

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top