等待进程组时没有来自waitpid()的子进程错误 [英] No Child Process Error from waitpid() when waiting for process group

查看:28
本文介绍了等待进程组时没有来自waitpid()的子进程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在编写我自己的玩具外壳,并且在尝试实现作业控制时遇到了麻烦.

Writing my own toy shell, and have run into a bump trying to implement job control.

我正在使用 setpgid 在子进程和父进程中设置子进程组.我的等待电话是:

I am setting the process group of the child, both in the child and the parent with setpgid. My wait call is:

pid = waitpid(-pid, &status, 0)

然而,waitpid 返回 -1 并且 perror 显示没有子进程".然而,它似乎每次都在等待.此外,从 shell 运行时, ps 输出看起来是正确的.由于 ps parent 的进程是 kbsh,就像我期望的那样.

However, waitpid returns -1 and perror says "No child process". However, it does seem to wait every time. Also, ps output looks right to mean when run from the shell. Since ps parent's process is kbsh like I would expect.

% 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

有人看到我做错了什么吗?如果需要可以发布更多代码...

Anyone see what I am doing wrong? can post more code if need be...

推荐答案

我忽略了 sigchld,来自 waitpid 手册页:

I was ignoring sigchld, from the waitpid man page:

POSIX.1-2001 规定,如果SIGCHLD 的处置设置为SIG_IGN 或 SA_NOCLDWAIT 标志是为 SIGCHLD 设置(参见 sigaction(2)),那么终止的孩子不会变成僵尸并调用 wait() 或waitpid() 将阻塞直到所有孩子们已经终止,然后将 errno 设置为 ECHILD 时失败.(原始 POSIX 标准离开了将 SIGCHLD 设置为的行为SIG_IGN 未指定.)Linux 2.6符合本规范.但是,Linux 2.4(及更早版本)确实不是:如果 wait() 或 waitpid() 调用是在 SIGCHLD 被忽略时进行,调用的行为就像SIGCHLD 没有被忽略,即是,调用阻塞直到下一个孩子终止,然后返回该子进程的进程 ID 和状态.

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.

这篇关于等待进程组时没有来自waitpid()的子进程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆