C语言 正在使用的 waitpid() 示例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21248840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example of waitpid() in use?
提问by user3063864
I know that waitpid()is used to wait for a process to finish, but how would one use it exactly?
我知道这waitpid()是用来等待一个进程完成,但究竟如何使用它呢?
Here what I want to do is, create two children and wait for the first child to finish, then kill the second child before exiting.
这里我想要做的是,创建两个孩子并等待第一个孩子完成,然后在退出之前杀死第二个孩子。
//Create two children
pid_t child1;
pid_t child2;
child1 = fork();
//wait for child1 to finish, then kill child2
waitpid() ... child1 {
kill(child2) }
回答by mf_starboi_8041
Syntax of waitpid():
的语法waitpid():
pid_t waitpid(pid_t pid, int *status, int options);
The value of pidcan be:
的值pid可以是:
- < -1: Wait for any child process whose process group ID is equal to the absolute value of
pid. - -1: Wait for any child process.
- 0: Wait for any child process whose process group ID is equal to that of the calling process.
- > 0: Wait for the child whose process ID is equal to the value of
pid.
- < -1:等待进程组 ID 等于 的绝对值的任何子进程
pid。 - -1:等待任何子进程。
- 0: 等待进程组 ID 等于调用进程组 ID 的任何子进程。
- > 0:等待进程 ID 等于 的值的子进程
pid。
The value of options is an OR of zero or more of the following constants:
options 的值是以下零个或多个常量的 OR:
WNOHANG: Return immediately if no child has exited.WUNTRACED: Also return if a child has stopped. Status for traced children which have stopped is provided even if this option is not specified.WCONTINUED: Also return if a stopped child has been resumed by delivery ofSIGCONT.
WNOHANG: 如果没有孩子退出,立即返回。WUNTRACED: 如果孩子已经停止,也返回。即使未指定此选项,也会提供已停止的跟踪子项的状态。WCONTINUED:如果停止的孩子已通过交付恢复,也返回SIGCONT。
For more help, use man waitpid.
如需更多帮助,请使用man waitpid.
回答by KARTHIK BHAT
The syntax is
语法是
pid_t waitpid(pid_t pid, int *statusPtr, int options);
1.where pid is the process of the child it should wait.
1.其中pid是它应该等待的子进程。
2.statusPtr is a pointer to the location where status information for the terminating process is to be stored.
2.statusPtr 是指向要存储终止进程的状态信息的位置的指针。
3.specifies optional actions for the waitpid function. Either of the following option flags may be specified, or they can be combined with a bitwise inclusive OR operator:
3.指定waitpid函数的可选动作。可以指定以下任一选项标志,或者它们可以与按位包含 OR 运算符组合:
WNOHANG WUNTRACED WCONTINUED
WNOHANG WUNTRACED W(续)
If successful, waitpid returns the process ID of the terminated process whose status was reported. If unsuccessful, a -1 is returned.
如果成功,waitpid 返回状态已报告的已终止进程的进程 ID。如果不成功,则返回 -1。
benifits over wait
等待的好处
1.Waitpid can used when you have more than one child for the process and you want to wait for particular child to get its execution done before parent resumes
1.Waitpid 当你有多个子进程并且你想在父进程恢复之前等待特定子进程完成它的执行时可以使用Waitpid
2.waitpid supports job control
2.waitpid 支持作业控制
3.it supports non blocking of the parent process
3.支持父进程的非阻塞
回答by Alex Stolt
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main (){
int pid;
int status;
printf("Parent: %d\n", getpid());
pid = fork();
if (pid == 0){
printf("Child %d\n", getpid());
sleep(2);
exit(EXIT_SUCCESS);
}
//Comment from here to...
//Parent waits process pid (child)
waitpid(pid, &status, 0);
//Option is 0 since I check it later
if (WIFSIGNALED(status)){
printf("Error\n");
}
else if (WEXITSTATUS(status)){
printf("Exited Normally\n");
}
//To Here and see the difference
printf("Parent: %d\n", getpid());
return 0;
}

