抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

wait - 回收子进程资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*************************************************************************
> File Name: wait.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月12日 星期二 19时01分25秒
************************************************************************/

/*
* #include <sys/types.h>
* #include <sys/wait.ch>
* pid_t wait(int *wstatus);
* 功能: 等待任意一个子进程结束, 如果任意一个子进程结束了,此函数会回收子进程的资源
* 参数: int *wstatus
* 进程退出时的状态信息,传入的是一个int类型的地址, 传出参数
* 返回值:
* - 成功: 返回被回收的子进程的ID
* - 失败: -1 (所有的子进程都结束了, 调用函数失败)
* 调用wait函数的进程会被挂起(阻塞),直到它的一个子进程退出或者收到一个不能被忽略的信号时才被唤醒(相当于继续执行)
* 如果没有子进程了,函数立刻返回,返回-1:如果子进程都已经结束了,也会立即返回-1
*/


#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

int main() {

// 有一个父进程,创建5个子进程(兄弟)
pid_t pid;
for(int i = 0; i < 5; i++) {
pid = fork();
if(pid == 0) {
// 干掉子进程
break;
}
}

if(pid > 0) {
// 父进程
while(1) {
printf("i am parent, pid = %d\n", getpid());
sleep(1);
}
} else if(pid == 0) {
// 子进程
printf("child, pid = %d\n", getpid());
}

return 0;
}

5次子进程结束后父进程一直再执行就会出现在5个孤儿进程

image-20220413230612712

image-20220413230734623

结束掉父进程

image-20220413230841640

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*************************************************************************
> File Name: wait.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月12日 星期二 19时01分25秒
************************************************************************/

/*
* #include <sys/types.h>
* #include <sys/wait.ch>
* pid_t wait(int *wstatus);
* 功能: 等待任意一个子进程结束, 如果任意一个子进程结束了,此函数会回收子进程的资 源
* 参数: int *wstatus
* 进程退出时的状态信息,传入的是一个int类型的地址, 传出参数
* 返回值:
* - 成功: 返回被回收的子进程的ID
* - 失败: -1 (所有的子进程都结束了, 调用函数失败)
* 调用wait函数的进程会被挂起(阻塞),直到它的一个子进程退出或者收到一个不能被忽略 的信号时才被唤醒(相当于继续执行)
* 如果没有子进程了,函数立刻返回,返回-1:如果子进程都已经结束了,也会立即返回-1
*/


#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

int main() {

// 有一个父进程,创建5个子进程(兄弟)
pid_t pid;
for(int i = 0; i < 5; i++) {
pid = fork();
if(pid == 0) {
// 干掉子进程
break;
}
}

if(pid > 0) {
// 父进程
while(1) {
printf("i am parent, pid = %d\n", getpid());

int ret = wait(NULL);

if(ret == -1) {
break;
}

printf("child die, pid = %d\n", ret);
sleep(1);
}
} else if(pid == 0) {
// 子进程
printf("child, pid = %d\n", getpid());
sleep(1);
}

return 0;
}

退出信息相关宏函数

image-20220415112552312

正常运行结束代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*************************************************************************
> File Name: wait.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月12日 星期二 19时01分25秒
************************************************************************/

/*
* #include <sys/types.h>
* #include <sys/wait.ch>
* pid_t wait(int *wstatus);
* 功能: 等待任意一个子进程结束, 如果任意一个子进程结束了,此函数会回收子进程的资 源
* 参数: int *wstatus
* 进程退出时的状态信息,传入的是一个int类型的地址, 传出参数
* 返回值:
* - 成功: 返回被回收的子进程的ID
* - 失败: -1 (所有的子进程都结束了, 调用函数失败)
* 调用wait函数的进程会被挂起(阻塞),直到它的一个子进程退出或者收到一个不能被忽略 的信号时才被唤醒(相当于继续执行)
* 如果没有子进程了,函数立刻返回,返回-1:如果子进程都已经结束了,也会立即返回-1
*/


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

int main() {

// 有一个父进程,创建5个子进程(兄弟)
pid_t pid;
for(int i = 0; i < 5; i++) {
pid = fork();
if(pid == 0) {
// 干掉子进程
break;
}
}

if(pid > 0) {
// 父进程
while(1) {
printf("i am 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) {
// 子进程
printf("child, pid = %d\n", getpid());
sleep(1);
exit(0);
}

return 0; // exit(0)
}

image-20220415155935776

信号干掉代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*************************************************************************
> File Name: wait.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月12日 星期二 19时01分25秒
************************************************************************/

/*
* #include <sys/types.h>
* #include <sys/wait.ch>
* pid_t wait(int *wstatus);
* 功能: 等待任意一个子进程结束, 如果任意一个子进程结束了,此函数会回收子进程的资 源
* 参数: int *wstatus
* 进程退出时的状态信息,传入的是一个int类型的地址, 传出参数
* 返回值:
* - 成功: 返回被回收的子进程的ID
* - 失败: -1 (所有的子进程都结束了, 调用函数失败)
* 调用wait函数的进程会被挂起(阻塞),直到它的一个子进程退出或者收到一个不能被忽略 的信号时才被唤醒(相当于继续执行)
* 如果没有子进程了,函数立刻返回,返回-1:如果子进程都已经结束了,也会立即返回-1
*/


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

int main() {

// 有一个父进程,创建5个子进程(兄弟)
pid_t pid;
for(int i = 0; i < 5; i++) {
pid = fork();
if(pid == 0) {
// 干掉子进程
break;
}
}

if(pid > 0) {
// 父进程
while(1) {
printf("i am 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)
}

image-20220415160453399

waitpid - 回收子进程资源

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*************************************************************************
> File Name: wait_pid.c
> Author: 秃头王
> Mail: 1658339000@qq.com
> Created Time: 2022年04月15日 星期五 16时38分41秒
************************************************************************/

/*
*
* #include <sys/types.h>
* #include <sys/wait.h>
* pid_t waitpid(pid_t pid, int *wstatus, int options);
* 功能: 回收指定进程号的子进程,可以设置是否阻塞。
* 参数:
* - pid:
* PID > 0 : 回收某个指定子进程的 PID
* PID = 0 : 回收当前进程组的所有子进程
* 进程 A -> B A -> C 放到一个集合 (A - B, A - C) <- D 在把D给别人就不输入当前组 释放时候就与D没有关系了。// ajx 查看组查看进程组
* PID = -1 : 回收所有的子进程 相当于 wait() (最长用)
* PID < -1 : 某个进程组的组id的绝对值回收指定进程组中的子进程
* - options: 设置阻塞或者非阻塞
* 0 : 阻塞
* WNOHANG : 非阻塞
* - 返回值:
* > 0 : 返回子进程的id
* = 0 : options = WNOHANG, 表示还有子进程活着
* = -1 : 错误, 或者没有子进程了
*
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.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);
}
sleep(1);
}
} else if(pid == 0) {
while(1) {
printf("child , pid = %d\n", getpid());
sleep(1);
}
exit(0);
}
return 0;
}

评论