2010-04-08 111 views
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

+0

http://stackoverflow.com/questions/2597084/problem-with-signal-handlers-being-called-too-many-times – 2010-04-08 02:13:28

+0

這是重複的,因爲有人關閉我的文章,但沒有給我一個機會回覆。 – Hristo 2010-04-08 02:14:00

+2

如果你的問題第一次被關閉,是什麼讓你認爲它會做得更好?不要發佈重複,至少拿出一個真正的標題。 – 2010-04-08 02:14:01

回答

1

從我們信息的少量...

  1. 的主要過程與PID = 16220
  2. 工作進程16220個運行創建工作進程,並終止
  3. 信號處理程序運行,並顯然「頭」列表中的第二個節點具有用於進程ID 16220的記錄(「stuff」打印兩次並且「if if」打印一次)。
  4. 主進程創建工作進程與PID = 16221
  5. 工作進程16221個運行並終止
  6. 信號處理程序運行,顯然所述第二節點中的「頭」列表具有的進程id 16221的記錄(「東西「打印兩次,」如果「打印一次)。

這就是我們可以從您提供的數據中收集的所有信息。如果你傳遞一個stat參數給waitpid,你可以看到爲什麼工作進程通過打印termChildPID和處理程序中的終止原因而終止。

如果你的問題是爲什麼「東西」打印兩次,然後看看「頭」指向什麼。

+0

謝謝你的迴應。我想出了我的問題並修復了它。 – Hristo 2010-04-08 03:01:31