孤儿进程:父进程先于子进程结束,则子进程成为孤儿进程,子进程的父进程成为init进程,init进程的pid为1
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
pid = fork();
if (pid == 0)
{
while (1)
{
printf("I am child, my parent pid = %d\n", getppid());
sleep(1);
}
} else if (pid > 0)
{
printf("I am parent, my pid is = %d\n", getpid());
sleep(9);
printf("------------parent going to die------------\n");
} else
{
perror("fork");
return 1;
}
return 0;
}
僵尸进程:子进程终止,父进程尚未回收,子进程残留资源(PCB)存放于内核中,变成僵尸(Zombie)进程。
特别注意,僵尸进程是不能使用kill命令清除掉的。因为kill命令只是用来终止进程的,而僵尸进程已经终止。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid, wpid;
pid = fork();
if (pid == 0)
{
printf("---child, my parent= %d, going to sleep 10s\n", getppid());
sleep(10);
printf("-------------child die--------------\n");
} else if (pid > 0)
{
while (1)
{
printf("I am parent, pid = %d, myson = %d\n", getpid(), pid);
sleep(1);
}
} else
{
perror("fork");
return 1;
}
return 0;
}
wait函数
pid_t wait(int *status);
作用:
① 阻塞等待子进程退出
② 回收子进程残留资源
③ 获取子进程结束状态(退出原因)
可使用wait函数传出参数status来保存进程的退出状态。
宏函数可分为如下三组:
1. WIFEXITED(status) 为非0 → 进程正常结束
WEXITSTATUS(status) 如上宏为真,使用此宏 → 获取进程退出状态 (exit的参数)
2. WIFSIGNALED(status) 为非0 → 进程异常终止
WTERMSIG(status) 如上宏为真,使用此宏 → 取得使进程终止的那个信号的编号。
3. WIFSTOPPED(status) 为非0 → 进程处于暂停状态
WSTOPSIG(status) 如上宏为真,使用此宏 → 取得使进程暂停的那个信号的编号。
WIFCONTINUED(status) 为真 → 进程暂停后已经继续运行
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid,wpid;
int status;
pid=fork();
if(pid == 0)
{
execl("abnor","abnor",NULL);
printf("i am child ,my parent=%d,going to sleep 3s\n",getppid());
sleep(60);
printf("-----child die -----\n");
return 100;
//exit(100);
}else if(pid >0)
{
wpid = wait(&status);//阻塞等待回收子进程,不会往下执行
if (wpid <0)
{
perror("wait error");
exit(1);
}
if (WIFEXITED(status))//正常退出
{
printf("child is return die values=%d \n",WEXITSTATUS(status));
}
if (WIFSIGNALED(status))//异常退出
{
printf("child killed by %d\n",WTERMSIG(status));
}
while(1){
printf("i m parent,pid=%d,myson=%d\n",getpid(),pid);
sleep(1);
}
}else
{
perror("fork error");
return 1;
}
return 0;
}
waitpid函数
作用同wait,但可指定pid进程清理,可以不阻塞。
pid_t waitpid(pid_t pid, int *status, in options); 成功:返回清理掉的子进程ID;失败:-1(无子进程)
特殊参数和返回情况:
参数pid:
0 回收指定ID的子进程
-1 回收任意子进程(相当于wait)
0 回收和当前调用waitpid一个组的所有子进程
< -1 回收指定进程组内的任意子进程
注意:一次wait或waitpid调用只能清理一个子进程,清理多个子进程应使用循环。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid;
int i=0;
int flag;
int wpid;
int status;
for(i=0;i<3;i++)
{
pid = fork();
if(pid == -1)
{
perror("fork error");
exit(1);
}else if (pid == 0)
{
break;
}
}
if(i == 0)
{
execlp("/bin/ps","ps","aux",NULL);
}
else if (i == 1)
{
execl("./hello","hello",NULL);
}
else if(i== 2)
{
execl("./abnor","abnor",NULL);
}
else
{
int n=i;
do
{
wpid = waitpid(-1,&status,WNOHANG);//WNOHANG 表示不阻塞
if (wpid > 0)
{
n--;
}
}while(n>0);
while(1)
{
printf("i am god \n");
sleep(1);
}
}
return 0;
}