wait函数
头文件
1 2
| #include <sys/types.h> #include <sys/wait.h>
|
函数原型:
1
| pid_t wait(int *status);
|
函数作用:
阻塞并等待子进程退出
回收子进程残留资源
获取子进程结束状态(退出原因)。
返回值:
成功:清理掉的子进程ID;
失败:-1 (没有子进程)
status参数:子进程的退出状态 – 传出参数
1 2 3 4 5 6 7
| WIFEXITED(status):为非0 → 进程正常结束
WEXITSTATUS(status):获取进程退出状态
WIFSIGNALED(status):为非0 → 进程异常终止
WTERMSIG(status):取得进程终止的信号编号。
|
wait函数练习
使用wait函数完成父进程对子进程的回收
waitpid函数
头文件
1 2
| #include <sys/types.h> #include <sys/wait.h>
|
函数原型:
1
| pid_t waitpid(pid_t pid, int *status, in options);
|
函数作用
同wait函数
函数参数
参数:
pid:
pid = -1 等待任意一个子进程。与wait等效。
pid > 0 等待其进程ID与pid相等的子进程。等待指定的pid
pid = 0 等待进程组ID与目前进程相同的任何子进程,也就是说任何和调用
waitpid()函数的进程在同一个进程组的进程。
pid < -1 等待其组ID等于pid的绝对值的任一子进程。(适用于子进程在其他组的情况)
status: 子进程的退出状态,用法同wait函数。
options:设置为WNOHANG,函数非阻塞,设置为0,函数阻塞。
调用一次wait或waitpid只能回收一个子进程
函数返回值
1 2 3 4 5
| >0:返回回收掉的子进程ID;
-1:无子进程
=0:参3为WNOHANG,且子进程正在运行。
|
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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h>
int main() { pid_t pid = fork(); if(pid<0) { perror("fork error"); return -1; } else if(pid>0) { printf("father: [%d], pid==[%d], fpid==[%d]\n", pid, getpid(),getppid()); int status; pid_t wpid = wait(&status); printf("wpid==[%d]\n", wpid); if(WIFEXITED(status)) { printf("child normal exit, status==[%d]\n", WEXITSTATUS(status)); } else if(WIFSIGNALED(status)) { printf("child killed by signal, signo==[%d]\n", WTERMSIG(status)); } } else if(pid==0) { printf("child: pid==[%d], fpid==[%d]\n", getpid(), getppid()); sleep(20); return 9; }
return 0; }
|
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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h>
int main() { pid_t pid = fork(); if(pid<0) { perror("fork error"); return -1; } else if(pid>0) { printf("father: [%d], pid==[%d], fpid==[%d]\n", pid, getpid(),getppid()); int status; while(1){ pid_t wpid = waitpid(-1,&status,WNOHANG); if(wpid > 0) { if(WIFEXITED(status)) { printf("child normal exit, status==[%d]\n", WEXITSTATUS(status)); } else if(WIFSIGNALED(status)) { printf("child killed by signal, signo==[%d]\n", WTERMSIG(status)); } }else if(wpid == 0) { }else if(wpid == -1) { printf("no child is living wpid[%d]\n",wpid); break; } }
} else if(pid==0) { printf("child: pid==[%d], fpid==[%d]\n", getpid(), getppid()); sleep(2); return 9; }
return 0; }
|
sleep函数