当只打印代码两次时,如何打印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
(我已经取出了重复的线条,所以我不占用太多空间)
谢谢, 赫里斯托斯
答案 0 :(得分:1)
从我们掌握的少量信息......
这就是我们可以从您提供的数据中收集到的所有信息。如果要将stat参数传递给waitpid,您可以通过在处理程序中打印出termChildPID和终止原因来查看工作进程终止的原因。
如果你的问题是为什么“东西”打印两次,那么看看“头”指向的是什么。