C 正在使用的waitpid()示例?

C 正在使用的waitpid()示例?,c,fork,parent-child,waitpid,C,Fork,Parent Child,Waitpid,我知道waitpid()用于等待进程完成,但如何准确地使用它呢 在这里,我想做的是,创建两个子对象,等待第一个子对象完成,然后在退出之前杀死第二个子对象 //Create two children pid_t child1; pid_t child2; child1 = fork(); //wait for child1 to finish, then kill child2 waitpid() ... child1 { kill(child2) } waitpid()的语法: pid的值可

我知道
waitpid()
用于等待进程完成,但如何准确地使用它呢

在这里,我想做的是,创建两个子对象,等待第一个子对象完成,然后在退出之前杀死第二个子对象

//Create two children
pid_t child1;
pid_t child2;
child1 = fork();

//wait for child1 to finish, then kill child2
waitpid() ... child1 {
kill(child2) }
waitpid()
的语法:

pid
的值可以是:

  • <-1:等待进程组ID等于
    pid
    绝对值的任何子进程
  • -1:等待任何子进程
  • 0:等待进程组ID等于调用进程ID的任何子进程
  • >0:等待进程ID等于
    pid
    值的子进程
选项的值是以下常量中的一个或多个:

  • WNOHANG
    :如果没有孩子退出,请立即返回
  • WUNTRACED
    :如果子级已停止,也返回。即使未指定此选项,也会提供已停止的跟踪子项的状态
  • WCONTINUED
    :如果通过交付
    SIGCONT
    恢复了已停止的子项,也将返回
有关更多帮助,请使用
manwaitpid

语法为

pid_t waitpid(pid_t pid, int *statusPtr, int options);
1.其中pid是子进程,它应该等待

2.statusPtr是指向存储终止进程状态信息的位置的指针

3.指定waitpid函数的可选操作。可以指定以下任一选项标志,也可以将其与按位包含或运算符组合使用:

WNOHANG 温特拉奇 w继续

如果成功,waitpid将返回其状态已报告的已终止进程的进程ID。如果不成功,则返回-1

等待的好处

1.当您有多个子进程,并且希望在父进程恢复之前等待特定子进程完成执行时,可以使用Waitpid

2.waitpid支持作业控制

3.支持父进程的非阻塞

\include
#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;
}
#包括 #包括 #包括 #包括 int main(){ int-pid; 智力状态; printf(“父项:%d\n”,getpid()); pid=fork(); 如果(pid==0){ printf(“子%d\n”,getpid()); 睡眠(2); 退出(退出成功); } //评论从这里到。。。 //父进程pid(子进程) waitpid(pid和status,0); //选项为0,因为我稍后会检查它 if(WIFSIGNALED(状态)){ printf(“错误\n”); } else if(WEXITSTATUS(状态)){ printf(“正常退出\n”); } //到这里来看看区别 printf(“父项:%d\n”,getpid()); 返回0; }
我注意到,我的Linux系统上的
wait(2)
手册页包含了一个如何使用
waitpid()
系统调用的实际示例。我认为,通过搜索
[c]waitpid
,您可以很容易地找到答案;搜索
[c][waitpid]
就不那么容易了。我在第一次搜索中回答了48个问题,在第二次搜索中回答了5个问题,我绝不是唯一一个回答了这些问题的人。通过示例提供更详细的信息。从这里对这个问题的两个回答中,我个人仍然无法理解如何有效地使用waitpid。您是否阅读了关于1)waitpid的返回值
waitpid
,2)何时使用
WEXITSTATE
,的文档,3)对于
WEXITSTATUS
,最正常的退出会返回什么?您可以解释一下您的答案吗?非常非常糟糕的示例、错误处理和在EINTR上醒来(经常发生)等。未处理。
#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;
}