信号处理程序的问题
当某个东西只打印两次代码时,它怎么能打印 3 次呢?我正在用 C 进行编码,代码位于我创建的 SIGCHLD 信号处理程序中。
void chld_signalHandler() {
int pidadf = (int) getpid();
printf("pidafdfaddf: %d\n", pidadf);
while (1) {
int termChildPID = waitpid(-1, NULL, WNOHANG);
if (termChildPID == 0 || termChildPID == -1) {
break;
}
dll_node_t *temp = head;
while (temp != NULL) {
printf("stuff\n");
if (temp->pid == termChildPID && temp->type == WORK) {
printf("inside if\n");
// read memory mapped file b/w WORKER and MAIN
// get statistics and write results to pipe
char resultString[256];
// printing TIME
int i;
for (i = 0; i < 24; i++) {
sprintf(resultString, "TIME; %d ; %d ; %d ; %s\n",i,1,2,temp->stats->mboxFileName);
fwrite(resultString, strlen(resultString), 1, pipeFD);
}
remove_node(temp);
break;
}
temp = temp->next;
}
printf("done printing from sigchld \n");
}
return;
}
我的 MAIN 进程的输出是这样的:
MAIN PROCESS 16214 created WORKER PROCESS 16220 for file class.sp10.cs241.mbox
pidafdfaddf: 16214
stuff
stuff
inside if
done printing from sigchld
MAIN PROCESS 16214 created WORKER PROCESS 16221 for file class.sp10.cs225.mbox
pidafdfaddf: 16214
stuff
stuff
inside if
done printing from sigchld
MONITOR 进程的输出是这样的:(
MONITOR: pipe is open for reading
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs225.mbox
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs225.mbox
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs241.mbox
MONITOR: end of readpipe
我已经删除了重复的行,所以我不会占用太多空间)
谢谢, 赫里斯托
how can something print 3 times when it only goes the printing code twice? I'm coding in C and the code is in a SIGCHLD signal handler I created.
void chld_signalHandler() {
int pidadf = (int) getpid();
printf("pidafdfaddf: %d\n", pidadf);
while (1) {
int termChildPID = waitpid(-1, NULL, WNOHANG);
if (termChildPID == 0 || termChildPID == -1) {
break;
}
dll_node_t *temp = head;
while (temp != NULL) {
printf("stuff\n");
if (temp->pid == termChildPID && temp->type == WORK) {
printf("inside if\n");
// read memory mapped file b/w WORKER and MAIN
// get statistics and write results to pipe
char resultString[256];
// printing TIME
int i;
for (i = 0; i < 24; i++) {
sprintf(resultString, "TIME; %d ; %d ; %d ; %s\n",i,1,2,temp->stats->mboxFileName);
fwrite(resultString, strlen(resultString), 1, pipeFD);
}
remove_node(temp);
break;
}
temp = temp->next;
}
printf("done printing from sigchld \n");
}
return;
}
the output for my MAIN process is this:
MAIN PROCESS 16214 created WORKER PROCESS 16220 for file class.sp10.cs241.mbox
pidafdfaddf: 16214
stuff
stuff
inside if
done printing from sigchld
MAIN PROCESS 16214 created WORKER PROCESS 16221 for file class.sp10.cs225.mbox
pidafdfaddf: 16214
stuff
stuff
inside if
done printing from sigchld
and the output for the MONITOR process is this:
MONITOR: pipe is open for reading
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs225.mbox
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs225.mbox
MONITOR PIPE: TIME; 0 ; 1 ; 2 ; class.sp10.cs241.mbox
MONITOR: end of readpipe
( I've taken out repeating lines so I don't take up so much space )
Thanks,
Hristo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我们掌握的少量信息来看...
这就是我们从您提供的数据中所能收集到的全部信息。如果您要向 waitpid 传递 stat 参数,您可以通过在处理程序中打印 termChildPID 和终止原因来了解工作进程终止的原因。
如果您的问题是为什么“stuff”打印两次,那么看看“head”指向什么。
From the small amount of information we have...
That is about all we can glean from the data you have provided. If you were to pass a stat parameter for to waitpid you could see why the worker processes terminated by printing out termChildPID and the termination reason in the handler.
If your question is why does "stuff" print twice then take a look at what "head" points to.