How to make parent process wait for child processes to finish?

左心房为你撑大大i 提交于 2021-02-10 18:19:43

问题


I have an assignment which gives me this code to transform into a code that makes the parent process wait for all children processes to finish. PS: the first code has 4 processes and needs to use waitpid to solve this.

#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);
  }
}

I've tried this, but I think that this only works for only 1 child process and not for a lot of them.

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");
            }
        }
    }
}

回答1:


This is one way to do it; there will be numerous others.

#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);
}

Example output:

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



回答2:


This won't do your assignment, but I hope it is advice enough to get you going. The assignment appear to be a riddle around fork(), your teacher has good taste :-)

fork() is different. It returns twice.

  • In the parent it returns the process ID of the created process.
  • In the child it returns 0; a process can always determine its PID using getpid()

Actually, the assignment is not good taste. Usually code using `fork() never lets any branch escape into enclosing code to avoid complete bullshit. Like so,

#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;
}

With this in mind, look at these two innocent looking lines,

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

So, after these two lines we have four processes executing the rest of the code in parallel. This is the point where brains explode. What does the leaked child of the k line do when it asks what p's value is?

Look at the output of this little program, to see what's the effect of leaking.

#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;
}


来源:https://stackoverflow.com/questions/61466564/how-to-make-parent-process-wait-for-child-processes-to-finish

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!