linux的waitpid函数
2016-12-23 16:53
141 查看
waitpid函数
作用同wait,但可指定pid进程清理,可以不阻塞。pid_t waitpid(pid_t pid, int *status, in options);
成功:返回清理掉的子进程ID;失败:-1(无子进程)
特殊参数和返回情况:
参数pid:
> 0 回收指定ID的子进程
-1 回收任意子进程(相当于wait)
0 回收和当前调用waitpid一个组的所有子进程
< -1 回收指定进程组内的任意子进程
返回0:参3为WNOHANG,且子进程正在运行。
注意:一次wait或waitpid调用只能清理一个子进程,清理多个子进程应使用循环。
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid, pid2, wpid;
int flg = 0;
pid = fork();
pid2 = fork();
if(pid == -1){
perror("fork error");
exit(1);
} else if(pid == 0){ //son
printf("I'm process child, pid = %d\n", getpid());
sleep(5);
exit(4);
} else { //parent
do {
wpid = waitpid(pid, NULL, WNOHANG);
//wpid = wait(NULL);
printf("---wpid = %d--------%d\n", wpid, flg++);
if(wpid == 0){
printf("NO child exited\n");
sleep(1);
}
} while (wpid == 0); //子进程不可回收
if(wpid == pid){ //回收了指定子进程
printf("I'm parent, I catched child process,"
"pid = %d\n", wpid);
} else {
printf("other...\n");
}
}
return 0;
}
相关文章推荐
- 模拟linux的shell---顺便复习一下fork,execlp和waitpid函数
- linux中的waitpid函数
- Linux系统进程控制编程——wait和waitpid函数
- 【Linux基础】wait和waitpid函数
- 【Linux编程】wait和waitpid函数
- Linux进程控制--wait和waitpid函数
- linux 中的进程wait()和waitpid函数,僵尸进程详解,以及利用这两个函数解决进程同步问题
- Linux系统编程_8_进程控制之fork_wait_waitpid函数
- Linux进程 -- wait/waitpid函数
- Linux下声卡通
- linux 用户行为审计
- crontab命令简介 linux定时设置 Cron实现自动任务
- 硬件全攻略--MODEM linux内猫配置 驱动软猫
- linux 无盘安装 没有光驱 无光驱 rawrite ftp安装
- 向Linux进军(X-Window配置指南)
- 大家来做linux调试日志 (tomcat jsp server 配置方法)
- 高效配置Linux代理服务器――Squid
- linux 下 squid 安装日志
- Linux系统中的入侵检测
- 在 linux 系统下安装 perl