我正在编写自己的玩具shell,并遇到了实现作业控制的问题。
我在子进程中和父进程中都使用setpgid设置子进程的进程组。我的等待调用如下:
pid = waitpid(-pid, &status, 0)
然而,waitpid返回-1并且perror显示“没有子进程”。不过,它似乎每次都在等待。此外,当从shell中运行时,ps输出看起来是正确的。由于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
有人能看出我哪里做错了吗?如果需要,可以发布更多的代码...