gpt4 book ai didi

child 收不到系统调用信号

转载 作者:太空宇宙 更新时间:2023-11-04 02:23:06 25 4
gpt4 key购买 nike

父亲根据命令行给出的参数fork出尽可能多的 child 。 child 出生后暂停,父亲通过发送信号唤醒他们。然而,按照我写的方式, children 似乎从来没有得到醒来的信号。我是否误解了 child pids 的概念,这就是为什么他们永远不会收到信号?

./test 4 5 1 3 2

如上所示 4, 5, .... 代表 child 4, child 5, ...我连续保存它们,因为它们是从命令行发布到数组中的,用于保存它们的索引(供以后使用...)和它们的 pids 在另一个数组中。我想按照命令行中引用的方式连续发送信号(父亲想要)

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

最佳答案

父进程和子进程的顺序是不确定的。

您可以在代码中添加sleep(1) 让父进程在子进程之后运行,如下所示:

...

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

...

关于 child 收不到系统调用信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55059966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com