Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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/7/css/41.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 已失效的进程,fork()_C_Fork - Fatal编程技术网

C 已失效的进程,fork()

C 已失效的进程,fork(),c,fork,C,Fork,如果我运行这个程序,我会有失效的进程吗?我正在尝试创建一个主程序,并行运行5个进程,然后不会得到失效的进程。问题主要在于确保这种情况不会发生。我不太确定到目前为止我是否做得对。我听说这是一个很好的实践,通过让您的进程“wait()”来确保您没有失效的进程,因为有尽可能多的子进程已经“fork()”了 #包括 #包括 #包括 #包括 #包括 无效的子项(int-nrof子项,int*nru子项){ pid_t pid; int i; 对于(i=0;iYes),您需要在子进程上等待。原因是,否则仍然

如果我运行这个程序,我会有失效的进程吗?我正在尝试创建一个主程序,并行运行5个进程,然后不会得到失效的进程。问题主要在于确保这种情况不会发生。我不太确定到目前为止我是否做得对。我听说这是一个很好的实践,通过让您的进程“wait()”来确保您没有失效的进程,因为有尽可能多的子进程已经“fork()”了

#包括
#包括
#包括
#包括
#包括
无效的子项(int-nrof子项,int*nru子项){
pid_t pid;
int i;

对于(i=0;iYes),您需要在子进程上等待。原因是,否则仍然会有与现在的僵尸进程相关的数据,例如进程返回值的空间。

看看如何使用daemon()命令将您的应用程序放在后台,然后使用pthreads来管理并行性

名称 守护进程-在后台运行

简介 #包括

   int daemon(int nochdir, int noclose);
glibc的特性测试宏要求(参见 功能测试宏(7)):

到这些文件描述符


终止后,所有进程都将失效。如果你等待它们,它们将在很短的时间内成为僵尸。如果你退出或从main返回,它们将在很短的时间内成为僵尸,因为它们将被等待它们的init采用。为什么不使用returnvalue将子进程数传回to调用者,用愚蠢的
*nr\u of_children
指针指向结果,而不是void()函数?@wildplasser-Aha这是个主意,我没有想到,谢谢:-)那么你的意思是我必须在else语句中使用wait(NULL)来确保它吗?@patriques是的,否则你将有“僵尸”在系统中徘徊的进程。
   int daemon(int nochdir, int noclose);
   daemon(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500)
   If nochdir is zero, daemon() changes the process’s current working directory to the root directory ("/"); otherwise,

   If noclose is zero, daemon() redirects standard input, standard output and standard error to /dev/null; otherwise, no changes are made