Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many processes are created with these fork() statements?

I believe that this creates 24 processes; however, I need verification. These questions often stump me. Thanks for the help!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
  pid_t pid = fork();
  pid = fork();
  pid = fork();
  if (pid == 0)
  {
    fork();
  }
  fork();
  return 0;
}
like image 985
Ben Reed Avatar asked Oct 01 '13 01:10

Ben Reed


People also ask

What is fork () in OS if I execute fork (); fork (); then how many number of child process can be created?

Explanation: If there are n fork() calls, then the number of child processes created is 2n – 1.

How many total processes are created if parent is executing two fork system calls as fork (); fork (); draw its activation tree also?

6 Answers. The answer using fork() four times is: 2 * 2 * 2 * 2 - 1 = 16 - 1 = 15 processes.

What does fork () do in C?

In the computing field, fork() is the primary method of process creation on Unix-like operating systems. This function creates a new copy called the child out of the original process, that is called the parent. When the parent process closes or crashes for some reason, it also kills the child process.


2 Answers

It's fairly easy to reason through this. The fork call creates an additional process every time that it's executed. The call returns 0 in the new (child) process and the process id of the child (not zero) in the original (parent) process.

pid_t pid = fork();  // fork #1
pid = fork();        // fork #2
pid = fork();        // fork #3
if (pid == 0)
{
  fork();            // fork #4
}
fork();              // fork #5
  1. Fork #1 creates an additional processes. You now have two processes.
  2. Fork #2 is executed by two processes, creating two processes, for a total of four.
  3. Fork #3 is executed by four processes, creating four processes, for a total of eight. Half of those have pid==0 and half have pid != 0
  4. Fork #4 is executed by half of the processes created by fork #3 (so, four of them). This creates four additional processes. You now have twelve processes.
  5. Fork #5 is executed by all twelve of the remaining processes, creating twelve more processes; you now have twenty-four.
like image 135
sfstewman Avatar answered Oct 17 '22 16:10

sfstewman


Calculate in this way :

Start with 1(Main Process) and for every fork make it twice if fork is not inside if(pid == 0) else add 1/2 of current process to current number of process.

In your code: 1P Got #1 fork() so double up current number of processes. Now new number of process 2P

Got #2 fork() so double up current number of processes. Now new number of process 4P

Got #3 fork() so double up current number of processes. Now new number of process 8P

Got #4 fork() but wait it's in if condition so (8+4 = 12)P

Got #5 fork() so double up the current number of processes. Now new number of process 24P

like image 3
Roshan Mehta Avatar answered Oct 17 '22 17:10

Roshan Mehta