我正在研究cygwin,以测试Linux系统编程。我的问题是使用信号时。我想创建n个进程,当父进程收到子进程发送的信号SIGALRM时,父进程将终止该进程。每个孩子都会发送一个警报,因此将发送n个警报,并杀死n个孩子。
我在执行程序时遇到问题,但是我不知道我做错了什么还是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);
}
有了这个,孩子们应该戒烟了,但是当我执行死刑时,我得到了一些奇怪的东西:
$ ./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)
我可以解决这个问题吗?
答案 0 :(得分:0)
我通过使用SIGKILL而不是SIGQUIT解决了