在继续之前,如何确保已收到信号

时间:2019-06-20 12:44:44

标签: c signals fork

我有几个孩子进程,我需要向父母发送信号,并确保已收到信号,然后再继续

#define CHILD 0
int n = 0;

void handler(int sig)  {
    n--;
    signal(SIGCHLD, handler);
}

int main(int argc, char * argv[]) {
    int p ;
    signal(SIGCHLD, handler);

    if(argc == 2) {
        int k = atoi(argv[1]);
        n = k;
        while(k-- != 0) {
            p = fork();
            if(p == CHILD) {
                srand(time(NULL)); 
                int r = 1 +(rand()%10);
                printf("No:%d %d Child PID: %d times.%d\n",k,n, getpid(),r);

//                int rai = raise(SIGCHLD);
                printf("rai: %d  %d\n", rai, n);
                sleep(r);

                exit(1);
            }
        }
    } else {
        puts("Please provide k num of zombie to be generated\n");
    }

    while(p > CHILD && n > 0) {
        printf("sleeping 2  %d\n", n);
        sleep(2);
    }

    return EXIT_SUCCESS;
}

如何确定父级捕获所有SIGCHILD信号 故意省略了进口

0 个答案:

没有答案