信号处理程序的问题

发布于 2024-08-28 13:58:42 字数 1650 浏览 4 评论 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 进程的输出是这样的:

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 

我已经删除了重复的行,所以我不会占用太多空间)

谢谢, 赫里斯托

how can something print 3 times when it only goes the printing code twice? I'm coding in C and the code is in a SIGCHLD signal handler I created.

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 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: 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-09-04 13:58:43

从我们掌握的少量信息来看...

  1. 主进程创建了 pid = 16220 的工作进程
  2. 工作进程 16220 运行并终止
  3. 信号处理程序运行,显然“头”列表中的第二个节点有进程 id 16220 的记录( “stuff”打印两次,“inside if”打印一次)。
  4. 主进程创建 pid = 16221 的
  5. 工作进程 工作进程 16221 运行并终止
  6. 信号处理程序运行,显然“head”列表中的第二个节点有进程 id 16221 的记录(“stuff”打印两次,“inside if”打印一次)。

这就是我们从您提供的数据中所能收集到的全部信息。如果您要向 waitpid 传递 stat 参数,您可以通过在处理程序中打印 termChildPID 和终止原因来了解工作进程终止的原因。

如果您的问题是为什么“stuff”打印两次,那么看看“head”指向什么。

From the small amount of information we have...

  1. The main process creates worker process with pid = 16220
  2. Worker process 16220 runs and terminates
  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).
  4. The main process creates worker process with pid = 16221
  5. Worker process 16221 runs and terminates
  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).

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.

If your question is why does "stuff" print twice then take a look at what "head" points to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文