wait waitpid
2014-05-27 00:18
218 查看
当一个进程终止之后,内核向父进程发送一个SIGCHLD。默认这个信号被忽略,进程可以通过singnal()或者sigaction()系统调用来处理这个信号。父进程希望得到子进程终止的更多信息,比如返回值,甚至显式的等待这个事件的到来,这就是wait或者waitpid,它们可以做:
1)阻塞,如果子进程仍然在执行。
2)立即返回,包含子进程的终止状态,如果一个子进程终止,等待它的终止状态被获取。
3)返回错误,如果它没有子进程。
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc, intoptions);
wait会阻塞调用者,直到一个子进程终止,而waitpid有一个设置不阻塞的选项。
1. #include <sys/wait.h>
2.
3. int WIFEXITED(status); //子进程正常结束
4. int WIFSIGNALED(status); //被异常终止
5. int WIFSTOPPED(status); //被暂停
6. int WIFCONTINUED(status); //继续运行时
7.
8. int WEXITSTATUS(status); //获取正常结束时的返回值
9. int WTERMSIG(status); //获取异常终止信号
10. int WSTOPSIG(status); //获取引起子进程暂停的信号
11. int WCOREDUMP(status); //获取子进程异常终止所产生的内核文件
WIFEXITED:如果子进程正常终止,返回true。可以通过WEXITSTATUS得到参数的低8位。
WIFSIGNALED:如果是信号导致子进程不正常终止,返回true。可以通过WTERMSIG返回信号的号。
一些UNIX实现定义了WCOREDUMP宏,如果进程dump
core来响应信号。
WIFSTOPPED:如果进程被停止,返回true,通过WSTOPSIG来获得导致子进程停止的信号。
WIFCONTINUED:如果状态是由已经被continued子进程返回,返回true。
1. #include <unistd.h>
2. #include <stdio.h>
3. #include <sys/types.h>
4. #include <sys/wait.h>
5.
6. int main(void){
7. int status;
8. pid_t pid;
9.
10. if(! fork()){
11. return 1;
12. }
13.
14. pid = wait(&status);
15. if(pid == -1)
16. perror("wait");
17. printf("pid=%d\n",pid);
18.
19. if(WIFEXITED(status))
20. printf("Normal termination with exit status=%d\n",WEXITSTATUS(status));
21. if(WIFSIGNALED(status))
22. printf("Killed by signal=%d%s\n",WTERMSIG(status),WCOREDUMP(status));
23. if(WIFSTOPPED(status))
24. printf("Stopped by signal=%d\n",WSTOPSIG(status));
25. if(WIFCONTINUED(status))
26. printf("Continued\n");
27.
28. return 0;
29. }
waitpid
pid参数指定了要等待的进程id:
< -1:等待任意一个绝对值和进程group id相等的进程。
-1:等待任意一个子进程,和wait一样
0:等待和当前进程相同组的任一子进程。
> 0: 等待该进程id的子进程
option 可以通过OR连接下列选项:
WNOHANG:不阻塞,如果没有匹配的子进程也直接返回。
WUNTRACED:如果被设置,WIFSTOPPED也会被设置。允许更一般的作业控制。
WCONTINUED:如果被设置,WIFCONTINUED也被设置。和WUNTRACED一起,
1. int status;
2. pid_t pid;
3. pid = waitpid (1742, &status, WNOHANG);
4. if (pid == -1)
5. perror ("waitpid");
6. else {
7. printf ("pid=%d\n", pid);
8. if (WIFEXITED (status))
9. printf ("Normal termination with exit status=%d\n",WEXITSTATUS (status));
10. if (WIFSIGNALED (status))
11. printf ("Killed by signal=%d%s\n",WTERMSIG (status),WCOREDUMP (status) ? " (dumped core)" : "");
12. }
1)阻塞,如果子进程仍然在执行。
2)立即返回,包含子进程的终止状态,如果一个子进程终止,等待它的终止状态被获取。
3)返回错误,如果它没有子进程。
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int *statloc, intoptions);
wait会阻塞调用者,直到一个子进程终止,而waitpid有一个设置不阻塞的选项。
1. #include <sys/wait.h>
2.
3. int WIFEXITED(status); //子进程正常结束
4. int WIFSIGNALED(status); //被异常终止
5. int WIFSTOPPED(status); //被暂停
6. int WIFCONTINUED(status); //继续运行时
7.
8. int WEXITSTATUS(status); //获取正常结束时的返回值
9. int WTERMSIG(status); //获取异常终止信号
10. int WSTOPSIG(status); //获取引起子进程暂停的信号
11. int WCOREDUMP(status); //获取子进程异常终止所产生的内核文件
WIFEXITED:如果子进程正常终止,返回true。可以通过WEXITSTATUS得到参数的低8位。
WIFSIGNALED:如果是信号导致子进程不正常终止,返回true。可以通过WTERMSIG返回信号的号。
一些UNIX实现定义了WCOREDUMP宏,如果进程dump
core来响应信号。
WIFSTOPPED:如果进程被停止,返回true,通过WSTOPSIG来获得导致子进程停止的信号。
WIFCONTINUED:如果状态是由已经被continued子进程返回,返回true。
1. #include <unistd.h>
2. #include <stdio.h>
3. #include <sys/types.h>
4. #include <sys/wait.h>
5.
6. int main(void){
7. int status;
8. pid_t pid;
9.
10. if(! fork()){
11. return 1;
12. }
13.
14. pid = wait(&status);
15. if(pid == -1)
16. perror("wait");
17. printf("pid=%d\n",pid);
18.
19. if(WIFEXITED(status))
20. printf("Normal termination with exit status=%d\n",WEXITSTATUS(status));
21. if(WIFSIGNALED(status))
22. printf("Killed by signal=%d%s\n",WTERMSIG(status),WCOREDUMP(status));
23. if(WIFSTOPPED(status))
24. printf("Stopped by signal=%d\n",WSTOPSIG(status));
25. if(WIFCONTINUED(status))
26. printf("Continued\n");
27.
28. return 0;
29. }
waitpid
pid参数指定了要等待的进程id:
< -1:等待任意一个绝对值和进程group id相等的进程。
-1:等待任意一个子进程,和wait一样
0:等待和当前进程相同组的任一子进程。
> 0: 等待该进程id的子进程
option 可以通过OR连接下列选项:
WNOHANG:不阻塞,如果没有匹配的子进程也直接返回。
WUNTRACED:如果被设置,WIFSTOPPED也会被设置。允许更一般的作业控制。
WCONTINUED:如果被设置,WIFCONTINUED也被设置。和WUNTRACED一起,
1. int status;
2. pid_t pid;
3. pid = waitpid (1742, &status, WNOHANG);
4. if (pid == -1)
5. perror ("waitpid");
6. else {
7. printf ("pid=%d\n", pid);
8. if (WIFEXITED (status))
9. printf ("Normal termination with exit status=%d\n",WEXITSTATUS (status));
10. if (WIFSIGNALED (status))
11. printf ("Killed by signal=%d%s\n",WTERMSIG (status),WCOREDUMP (status) ? " (dumped core)" : "");
12. }
相关文章推荐
- linuc之waitpid
- wait和 wait_pid函数用法
- Linux 父进程如何获取子进程返回值?parent get child process return value,fork,wait,wait_pid
- waitpid(or wait)和SIGCHILD的关系 http://blog.csdn.net/liuxingen/article/details/38350347
- the differences between wait and waitpid
- wait waitpid
- <转载>Wait and Waitpid
- Using waitpid to Reap Zombie Children
- wait_pid代码分析
- mysql:ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
- PID为0的系统空闲进程连接状态为TIME_WAIT
- 怎样kill掉TCP连接状态是TIME_WAIT的而且找不到PID
- linux waitpid wait (转)
- introduce to waitpid() in linux
- wait and waitpid
- 进程函数 fork waitpid
- centos7 mysql: mariadb-wait-ready $MAINPID (code=exited, status=1/FAILURE)
- wait waitpid
- 32-wait大变身之waitpid
- wait waitpid WNOHANG 僵尸进程