一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的PCB还保留着,内核在其中保存了一些信息:如果是正常终止则保存着退出状态,如果是异常终止则保存着导致该进程终止的信号是哪个。
孤儿进程
父进程先于子进程结束,则子进程成为孤儿进程,子进程的父进程成为init进程,称为init进程领养孤儿进程。
僵尸进程
进程终止,父进程尚未回收,子进程残留资源(PCB)存放于内核中,变成僵尸(Zombie)进程。【注意】:僵尸进程是不能使用kill命令清除掉的。因为kill命令只是用来终止进程的,而僵尸进程已经终止。可以杀死父进程清除掉僵尸进程;
wait
父进程调用wait函数可以回收子进程终止信息。该函数有三个功能:
1 阻塞等待子进程退出;
2 回收子进程残留资源;
3 获取子进程结束状态(退出原因);
- SYNOPSIS
#include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status);
- DESCRIPTION
status
:当进程终止时,操作系统的隐式回收机制会:1.关闭所有文件描述符 2. 释放用户空间分配的内存。内核的PCB仍存在。其中保存该进程的退出状态。(正常终止→退出值;异常终止→终止信号)。可使用wait函数传出参数status来保存进程的退出状态。借助宏函数来进一步判断进程终止的具体原因。宏函数可分为如下三组:WIFEXITED(status)
:为非0表示进程正常结束;
WEXITSTATUS(status)
:如上宏为真,使用此宏 获取进程退出状态 (exit的参数);WIFSIGNALED(status)
:为非0表示进程异常终止;
WTERMSIG(status)
:如上宏为真,使用此宏取得使进程终止的那个信号的编号;WIFSTOPPED(status)
:为非0表示进程处于暂停状态;
WSTOPSIG(status)
:如上宏为真,使用此宏取得使进程暂停的那个信号的编号;
WIFCONTINUED(status)
:为真表示进程暂停后已经继续运行;
- RETURN VALUE
成功:清理掉的子进程ID;失败:-1 (没有子进程)。 - 示例:
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/wait.h> int main(void) { pid_t pid, wpid; int status; pid = fork(); if(pid == -1){ perror("fork error"); exit(1); } else if(pid == 0){ printf("I'm child, pid = %d\n", getpid()); sleep(1); exit(10); } else { wpid = wait(&status); if(WIFEXITED(status)){ printf("I'm parent, The child %d exit normally\n", wpid); printf("return value:%d\n", WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { printf("The child exit abnormally, killed by signal %d\n", WTERMSIG(status)); } else { printf("other...\n"); } } return 0; }
- 注意:
一次 wait 调用只能清理一个子进程,清理多个子进程应使用循环。
waitpid
作用同wait,但可指定 pid 进程清理,可以不阻塞。
-
SYNOPSIS
#include <sys/types.h> #include <sys/wait.h> pid_t waitpid(pid_t pid, int *status, int options);
-
DESCRIPTION
pid
:> 0
: 回收指定ID的子进程;-1
: 回收任意子进程(相当于wait);0
: 回收和当前调用waitpid一个组的所有子进程;< -1
: 回收指定进程组内的任意子进程;
status
:参考wait;options
:- WNOHANG:非阻塞;
- 0:阻塞;
-
RETURN VALUE
成功:返回清理掉的子进程ID;失败:-1(无子进程);当参数 options 为 WNOHANG 时,返回0表示子进程正在运行; -
示例:
#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> int main(int argc, char *argv[]) { int n = 5, idx; pid_t pid, wpid; int status; for (idx=0; idx<n; idx++) { pid = fork(); if (pid == -1) { perror("fork"); } else if (pid == 0) { break; } } if (idx == n) { sleep(n); printf("I'm parent, pid = %d\n", getpid()); do { wpid = waitpid(-1, &status, WNOHANG); if (wpid > 0) { idx--; } } while(idx>0); } else { sleep(idx); printf("I'm the %d child, pid = %u\n", idx+1, getpid()); } return 0; }
-
注意:
一次 waitpid 调用只能清理一个子进程,清理多个子进程应使用循环。