进程退出
#include <stdlib.h>
void exit(int status);
#include <unistd.h>
void _exit(int status);
参数:status参数,是进程退出时的一个状态信息。父进程回收子进程资源的时候可以获取到。
标准C库的exit()函数在底层会调用linux中的_exit()函数
测试代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
printf("hello\n");
printf("world");
// exit(0);
_exit(0);
return 0;
}
由于/n换行符可以刷新缓冲区,因此hello在两种退出方式中都可以被打印出来
对于第二行的world,我们调用printf函数写入了缓冲区,但由于_exit()操作时不会对缓冲区进行刷新,world停留在缓冲区不会显示在屏幕上
因此退出时我们常用C库的exit()函数
孤儿进程
示例代码
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() {
// 创建子进程
pid_t pid = fork();
// 判断是父进程还是子进程
if(pid > 0) {
printf("parent process, pid : %d, ppid : %d\n", getpid(), getppid());
} else if(pid == 0) {
sleep(1);
// 当前是子进程
printf("child process, pid : %d, ppid : %d\n", getpid(),getppid());
}
// for循环
for(int i = 0; i < 3; i++) {
printf("i : %d , pid : %d\n", i , getpid());
}
return 0;
}
在本例中,子进程先进行了一个一秒钟的sleep,在这期间父进程已经执行完返回了,因此子进程变为孤儿进程被init接管直到结束,init负责释放子进程的相关控制信息
3.僵尸进程
以下为僵尸进程的示例代码
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main() {
// 创建子进程
pid_t pid = fork();
// 判断是父进程还是子进程
if(pid > 0) {
while(1) {
printf("parent process, pid : %d, ppid : %d\n", getpid(), getppid());
sleep(1);
}
} else if(pid == 0) {
// 当前是子进程
printf("child process, pid : %d, ppid : %d\n", getpid(),getppid());
}
// for循环
for(int i = 0; i < 3; i++) {
printf("i : %d , pid : %d\n", i , getpid());
}
return 0;
}
上述代码中,子进程很快地结束了,但是父进程一直在循环打印没空理他,子进程变为了一个僵尸进程
4.进程回收
4.1wait函数
功能:等待任意一个子进程结束,如果任意一个子进程结束了,此函数会回收子进程的资源。
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *wstatus);
参数:int *wstatus
进程退出时的状态信息,传入的是一个int类型的地址,传出参数。
返回值:
- 成功:返回被回收的子进程的id
- 失败:-1 (所有的子进程都结束,调用函数失败)
调用wait函数的进程会被挂起(阻塞),直到它的一个子进程退出或者收到一个不能被忽略的信号时才被唤醒(相当于继续往下执行)
如果没有子进程了,函数立刻返回,返回-1;如果子进程都已经结束了,也会立即返回,返回-1.
wait(NULL)是一个系统调用函数,它的作用是等待任何子进程结束并回收其资源
示例代码
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
// 有一个父进程,创建5个子进程(兄弟)
pid_t pid;
// 创建5个子进程
for(int i = 0; i < 5; i++) {
flag = fork();
if(falg == 0) {//让子进程不执行fork防止生成孙子进程
break;
}
}
if(pid > 0) {
// 父进程
while(1) {
printf("parent, pid = %d\n", getpid());
// int ret = wait(NULL);
int st;
int ret = wait(&st);
if(ret == -1) {//所有子进程都已释放或者调用失败
break;
}
if(WIFEXITED(st)) {
// 是不是正常退出
printf("退出的状态码:%d\n", WEXITSTATUS(st));
}
if(WIFSIGNALED(st)) {
// 是不是异常终止
printf("被哪个信号干掉了:%d\n", WTERMSIG(st));
}
printf("child die, pid = %d\n", ret);
sleep(1);
}
} else if (pid == 0){
// 子进程
while(1) {
printf("child, pid = %d\n",getpid());
sleep(1);
}
exit(0);
}
return 0; // exit(0)
}
值得注意的是这两行
int st;
int ret = wait(&st);
wait函数需要传入一个int类型的地址,并且他是一个传出参数
明确传出参数的概念:
就是你给他传入一个空的坑,然后函数去给你填数据,所以随便int一个st就行,执行完wait他给你把st的信息填满,然后你再去使用
4.2 waitpid函数
功能:回收指定进程号的子进程,可以设置是否阻塞。
#include <sys/types.h>
#include <sys/wait.h>
pid_t waitpid(pid_t pid, int *wstatus, int options);
参数:
- pid:
pid > 0 : 某个子进程的pid
pid = 0 : 回收当前进程组的任意一个子进程
pid = -1 : 回收所有的子进程,相当于 wait() (最常用)
pid < -1 : 某个进程组的组id的绝对值,回收指定进程组中的子进程
比如父进程在组1子进程在组3,设置为-3
- options:设置阻塞或者非阻塞
0 : 阻塞
WNOHANG : 非阻塞
- 返回值:
> 0 : 返回子进程的id
= 0 : options=WNOHANG时, 表示还有子进程活着
= -1 :错误,或者没有子进程了
测试代码
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
// 有一个父进程,创建5个子进程(兄弟)
pid_t pid;
// 创建5个子进程
for(int i = 0; i < 5; i++) {
pid = fork();
if(pid == 0) {
break;
}
}
if(pid > 0) {
// 父进程
while(1) {
printf("parent, pid = %d\n", getpid());
sleep(1);
int st;
// int ret = waitpid(-1, &st, 0);
int ret = waitpid(-1, &st, WNOHANG);
if(ret == -1) {
break;
} else if(ret == 0) {
// 说明还有子进程存在
continue;
} else if(ret > 0) {
if(WIFEXITED(st)) {
// 是不是正常退出
printf("退出的状态码:%d\n", WEXITSTATUS(st));
}
if(WIFSIGNALED(st)) {
// 是不是异常终止
printf("被哪个信号干掉了:%d\n", WTERMSIG(st));
}
printf("child die, pid = %d\n", ret);
}
}
} else if (pid == 0){
// 子进程
while(1) {
printf("child, pid = %d\n",getpid());
sleep(1);
}
exit(0);
}
return 0;
}
这里注意:
如果设置成WNOHANG非阻塞模式,while会一直更新ret的值,所以我们需要两个else if一直判断
ret的值是否变化并执行相对应的操作。
wait函数会一直等,但WNOHANG的waitpid函数在有子进程时ret==1,类似一种动态的等待