等待进程组时,waitpid()没有子进程错误

前端之家收集整理的这篇文章主要介绍了等待进程组时,waitpid()没有子进程错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
编写我自己的玩具外壳,并试图实现作业控制.

我正在使用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

有谁看到我做错了什么?可以发布更多代码,如果需要…

解决方法

我在waitpid手册页中忽略了sigchld:

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.

原文链接:https://www.f2er.com/c/116864.html

猜你在找的C&C++相关文章