425,763 Members | 1,641 Online Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

# Small Fork Problem

 P: n/a 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. Mar 5 '07 #1
3 Replies

 P: n/a th******************@gmail.com wrote: 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. -- Ian Collins. Mar 5 '07 #2

 P: n/a On Mar 5, 2:19 pm, Ian Collins

 P: n/a th******************@gmail.com wrote: 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. Mar 5 '07 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 