簡體   English   中英

信號處理程序出現問題

[英]Problem with signal handlers

僅打印兩次代碼時,如何打印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 

(我已經刪除了重復的行,所以我不會占用太多空間)

謝謝,克里斯托

從少量信息中我們可以獲得...

  1. 主進程創建pid = 16220的工作進程
  2. 工作進程16220運行並終止
  3. 信號處理程序運行,顯然“頭”列表中的第二個節點具有進程ID 16220的記錄(“填充”打印兩次,“內部如果打印”一次)。
  4. 主進程創建pid = 16221的工作進程
  5. 工作進程16221運行並終止
  6. 信號處理程序運行,顯然“頭”列表中的第二個節點具有進程ID 16221的記錄(“填充”打印兩次,“內部如果”打印一次)。

這就是我們可以從您提供的數據中收集到的所有信息。 如果將stat參數傳遞給waitpid,則可以看到為什么工作進程通過打印出termChildPID終止的原因以及處理程序中的終止原因。

如果您的問題是為什么“填充”打印兩次,請查看“打印頭”指向的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM