using fork in an if-statement/ managing processes

前端 未结 2 1653
死守一世寂寞
死守一世寂寞 2021-01-26 11:31

I have this bit of code:

   printf(\"L1 \");

   if(fork() != 0) {
      printf(\"L2 \");

      if(fork() != 0) {

         printf(\"L3 \");
         fork();
          


        
2条回答
  •  后悔当初
    2021-01-26 12:10

    In your code, the child of each fork proceeds directly to the printf("End"). Only the parent will print L1, L2, L3, so they'll only be printed once each, in that order. Mixed in with that output would be the Ends of the children (and the final end of the parent).

提交回复
热议问题