阻塞和非阻塞等待
来源:互联网 发布:java定义数组并赋值 编辑:程序博客网 时间:2024/06/06 01:53
阻塞式等待
#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/wait.h>int main(){ pid_t pid; pid=fork(); if(pid<0) { printf("fork error\n"); return 1; } else if(pid==0) { printf("child id running,pid is:%d\n",getpid()); sleep (5); exit(2); } else { int status=0; pid_t ret=waitpid(-1,&status,0); printf("test wait\n"); if(WIFEXITED(status)&& ret==pid) { printf("wait 5s,child return code is: %d\n",WEXITSTATUS(status)); } else
{ printf("wait child failed\n"); return 1; }}return 0;
}
非阻塞式等待:
#include<stdio.h>#include<unistd.h>#include<stdlib.h>#include<sys/wait.h>int main(){ pid_t pid; pid=fork(); if(pid<0) { printf("fork error\n"); return 1; } else if(pid==0) { printf("child id running,pid is:%d\n",getpid()); sleep (5); exit(2); } else { int status=0; pid_t ret=0; do { ret=waitpid(-1,&status,WNOHANG); if(ret==0) { printf("child is running\n"); } sleep(1); }while(ret==0); if(WIFEXITED(status)&& ret==pid) { printf("wait 5s,child return code is: %d\n",WEXITSTATUS(status)); } else { printf("wait child failed\n"); return 1; } } return 0;}
0 0
- 阻塞和非阻塞等待
- 等待队列 阻塞非阻塞
- IO 的阻塞和非阻塞一:等待队列
- 非阻塞轮询读终端和等待超时
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞
- 阻塞和非阻塞通信
- 阻塞和非阻塞通信
- 阻塞和非阻塞通信
- 阻塞和非阻塞通信
- HDU 4544 湫湫系列故事——消灭兔子 (贪心+优先队列)
- CloudVisor译稿
- 如何安装Hadoop 2.6.0完全分布式?
- HDU.5120 Intersection
- Linux的简单介绍
- 阻塞和非阻塞等待
- poj 2274 线段树+堆(优先队列)
- Java归并排序
- windows下C语言开发环境搭建
- HDU 1176 免费馅饼(DP)
- 编程的智慧
- ScrolView和RecyclerView嵌套使用的问题
- SQLi Labs Lesson1
- 过滤器应用案例分析