1
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main()
{
    int i = 0;
    fork();
    for(;i<3; ++i)
        printf("%d", i);
    fork();
    return 0;
}

Here is my code. I want to know how many processes do I have after executing the last fork()

asky
  • 1,520
  • 12
  • 20
Mehdi Gilanpour
  • 156
  • 2
  • 13

1 Answers1

3

Fork splits the current process into 2 processes, so you have 2 after the first fork and 4 after the second.

Edit: After the first fork() there will be two processes, both executing the following statements. The initial process and the forked process will both call fork() the second time, resulting in 4 total processes after that call. For more info check out this link: http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html

jpetrichsr
  • 247
  • 2
  • 11
  • After the first fork() there will be two processes, both executing the following statements. The initial process and the forked process will both call fork() the second time, resulting in 4 total processes after that call. http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html – jpetrichsr Nov 24 '17 at 20:58
  • The whole point is here. I don`t know if the answer is three or four – Mehdi Gilanpour Nov 24 '17 at 20:59
  • Done! Good call. – jpetrichsr Nov 24 '17 at 21:01