我正在使用setpgid在子级和父级中设置子级的进程组.我的等待电话是:
pid = waitpid(-pid,&status,0)
但是,waitpid返回-1并且perror说“没有子进程”.但是,它确实似乎每次都在等待.另外,ps输出在从shell运行时看起来正确.因为ps父级的进程就像我期望的那样是kbsh.
% ps -o pid,ppid,pgrp,session,tpgid,comm Forking In Parent: Setting process group to 20809 of process 20809 with setpgid In Child Processes,pid of child process is 20809 in Child: Setting process group to 20809 of process 20809 with setpgid Requesting that Process Group 20809 becomes the foreground process with tcsetpgrp Waiting for job with process group 20809 PID PPID PGRP SESS TPGID COMMAND 12002 32573 12002 12002 20809 zsh 20808 12002 20808 12002 20809 kbsh 20809 20808 20809 12002 20809 ps Wait Error: No child processes Restoring Shell process group 20808 to forground
有谁看到我做错了什么?可以发布更多代码,如果需要…
解决方法
POSIX.1-2001 specifies that if the
disposition of SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag is set for SIGCHLD (see sigaction(2)),then children that terminate do not become zombies and a call to wait() or waitpid() will block until all children have terminated,and then fail with errno set to ECHILD. (The original POSIX standard left the behavIoUr of setting SIGCHLD to SIG_IGN unspecified.) Linux 2.6 conforms to this specification. However,Linux 2.4 (and earlier) does not: if a wait() or waitpid() call is made while SIGCHLD is being ignored,the call behaves just as though SIGCHLD were not being ignored,that is,the call blocks until the next child terminates and then returns the process ID and status of that child.