简体   繁体   English

信号处理程序出现问题

[英]Problem with signal handlers

how can something print 3 times when it only goes the printing code twice? 仅打印两次代码时,如何打印3次? I'm coding in C and the code is in a SIGCHLD signal handler I created. 我用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;
}

the output for my MAIN process is this: 我的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 

and the output for the MONITOR process is this: 并且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 

( I've taken out repeating lines so I don't take up so much space ) (我已经删除了重复的行,所以我不会占用太多空间)

Thanks, Hristo 谢谢,克里斯托

From the small amount of information we have... 从少量信息中我们可以获得...

  1. The main process creates worker process with pid = 16220 主进程创建pid = 16220的工作进程
  2. Worker process 16220 runs and terminates 工作进程16220运行并终止
  3. The signal handler runs and apparently the second node in the "head" list has a record for process id 16220 ("stuff" prints twice and "inside if" prints once). 信号处理程序运行,显然“头”列表中的第二个节点具有进程ID 16220的记录(“填充”打印两次,“内部如果打印”一次)。
  4. The main process creates worker process with pid = 16221 主进程创建pid = 16221的工作进程
  5. Worker process 16221 runs and terminates 工作进程16221运行并终止
  6. The signal handler runs and apparently the second node in the "head" list has the record for process id 16221 ("stuff" prints twice and "inside if" prints once). 信号处理程序运行,显然“头”列表中的第二个节点具有进程ID 16221的记录(“填充”打印两次,“内部如果”打印一次)。

That is about all we can glean from the data you have provided. 这就是我们可以从您提供的数据中收集到的所有信息。 If you were to pass a stat parameter for to waitpid you could see why the worker processes terminated by printing out termChildPID and the termination reason in the handler. 如果将stat参数传递给waitpid,则可以看到为什么工作进程通过打印出termChildPID终止的原因以及处理程序中的终止原因。

If your question is why does "stuff" print twice then take a look at what "head" points to. 如果您的问题是为什么“填充”打印两次,请查看“打印头”指向的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM