gpt4 book ai didi

c - 检测多个子进程的终止

转载 作者:行者123 更新时间:2023-11-30 20:36:37 26 4
gpt4 key购买 nike

我必须编写代码来使用父进程创建多个子进程。父进程应该处于休眠状态,直到它创建了一定数量的进程。子进程向父进程发送 SIGCHLD 信号以中断 sleep ,并强制父进程调用 wait 来收集已终止子进程的状态集合。

我在UBUNTU上编写了以下代码:

#include <stdlib.h> /* needed to define exit() */
#include <unistd.h> /* needed for fork() and getpid() */
#include <signal.h> /* needed for signal */
#include <stdio.h> /* needed for printf() */

int main(int argc, char **argv)
{
void catch(int); /* signal handler */
void child(int); /* the child calls this */
void parent(int pid); /* the parent calls this */
int pid; /* process ID */
int i; /* Number of processes*/

signal(SIGCHLD, catch); /* detect child termination */

for(i=0;i<5;i++)
{
pid = fork(); /* create child process*/
switch (pid)
{
case -1: /* something went wrong */
perror("fork");
exit(1);
case 0: /* a fork returns 0 to the child */
child(i+1);
break;
default: /* a fork returns a pid to the parent */
//parent(pid);
break;
}
}
exit(0);
}

void child(int id)
{
printf("\tchild%d: I'm the child\n",id);
sleep(10); /* do nothing for 3 seconds */
printf("\tchild: I'm exiting\n");
exit(id); /* exit with a return code of 123 */
}

void parent(int pid)
{
printf("parent: I'm the parent\n");
sleep(15); /* do nothing for five seconds */
printf("parent: exiting\n");
}

void catch(int snum)
{
int pid;
int status;

pid = wait(&status);
printf("parent: child process exited with value %d\n",WEXITSTATUS(status));
/* WEXITSTATUS(status):
* Since only the last 8 bits are available
* it will be logical to mask the upper bit by
* doing a bitwise and operation with 255.
* A system defined macro does this for us.
*/
}

我得到这样的输出:

parent: I'm the parent
child1: I'm the child
child: I'm exiting
parent: child process exited with value 1
parent: exiting
parent: I'm the parent
child2: I'm the child
child: I'm exiting
parent: child process exited with value 2
parent: exiting
parent: I'm the parent
child3: I'm the child
child: I'm exiting
parent: child process exited with value 3
parent: exiting
parent: I'm the parent
child4: I'm the child
child: I'm exiting
parent: child process exited with value 4
parent: exiting
parent: I'm the parent
child5: I'm the child
child: I'm exiting
parent: child process exited with value 5
parent: exiting

而我想要这样的输出:

parent: I'm the parent
child1: I'm the child
child: I'm exiting
parent: child process exited with value 1
child2: I'm the child
child: I'm exiting
parent: child process exited with value 2
child3: I'm the child
child: I'm exiting
parent: child process exited with value 3
child4: I'm the child
child: I'm exiting
parent: child process exited with value 4
child5: I'm the child
child: I'm exiting
parent: child process exited with value 5
parent: exiting

我应该在代码中进行哪些更改...?请帮忙..!!!

最佳答案

您正在循环内打印 parent: I'm the Parentparent: exiting 。如果您希望它们只打印一次,请将它们移出循环。

您可能还想考虑使用 waitwaitpid 函数来等待子进程终止,而不是使用信号处理程序和 sleep.

关于c - 检测多个子进程的终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com