Processes


Including the main, how many proceses does the following program create? Assume that there is no failure in process creation and proper header files have been included.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int main()
    {
      int n = 2;
      pid_t childpid;
      int i, j;

      for ( i = 0; i < n; i++ ) {
        for ( j = 0; j < n; j++ ) {
          if ( ( childpid = fork()) > 0 )
            break;         
        }
        wait (NULL);        // wait for child  
      }
      wait (NULL);  // wait for child  

      return 0;
    }


Is the answer to this 9? If not can someone explain their count and how I miscounted?

And if I changed n to 3 would it be more than 40?

It's n * n (or n2). for n = 2 -> 4, n = 3 -> 9 and so on
> Is the answer to this 9?

Yes.


> And if I changed n to 3 would it be more than 40?

Yes. It would be 64. (can you say why?)

http://coliru.stacked-crooked.com/a/6f855a095fdfe982
Topic archived. No new replies allowed.