fork() execution in for loop

后端 未结 3 1716
谎友^
谎友^ 2021-01-24 23:27
int main(int argc, char** argv) {
    int i = 0;
    while (i < 2) {
        fork();
        system(\"ps -o pid,ppid,comm,stat\");
        i++;
     }
     return (EX         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 00:09

    Initial Process
    i == 0
    -> Fork 1
       system call
       i == 1
       -> Fork 1.1
          system call
       system call
    system call
    i == 1
    -> Fork 2
       system call
    system call
    

    I count 6, 2 each from the initial process and the first fork (4), and one from each process forked when i == 1 from those 2 processes.

    Of course that's assuming you fix the missing end brace (and define EXIT_SUCCESS), otherwise none, since it won't compile. :-)

提交回复
热议问题