在C中接收到SIGINT时,在进程树中使用fork()创建X child
我已经为此挣扎了一段时间,我放弃了,所以我请求帮助 我需要编写一个永久等待SIGINT(Ctrl+C)的程序,当收到时,上一代进程(es)会创建X个child。你会说这是一棵“Xary”树(如果我错了,请纠正我) 因此: 收到一次信号时:在C中接收到SIGINT时,在进程树中使用fork()创建X child,c,process,tree,fork,C,Process,Tree,Fork,我已经为此挣扎了一段时间,我放弃了,所以我请求帮助 我需要编写一个永久等待SIGINT(Ctrl+C)的程序,当收到时,上一代进程(es)会创建X个child。你会说这是一棵“Xary”树(如果我错了,请纠正我) 因此: 收到一次信号时: $ pstree -p 6227 example(6227) example(6229) example(6230) 收到两次: $ pstree -p 6227 example(6229) exampl
$ pstree -p 6227
example(6227) example(6229)
example(6230)
收到两次:
$ pstree -p 6227
example(6229) example(6235)
example(6236)
tarea2(6227)
example(6230) example(6234)
example(6237)
等等
到目前为止,我能够捕获SIGINT,但在多次尝试失败后,我对如何创建树一无所知
这是我的代码:
(请忽略未使用的包含项)
#包括/*libreria estandar*/
#包括
#包括
#包括
#包括
#包括
#包括
#定义x2
无效复制(内部符号)
{
if(signum==SIGINT)
开关(信号)
{
案例信号:
/*一些我一直想弄明白的事情*/
}
}
内部主(空)
{
printf(“PID:%d\n”,getpid());
而(1)
{
中频(信号(SIGINT,REPLAYIR)=SIG_ERR)
{
perror(“错误”);
退出(退出失败);
}
}
}
提前非常感谢,我为所有的语法错误感到抱歉,这里以西班牙语为母语。这就是我想要的:
int reproducir(void)
{
if (spawn == 0){
for (i=0;i < X;i++)
{
spawn = fork();
if(spawn == 0) {
break;
}
}
}
return 0;
}
int-reproadir(无效)
{
如果(生成==0){
对于(i=0;i
您还可以向我们展示如何使用fork()
,并告诉我们您面临的问题。
int reproducir(void)
{
if (spawn == 0){
for (i=0;i < X;i++)
{
spawn = fork();
if(spawn == 0) {
break;
}
}
}
return 0;
}