简体   繁体   English

孩子没有得到系统呼叫信号

[英]children do not get the system call signal

The father forks as many children as the parameter given from the command line.The children after birth are paused and the father awakens them by sending signal. 父亲分叉了与命令行中给定的参数一样多的孩子,分娩后的孩子被暂停,父亲通过发送信号唤醒他们。 However, in the way I wrote it the children never seem to get the awaking signal. 但是,按照我写这本书的方式,孩子们似乎从来没有得到唤醒的信号。 Do I get the concept of children pids wrong and that's why they never get the signal? 我是否理解儿童pid的概念是错误的,这就是为什么他们从来没有收到信号的原因?

./test 4 5 1 3 2 /测试4 5 1 3 2

as posted above 4, 5, .... stand for child 4, child 5, ... I save them consecutively as they are posted from the command line in array for saving their indexes (for later usage...) and their pids in another array . 如上面的4,5,....代表child 4,child 5,...我连续保存它们,因为它们是从命令行以数组形式发布的,用于保存它们的索引(供以后使用...)及其pids在另一个数组中。 I want to send the signal (the father wants) consecutively as cited in the command line 我想按命令行中的顺序连续发送信号(父亲想要的)

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

void handler();

int main(int argc, char **argv){

    pid_t proc_pid[argc-1], order[argc-1];
    int i;

    for(i=0;i<=argc-2;i++){
        order[i]=atoi(argv[i+1]);
        //printf("%d\n",order[i]);
        //printf("%d\n",i);
        if((proc_pid[i] = fork()) < 0){
            perror("fork");
        }
        else if(proc_pid[i] == 0){
           // proc_pid[i]=getpid();
            printf("%d\n",proc_pid[i]);
            pause();
            signal(SIGINT, handler);
            exit(0);
        }
        else{     
            kill(proc_pid[i], SIGINT);     
            //kill(0, SIGINT);
           // exit(0); 
        }     
    } 
    return 0;
}

void handler(){
    printf("ok\n");
}

The order of father-process and child-process is uncertain. 父进程和子进程的顺序不确定。

You can add sleep(1) in your code to let father-process run after child-process, like this: 您可以在代码中添加sleep(1) ,以使父进程在子进程之后运行,如下所示:

...

   sleep(1);
   kill(proc_pid[i], SIGINT);     
   //kill(0, SIGINT);
   // exit(0); 

...

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

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