如何使父进程等待子进程完成?

时间:2020-04-27 19:10:33

标签: c ubuntu fork pid waitpid

我有一个分配,该赋值使我将此代码转换为使父进程等待所有子进程完成的代码。 PS:第一个代码有4个过程,需要使用waitpid来解决。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(){
  pid_t p = fork();
  pid_t k = fork();

  if(p>0){
     printf("p=%d: PID = %d\n", p, getpid());
     sleep(45);
     exit(0);
  }
  else if(p==0){
     printf("p=%d: PID = %d\n", p, getpid());
     exit(0);
  }
  else if(p<0){
     printf("ERRO! p=%d\n", p);
     exit(p);
  }
}

我已经尝试过了,但是我认为这仅适用于1个子进程,不适用于许多子进程。

int main(){

    pid_t p = fork(); 
    pid_t k = fork(); 

    if(p<0){
        printf("fodeu");
        exit(p);
    }
    else if(p==0){
        printf("");
        exit(0);
    }
    else{
        for(i=0;i<4;i++){
            int returnstatus; 
            waitpid(p,&returnstatus,0);

            if(returnstatus == 0){
                printf("o processo filho correu normalmente");
            }
            else if(returnstatus == 1){
                printf("o processo filho ardeu");
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是一种实现方法;还有很多其他人。

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

static void wait_for_kids(void);

int main(void)
{
    pid_t p = fork();
    pid_t k = fork();

    if (p > 0)
    {
        printf("p=%d: PID = %d\n", p, getpid());
        sleep(5);
        wait_for_kids();
        printf("%d: p = %5d, k = %5d - exiting\n", getpid(), p, k);
        exit(0);
    }
    else if (p == 0)
    {
        printf("p=%d: PID = %d\n", p, getpid());
        wait_for_kids();
        printf("%d: p = %5d, k = %5d - exiting\n", getpid(), p, k);
        exit(0);
    }
    else
    {
        printf("ERRO! p=%d\n", p);
        wait_for_kids();
        printf("%d: p = %5d, k = %5d - exiting\n", getpid(), p, k);
        exit(p);
    }
    /*NOTREACHED*/
}

static void wait_for_kids(void)
{
    int corpse;
    int status;
    int pid = getpid();

    while ((corpse = waitpid(0, &status, 0)) > 0)
        printf("%d: child %d exited with status 0x%.4X\n", pid, corpse, status);
}

示例输出:

p=43445: PID = 43444
p=43445: PID = 43446
p=0: PID = 43445
p=0: PID = 43447
43447: p =     0, k =     0 - exiting
43445: child 43447 exited with status 0x0000
43445: p =     0, k = 43447 - exiting
43446: p = 43445, k =     0 - exiting
43444: child 43445 exited with status 0x0000
43444: child 43446 exited with status 0x0000
43444: p = 43445, k = 43446 - exiting

答案 1 :(得分:0)

这不会完成您的任务,但我希望这对您有所帮助 你去。作业似乎是fork()的一个谜,您的 老师很有品味:-)

fork()是不同的。它返回两次。

  • 在父级中,它返回创建的进程的进程ID。
  • 在子级中返回0;在子级中返回0。流程始终可以使用getpid()
  • 确定其PID

实际上,任务不是很好。通常使用`fork() 永远不要让任何分支逃逸到封闭的代码中以避免完成 废话。像这样

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
    pid_t pid = fork();
    if (pid == 0 /*child*/) {
        printf("PID %d (child) doing work\n", pid);
        sleep(5);
        exit(0); // don't let it continue (leak) into parent code
    }
    else if (pid > 0 /*parent*/) {
        int status;
        pid_t terminated;

        printf("PID %d (parent) waiting for child PID %d\n", getpid(), pid);

        terminated = waitpid(pid, &status, 0);
        if (terminated == -1) {
            perror("waitpid");
            exit(1);
        }

        if (WIFEXITED(status))
            printf("child exited normally with status %d\n", WEXITSTATUS(status));
        else
            printf("hm. child died otherwise. see 'man waidpid' for more\n");
    }

    return 0;
}

牢记这一点,看看这两条无辜的表情,

pid_t p = fork(); // two processes after this
pid_t k = fork(); // executed by **two** processes, again duplicating

因此,在这两行之后,我们有四个进程执行其余的 并行代码。这是大脑爆炸的地方。什么 问k的值时,p行的泄漏子代会做什么 是吗?

看看这个小程序的输出,看看它的作用是什么 泄漏。

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(){
  printf("MAIN PID %d\n", getpid());

  fork();
  fork();

  printf("PID %d, PPID %d\n", getpid(), getppid());
  return 0;
}