Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 使用wait在所有进程完成但输出异常后等待_C_Linux_Fork - Fatal编程技术网

C 使用wait在所有进程完成但输出异常后等待

C 使用wait在所有进程完成但输出异常后等待,c,linux,fork,C,Linux,Fork,我想用四个并行进程做一些事情 我首先fork onec,然后在子和父fork中再次得到4个进程 我想要的是在所有4个进程完成后执行一些操作,因此我使用waitpid(-1,&status,0) 我的理想输出是 in numbers out 但实际产出有时可能是 in numbers out one number 我想不出来 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #in

我想用四个并行进程做一些事情

我首先fork onec,然后在子和父fork中再次得到4个进程

我想要的是在所有4个进程完成后执行一些操作,因此我使用
waitpid(-1,&status,0)

我的理想输出是

in

numbers

out
但实际产出有时可能是

in

numbers

out

one number
我想不出来

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <error.h>

int main()
{
    pid_t cpid, w;
    int status;

    printf("%s\n", "in");
      cpid = fork();
      if (cpid == 0)
      {
          cpid = fork();
          if (cpid == 0)
          {
            printf("%s\n", "1");
            exit(EXIT_SUCCESS);
          }
          else{
            printf("%s\n", "2");
            exit(EXIT_SUCCESS);
          }
      }
      else{
          cpid = fork();
          if (cpid == 0)
          {
            printf("%s\n", "3");
            exit(EXIT_SUCCESS);
          }
          else{
              printf("%s\n", "4");
              //exit(EXIT_SUCCESS);
          }      
      }
      waitpid(-1, &status, 0); 
      printf("%s\n", "out");
      //do something
      exit(EXIT_SUCCESS);
    return 0 ;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
pid_t cpid,w;
智力状态;
printf(“%s\n”、“in”);
cpid=fork();
如果(cpid==0)
{
cpid=fork();
如果(cpid==0)
{
printf(“%s\n”,“1”);
退出(退出成功);
}
否则{
printf(“%s\n”,“2”);
退出(退出成功);
}
}
否则{
cpid=fork();
如果(cpid==0)
{
printf(“%s\n”,“3”);
退出(退出成功);
}
否则{
printf(“%s\n”,“4”);
//退出(退出成功);
}      
}
waitpid(-1,状态为0(&S);
printf(“%s\n”、“out”);
//做点什么
退出(退出成功);
返回0;
}

您正在使用pid=-1的waitpid()。它等待任何子进程完成。这意味着,一旦任何一个子进程完成,父进程的waitpid()就会退出。它不会等待任何其他子进程完成

处理场景的更好方法是在主进程中使用“SIGCHLD”的signale处理程序,并使用wait/waitpid获取子退出案例。在这种情况下,可以监控/通知您的所有儿童死亡,因为SIGCLD将传递到主流程