linux 信号量
2015-06-25 16:23
246 查看
1.wait 与waitpid的关系
2.status 与 option
函数的返回值
wait() 回收子孙进程,而非仅仅儿子进程
wait() and waitpid() The wait() system call suspends execution of the calling process until one of its children terminates. The call wait(&status) is equivalent to: waitpid(-1, &status, 0);
2.status 与 option
The waitpid() system call suspends execution of the calling process until a child specified by pid argument has changed state. By default, waitpid() waits only for terminated children, but this behavior is mod‐ ifiable via the options argument, as described below. The value of pid can be: < -1 meaning wait for any child process whose process group ID is equal to the absolute value of pid. -1 meaning wait for any child process. 0 meaning wait for any child process whose process group ID is equal to that of the calling process. > 0 meaning wait for the child whose process ID is equal to the value of pid. The value of options is an OR of zero or more of the following con‐ stants: WNOHANG return immediately if no child has exited. WUNTRACED also return if a child has stopped (but not traced via ptrace(2)). Status for traced children which have stopped is provided even if this option is not specified. WCONTINUED (since Linux 2.6.10) also return if a stopped child has been resumed by delivery of SIGCONT.
函数的返回值
RETURN VALUE wait(): on success, returns the process ID of the terminated child; on error, -1 is returned. waitpid(): on success, returns the process ID of the child whose state has changed; if WNO‐ HANG was specified and one or more child(ren) specified by pid exist, but have not yet changed state, then 0 is returned. On error, -1 is returned. waitid(): returns 0 on success or if WNOHANG was specified and no child(ren) specified by id has yet changed state; on error, -1 is returned. Each of these calls sets errno to an appropriate value in the case of an error. ERRORS ECHILD (for wait()) The calling process does not have any unwaited-for children. ECHILD (for waitpid() or waitid()) The process specified by pid (waitpid()) or idtype and id (waitid()) does not exist or is not a child of the calling process. (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.) EINTR WNOHANG was not set and an unblocked signal or a SIGCHLD was caught; see signal(7). EINVAL The options argument was invalid.
#include <stdio.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <stdlib.h> int main() { int fd[2]; pipe(fd); int status; int pid=fork(); if(pid==0){ printf("in child,sleep 4s \n"); sleep(4); printf("exit...\n"); exit(-4);//the same to return(-50) }else if(pid<0){ printf("fork fail!\n"); } printf("wait for child!\n"); fflush(stdout); printf("return code is child pid[%d]\n",wait(&status)); if(WIFEXITED(status)){ printf("return normally!\n"); printf("return value is[%d]\n",WEXITSTATUS(status)); } return 0; }
wait() 回收子孙进程,而非仅仅儿子进程
#include <stdio.h> #include <signal.h> #include <string.h> #include <sys/types.h> #include <sys/wait.h> #include <stdlib.h> int main() { int pid,i; if(fork()==0) { for(i=0; i<1000; i++) { pid=fork(); if(pid==0) { exit(0); } } } printf("wait for 20s\n"); fflush(stdout); sleep(20); if((pid=wait(NULL))>0) { printf("clooect [%d]\n",pid); while(pid>0) { pid=wait(NULL); printf("clooect [%d]\n",pid); fflush(stdout); } printf("wait for 50s again\n"); fflush(stdout); sleep(50); return 0; } }
相关文章推荐
- 使用监控宝监控CentOS主机
- Linux下php-cgi占内存100%电脑变的很卡的解决方法
- 在windows下面调试linux代码
- Linux如何禁止系统内核Kernel自动升级避免不必要的麻烦
- 自助Linux之问题诊断工具strace
- centos下jdk安装
- CentOS配置网易163 中科大 yum源
- DayDayUP_Linux运维学习_Windowns下的端口查询
- Linux 安装android
- centos下 No package 'glib-2.0' found的解决办法
- 给VM虚拟机中的CentOS Linux系统挂载U盘的方法图文教程
- Linux vim查找与替换操作及快捷键
- linux下的c/c++调试器gdb
- Linux vim光标移动快捷键
- linux搭建svn服务
- Linux命令之grep命令使用介绍
- Linux下优秀的音频编辑软件
- Linux命令之cut命令使用介绍
- Linux命令之awk命令使用介绍
- linux下安装PHP扩展