0
當打印代碼只執行兩次打印代碼時,如何打印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 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
(我取出重複行,所以我不佔用太多空間)
謝謝, Hristo
http://stackoverflow.com/questions/2597084/problem-with-signal-handlers-being-called-too-many-times – 2010-04-08 02:13:28
這是重複的,因爲有人關閉我的文章,但沒有給我一個機會回覆。 – Hristo 2010-04-08 02:14:00
如果你的問題第一次被關閉,是什麼讓你認爲它會做得更好?不要發佈重複,至少拿出一個真正的標題。 – 2010-04-08 02:14:01