选择哪一个waitpid函数/等待/ waitid? [英] Which one to choose waitpid/wait/waitid?
问题描述
我想这样做叉后使用的子进程EXECL。 EXECL将执行这将需要大约120秒的时间脚本。我几乎尝试了所有waitpid函数的结合,等待与不同参数(0,WNOHANG等),但在所有的情况下,我得到-1返回值waitid。所以,我想知道在等待功能,我需要的时候使用?所以,我可以在一个等待功能集中,使其工作。
这是我从我的日志发现的一个更有趣的是,当我无所事事在子进程它显示为孤立我的父线程。我不知道它是如何可能的?我父线程如何才能成为孤儿?
的#include<&errno.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / wait.h>
#包括LT&;&unistd.h中GT;INT主要(无效)
{
将为pid_t Checksum_pid = fork()的; 如果(Checksum_pid℃,)
的printf(叉失败\\ n);
否则,如果(Checksum_pid == 0)
{
EXECL(/斌/ LS,LS,(字符*)NULL);
出口(EXIT_FAILURE);
}
其他
{
INT childStatus;
将为pid_t的returnValue = waitpid函数(Checksum_pid,&放大器; childStatus,0); 如果(的returnValue大于0)
{
如果(WIFEXITED(childStatus))
的printf(退出code:%d个\\ N,WEXITSTATUS(childStatus)); }
否则,如果(的returnValue == 0)
的printf(子进程仍在运行\\ n);
其他
{
如果(错误== ECHILD)
的printf(错误ECHILD!\\ n);
否则,如果(错误== EINTR)
的printf(错误EINTR!\\ n);
其他
的printf(错误EINVAL!\\ n);
}
} 返回0;
}
当我说:你上次其他
应该只是
其他PERROR(waitpid函数);
但你得到 ECHILD
。所以,请阅读 waitpid函数(2)手册页:
ECHILD(对于等待())调用进程没有任何unwaited-
为儿童。 ECHILD(对于waitpid函数()或waitid())由pid指定的进程
(waitpid函数())或idtype和id(waitid())不存在或为
不是调用进程的孩子。 (发生这种情况的
自己的孩子,如果为SIGCHLD的动作设置为SIG_IGN。
另请参见有关线程的Linux的注释部分)。
块引用>顺便说一句,我不能重现你的错误。还要检查你的极限作为庆典由
的ulimit -a
给出。也许你的
EXECL
失败(特别是当你执行一些脚本,而不是/斌/ LS
)。之后,将它添加到PERROR
的电话。此外,编译
的gcc -Wall -g
,并使用strace的
和GDB
I want to use execl in child process after doing fork. execl will execute the script which will take around 120 seconds of time. I tried almost all the combination with waitpid, wait and waitid with different argument (0, WNOHANG, etc.,) but in all case I am getting -1 return value. So I want to know in which wait function I need to use when? So I can concentrate in one wait function to make it work.
One more interesting thing which I observed from my logs is that when I am doing nothing in child process it is showing my parent thread as orphaned. I don't know how it is possible? How can my parent thread become orphaned?
#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <unistd.h> int main(void) { pid_t Checksum_pid = fork(); if (Checksum_pid < 0) printf("Fork Failed\n"); else if (Checksum_pid == 0) { execl("/bin/ls","ls",(char *)NULL) ; exit(EXIT_FAILURE); } else { int childStatus; pid_t returnValue = waitpid(Checksum_pid, &childStatus, 0); if (returnValue > 0) { if (WIFEXITED(childStatus)) printf("Exit Code: %d\n", WEXITSTATUS(childStatus)); } else if (returnValue == 0) printf("Child process still running\n"); else { if (errno == ECHILD) printf(" Error ECHILD!!\n"); else if (errno == EINTR) printf(" Error EINTR!!\n"); else printf("Error EINVAL!!\n"); } } return 0; }
解决方案As I commented: your last
else
should just beelse perror("waitpid");
But you are getting
ECHILD
. So please read waitpid(2) man page:ECHILD (for wait()) The calling process does not have any unwaited- for children. ECHILD (for waitpid() or waitid()) The process specified by pid (waitpid()) or idtype and id (waitid()) does not exist or is not a child of the calling process. (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.)
BTW, I cannot reproduce your error. Check also your limits as given by
ulimit -a
on bash.Maybe your
execl
fails (especially if you execute some script instead of/bin/ls
). Add a call toperror
after it.Also, compile with
gcc -Wall -g
and usestrace
andgdb
这篇关于选择哪一个waitpid函数/等待/ waitid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!