2010-04-08 109 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