进程waitpid()的用法
2016-06-19 08:28
381 查看
代码分析:
/* waitpid.c */ #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { pid_t pc, pr; pc = fork(); if( pc < 0 ) { printf("Error fork\n"); exit(1); } else if( pc == 0 ) /* 子进程 */ { /* 子进程暂停5s */ sleep(5); /* 子进程正常退出 */ exit(0); } else /* 父进程 */ { /* 循环测试子进程是否退出 */ do { /* 调用waitpid,且父进程不阻塞 */ pr = waitpid(pc, NULL, WNOHANG); /* 若子进程还未退出,则父进程暂停1s */ if( pr == 0 ) { printf("The child process has not exited\n"); sleep(1); } }while( pr == 0 ); /* 若发现子进程退出,打印出相应情况 */ if( pr == pc ) { printf("Get child exit code: %d\n",pr); } else { printf("Some error occured.\n"); } } }
相关文章推荐
- Failed to download samples index, please check your connection and try again
- 踩坑[INSTALL_FAILED_PERMISSION_LEVEL_DOWNGRADE]
- Develop -- Training(十四) -- 打印内容
- 关于PCA简单入门介绍
- 80老翁谈人生(5):人工智能与高考录取流程
- sublime package control 出现There are no packages available for installation
- 使用AIDL实现IPC通信之——实现服务端主动发送数据给客户端
- HDU 2925 Musical Chairs(约瑟夫环问题)
- 使用AIDL实现IPC通信之——简单调用远程服务的方法
- Loadrunner在场景中添加多个负载机报错:Action.c(38): Error -26488: Could not obtain information about submitted解决方法
- AIDL介绍和实例讲解
- IPC之AIDL简析
- Paint设置过滤器更变图片整体颜色效果
- ORA-38104: 无法更新 ON 子句中引用的列: "A"."MAINID"
- scp或ssh登录时遇到Host key verification failed问题的解决方法
- error CS0117: `System.IO.File' does not contain a definition for `WriteAllBytes'
- 配置Mail邮件发送
- 172. Factorial Trailing Zeroes
- 关于启动安卓应用程序时报 Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define 错误
- 使用Kali2搭建Tor隐藏服务