简体   繁体   English

等待C中的兄弟进程

[英]Wait for sibling process in C

While I was playing with C, and trying to learn more about processes, forks and wait, I've reached a problem where I'm not able to wait for a sibling process to finish until I can continue.当我在玩 C 并尝试了解有关进程、分叉和等待的更多信息时,我遇到了一个问题,即我无法等待兄弟进程完成,直到我可以继续。

So, here's the problem:所以,问题来了:

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


int processoDisplayLyric;
int processoDisplayGraph;
int processoTimer;

int main(){

    int numeroMusicaAtual = 0;
    int continueWorking = 0;
    int fathersPID = getpid();


    while(continueWorking == 0) {


        //Create graph process
        processoDisplayGraph = fork();

        if(processoDisplayGraph == 0){
            int work = 0;
            while(work == 0){
                pause();
            }
        }

        if(processoDisplayGraph != 0){

            //Create lyric process.
            processoDisplayLyric =  fork();

            if(processoDisplayLyric == 0){

                int work = 0;
                while(work == 0){
                    pause();
                }
            }

        }

        if(processoDisplayLyric != 0 && processoDisplayGraph != 0){

            //Create timer process.
            processoTimer = fork();

            if(processoTimer == 0){
                printf("I was created and i just want to wait for my brothers.\n");
            }

        }

        if(getpid() != fathersPID){

            wait(processoDisplayLyric);
            wait(processoDisplayGraph);
        }else{
            //It's the father.
            int child_status;
            for (int i = 0; i < 2; i++) {
                pid_t wpid = waitpid(processoDisplayLyric, &child_status, 0);
                if (WIFEXITED(child_status))
                    printf("Saw %d done with %d\n", wpid, WEXITSTATUS(child_status));
                else
                    printf("Child %d terminated abnormally\n", wpid);
            }
        }

        numeroMusicaAtual++;
    }

}

The thing is: processDisplayLyric, processDisplayGraph and processTimer are created, BUT, the Timer DOESN'T wait for the other two to finish (something that should never happen.!!).问题是:创建了 processDisplayLyric、processDisplayGraph 和 processTimer,但是,Timer 不会等待其他两个完成(这永远不会发生。!!)。

Under standard POSIX, only a parent process can wait for its (immediate) children to die.在标准 POSIX 下,只有父进程可以等待其(直接)子进程死亡。

  • Sibling processes cannot wait for other sibling processes to die.同级进程不能等待其他同级进程死亡。

  • Child processes cannot wait for parent processes to die.子进程不能等待父进程死亡。

  • A grandparent process cannot wait for grandchildren to die.祖父母进程不能等待孙辈死亡。

There are ways to detect whether a parent process is dead (the result of getppid() is 1 , or if you recorded the original PPID, then signalling it fails).有一些方法可以检测父进程是否已死( getppid()的结果是1 ,或者如果您记录了原始 PPID,则表明它失败了)。 You can find out if you could signal a known PID — and if it doesn't exist, you can't.您可以找出是否可以发出已知 PID 的信号——如果它不存在,则不能。 There may be some alternatives on some platforms using other APIs.在使用其他 API 的某些平台上可能有一些替代方案。 But there's no general analogy to wait() for siblings or parents (or any other related process) — only children can be waited for.但是对于兄弟姐妹或父母(或任何其他相关过程)没有一般的类比wait() ) - 只有孩子可以被等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM