gpt4 book ai didi

无法在 cygwin 中使用 SIGQUIT 终止子进程

转载 作者:行者123 更新时间:2023-11-30 16:19:59 24 4
gpt4 key购买 nike

我正在使用 cygwin 来测试 Linux 系统编程。我的问题是在使用信号时。我想创建 n 个进程,当父进程收到子进程发送的信号 SIGALRM 时,父进程将杀死这些进程。每个 child 都会发送一个警报,因此将发送 n 个警报,并且应杀死 n 个 child 。

我在执行程序时遇到问题,但我不知道是我做错了什么还是 cygwin 无法做到这一点。

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

int nb;
pid_t* p;

void handler(int s) {
signal(s, handler);
for(int i=0; i<nb; i++) {
printf("%d killed\n", p[i]);
kill(p[i], SIGQUIT);
}
}

int main(int arc, char* argv[]) {
signal(SIGALRM, handler);
int n=atoi(argv[1]);
nb=n;
p=(pid_t*)malloc(nb*sizeof(pid_t));
for(int i=0; i<nb; i++) {
if((p[i]=fork())==0) {
printf("child : %d\n", getpid());
alarm(1);
for(;;);
exit(0);
}
}
printf("parent : %d\n", getpid());
pause();
exit(0);
}

有了这个, children 应该退出,但当我执行时,我遇到了一些奇怪的事情:

$ ./a.out 5
child : 1048
child : 1049
child : 1050
child : 1051
parent : 1047
child : 1052
0 killed
Stack trace:
Frame Function Args
000FFFFC7D0 00180060BE9 (00000000000, 000FFFFCE00, 000FFFFC9D8, 000FFFFDE50)
00000000002 00180062ABA (00000000000, 001800FE6C8, 000FFFFC9B0, 00000000000)
000FFFFCBD0 00180128550 (00000000000, 001800FE6C8, 000FFFFC9B0, 00000000000)
000FFFFCBD0 001004011C4 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFC5E0 001004011C4 (7FFBE56E6A08, 00000000000, 3734EC77843980, 000FFFFC830)
000FFFFC830 00068040004 (7FFBE56E6A08, 00000000000, 3734EC77843980, 000FFFFC830)
End of stack trace
Stack trace:
Frame Function Args
000FFFFC7D0 00180060BE9 (00000000000, 000FFFFCE00, 000FFFFC9D8, 000FFFFDE50)
Stack trace:
Frame Function Args
00000000002 00180062ABA000FFFFC7D0 00180060BE9 (00000000000 (00000000000, 000FFFFCE00Stack trace:
Frame Function Args
, 001800FE6C8000FFFFC080 00180060BE9, 000FFFFC9B0, 00000000000)
000FFFFCBD0 00180128550 (00000000000 (00000000000, 000FFFFCE00, 000FFFFC288, 001800FE6C8, 000FFFFDE50, 000FFFFC9B0)
, 00000000000)
000FFFFCBD0 001004011C4 (00000000000, 00000000000, 00000000000, 00000000000)
00000000002 00180062ABA, 000FFFFC9D8, 000FFFFDE50)
00000000002 00180062ABA (00000000000 (00000000064, 001800FE6C8Stack trace:
Frame Function Args
, 000FFFFC9B0, 00000000000)
000FFFFCBD0 00180128550 (00000000000000FFFFC5E0 001004011C4 (7FFBE56E6A08, 001800FE6C8, 00000000000, 000FFFFC9B0000FFFFC7D0 00180060BE9 (00000000000, 0000000017C, 00000000000, 3734EC77862237, 000FFFFC830, 00000000000, 000FFFFCE00)
, 00000000000)
, 000FFFFC9D8000FFFFCBD0 001004011C4 (00000000000, 00000000000, 00000000000, 000FFFFDE50, 00000000000)
000FFFFC830 00068040004)
000FFFFC5E0 001004011C4 (7FFBE56E6A08, 00000000000)
(7FFBE56E6A0800000000002 00180062ABA (00000000000000FFFFC360 00180130B1B (000FFFFC514, 001800FE6C8, 00000000000, 3734EC77862237, 000FFFFC830, 3734EC778A3B1B, 00000000001, 000FFFFC830, 000FFFFC9B0)
, 00000000000, 00100000000)
End of stack trace
, 00000B74440000FFFFC830 00068040004 (7FFBE56E6A08, 00000000000)
, 3734EC778A3B1B)
000FFFFCBD0 00180128550 (00000000000, 000FFFFC830, 001800FE6C8)
, 000FFFFC9B0End of stack trace
000FFFFC700 0018012CAAE, 00000000000)
(7FFBE56EC859000FFFFCBD0 001004011C4, 00000000001 (00000000000, 7FFBE56E3533, 00000000000, 00000000001, 00000000000, 00000000000)
000FFFFC5E0 001004011C4)
(7FFBE56E6A08000FFFFC5F0 0018012CE7F, 00000000000 (00100000000, 3734EC7788519E, 000FFFFC830, 00000000000, 0018021E2DC, 0000000000D)
)
000FFFFC7E0 0018012CFDC000FFFFC830 00068040004 (000FFFFC7D0 (7FFBE56E6A08, 000FFFFC830, 00000000000, 3734EC7788519E, 000FFFFC830)
End of stack trace
, 000FFFFC780, 00000000003)
000FFFFC7E0 0018012D180 (00000000000, 00010000000, 00000002000, 00000000138)
000FFFFC7E0 0018012842B (00000000000, 00010000000, 00000002000, 00000000138)
000FFFFC7E0 001004010EF (00180240A28, 001800FE3B0, C85632670B110000, 0018027EF00)
00010000000 00180062ABA (00000000000, 001800FE6C8, 000FFFFC9B0, 00000000000)
000FFFFCBD0 00180128550 (00000000000, 001800FE6C8, 000FFFFC9B0, 00000000000)
000FFFFCBD0 001004011C4 (00000000000, 00000000000, 00000000000, 00000000000)
000FFFFC5E0 001004011C4 (001E56E6A08, 0C800000000, 00600048640, 00600048970)
000FFFFC830 00068040004 (001E56E6A08, 0C800000000, 00600048640, 00600048970)
End of stack trace
Quit (core dumped)

我可以解决这个问题吗?

最佳答案

我通过使用 SIGKILL 而不是 SIGQUIT 解决了这个问题

关于无法在 cygwin 中使用 SIGQUIT 终止子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429328/

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