首页 > 代码库 > 6进程原语:wait()和waitpid()函数
6进程原语:wait()和waitpid()函数
1 wait/waitpid
僵尸进程:子进程退出,父进程没有回收子进程资源(PCB),则子进程变成僵尸进程
孤儿进程:父进程先于子进程结束,则子进程成为孤儿进程,子进程的父进程成为1号
进程init进程,称为init进程领养孤儿进程
2依赖的头文件
#include <sys/types.h>
#include <sys/wait.h>
3函数声明
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, intoptions);
The value of pid can be:
< -1 meaning wait for anychild process whose process group ID is
equal to the absolute value ofpid.
-1 meaning wait for any childprocess.
0 meaning wait for any child process whose process group ID is
equal to that of the callingprocess.
> 0 meaning wait for thechild whose process ID is equal to the
value of pid.
< -1回收指定进程组内的任意子进程
-1回收任意子进程
0回收和当前调用waitpid一个组的所有子进程
> 0回收指定ID的子进程
3.说明:
一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的PCB还
保留着,内核在其中保存了一些信息:如果是正常终止则保存着退出状态,如果是异常终止
则保存着导致该进程终止的信号是哪个。这个进程的父进程可以调用wait或waitpid获取这
些信息,然后彻底清除掉这个进程。我们知道一个进程的退出状态可以在Shell中用特殊变
量$?查看,因为Shell是它的父进程,当它终止时Shell调用wait或waitpid得到它的退出状
态同时彻底清除掉这个进程。
如果一个进程已经终止,但是它的父进程尚未调用wait或waitpid对它进行清理,这时
的进程状态称为僵尸(Zombie)进程。任何进程在刚终止时都是僵尸进程,正常情况下,僵
尸进程都立刻被父进程清理了,为了观察到僵尸进程,我们自己写一个不正常的程序,父进
程fork出子进程,子进程终止,而父进程既不终止也不调用wait清理子进程:
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
pid_t pid=fork();
if(pid<0) {
perror("fork");
exit(1);
}
if(pid>0) { /* parent */
while(1);
}
/* child */
return 0;
}
若调用成功则返回清理掉的子进程id,若调用出错则返回-1。父进程调用wait或
waitpid时可能会:
*阻塞(如果它的所有子进程都还在运行)。
*带子进程的终止信息立即返回(如果一个子进程已终止,正等待父进程读取其终止信
息)。
*出错立即返回(如果它没有任何子进程)。
这两个函数的区别是:
*如果父进程的所有子进程都还在运行,调用wait将使父进程阻塞,而调用waitpid时如果在options参数中指定WNOHANG可以使父进程不阻塞而立即返回0。
* wait等待第一个终止的子进程,而waitpid可以通过pid参数指定等待哪一个子进程。
可见,调用wait和waitpid不仅可以获得子进程的终止信息,还可以使父进程阻塞等待子进
程终止,起到进程间同步的作用。如果参数status不是空指针,则子进程的终止信息通过
这个参数传出,如果只是为了同步而不关心子进程的终止信息,可以将status参数指定为
NULL。
例waitpid
The value of options is an OR of zero or more of the following con‐
stants:
WNOHANG return immediately ifno child has exited.
WUNTRACED also return if a childhas stopped (but not traced via
ptrace(2)). Status for traced children which havestopped
is provided even if thisoption is not specified.
WCONTINUED (since Linux 2.6.10)
also return if a stoppedchild has been resumed by delivery
of SIGCONT.
(For Linux-only options, see below.)
If status is not NULL, wait() and waitpid()store status information in
the int to which it points. Thisinteger can be inspected with the
following macros (which take the integer itself as anargument, not a
pointer to it, as is done in wait() and waitpid()!):
WIFEXITED(status)
returns true if the childterminated normally, that is, by call‐
ing exit(3) or _exit(2), or byreturning from main().
WEXITSTATUS(status)
returns the exit status of thechild. This consists of the
least significant 8 bits of thestatus argument that the child
specified in a call to exit(3) or _exit(2) oras the argument
for a return statement inmain(). This macro should be employed
only if WIFEXITED returned true.
WIFSIGNALED(status)
returns true if the child processwas terminated by a signal.
WTERMSIG(status)
returns the number of the signal that caused the child process
to terminate. This macro should be employed only ifWIFSIGNALED
returned true.
WCOREDUMP(status)
returns true if the child produced a core dump. This macro
should be employed only if WIFSIGNALED returned true. This
macro is notspecified in POSIX.1-2001 and is not available on
some UNIX implementations (e.g.,AIX, SunOS). Only use this
enclosed in #ifdef WCOREDUMP ...#endif.
WIFSTOPPED(status)
returns true if the child process was stopped by delivery of a
signal; this is possible only ifthe call was done using WUN‐
TRACED or when the child is beingtraced (see ptrace(2)).
WSTOPSIG(status)
returns the number of the signalwhich caused the child to stop.
This macro should be employedonly if WIFSTOPPED returned true.
WIFCONTINUED(status)
(since Linux 2.6.10) returns true if the child process was
resumed by delivery of SIGCONT.
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
pid_t cpid, w;
int status;
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /*Code executed by child */
printf("Child PID is%ld\n", (long) getpid());
if (argc == 1)
pause(); /* Wait for signals */
_exit(atoi(argv[1]));
} else { /*Code executed by parent */
do {
w = waitpid(cpid,&status, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited,status=%d\n", WEXITSTATUS(status));
} else if(WIFSIGNALED(status)) {
printf("killed by signal%d\n", WTERMSIG(status));
} else if(WIFSTOPPED(status)) {
printf("stopped bysignal %d\n", WSTOPSIG(status));
} else if(WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status)&& !WIFSIGNALED(status));
exit(EXIT_SUCCESS);
}
}
6进程原语:wait()和waitpid()函数