C使用信号停止子进程 [英] C using Signals to stop child processes

查看:103
本文介绍了C使用信号停止子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的程序是创建子进程并为其提供工作(CPU密集型工作). main()坐在那里,等待子进程通过管道(使用select)发送数据.

My current program is creating child processes and giving them work (CPU intensive work). The main() sits there and waits for the child processes to send data via pipes (using select).

我想要做的是当程序正在处理数据时,我可以按CTRL + C来停止子进程的工作,并询问用户是否要退出或继续工作.

What I wanted to do is when the program is processing data I could press CTRL+C to stop the child processes from working and asking the user if he wants to quit or resume work.

如果用户要退出,该程序将终止所有进程.如果用户想恢复工作,它将告诉子进程恢复计算.

If user wants to quit, the program would kill all the processes. If user wants to resume work, it would tell the child processes to resume the computation.

我已经准备好了代码,但是工作不正常.

I already have the code in place but it's not quite working right.

总的来说,我有signal(SIGINT, pausar);来处理SIGINT(CTRL + C).

In main I have signal(SIGINT, pausar); to handle SIGINT (CTRL+C).

这是pausar()函数:

This is the pausar() function:

void pausar(int signum){
    signal(SIGINT, pausar);

    int i;
    // pid[] contains all the child processes
    for(i = 0; i<CORES; i++)
    {
        kill(pid[i], SIGSTOP);
    }

    char option[2];
    printf("\n Computacao pausada.\n'S' para sair ou 'C' para continuar: ");

    scanf("%1s", option);
    if (option[0] == 's' || option[0] == 'S') {
        printf("A desligar...\n");

        //if user wants to quit, kill all the child processes
        for(i = 0; i<CORES; i++)
        {
            kill(pid[i], SIGKILL);
        }

        exit(0);
    }
    else
    {
        printf("[%d] A resumir computacao...\n",getpid());
        kill(getpid(), SIGCONT);

        //if user wants to resume work, send signal to continue
        for(i = 0; i<CORES; i++)
        {
            kill(pid[i], SIGCONT);
            printf("%d resumiu\n", pid[i]);
        }
    }
}

问题是有时我按CTRL + C,但控制台中什么都没显示(但是进程STOP,因为我正在关注进程管理器).另一个问题是,当我输入"C"以恢复工作后,在select()中出现错误,孩子们再也无法恢复工作了.

The problem is that sometimes I press CTRL+C and nothing shows in the console (but the processes STOP because I'm paying attention to the process manager). The other problem is that after I enter 'C' to resume work, I get errors in select() and the children never resume work.

推荐答案

首先,对于您要执行的操作,信号处理程序过于复杂.其次,在信号处理程序中调用signal()并不是一个好主意……它不是异步信号安全功能.

First, for what you're trying to-do, your signal handler is way too complex. Secondly, calling signal() inside your signal handler is not a good idea ... it's not an asynchronous signal-safe function.

您可以执行以下操作:

  1. 在主机中,像完成操作一样,使用signal()设置信号处理程序功能.
  2. 通过sigprocmask()阻止SIGINT信号.这样可以防止在调用pselect()之前出现虚假信号.
  3. 仅在信号处理程序内部 设置一个简单的全局标志,该标志是sig_atomic_t
  4. 使用pselect()代替select().这将允许您更改过程信号掩码,以允许SIGINT信号到达,并且它将相对于信号以原子方式这样做.否则,您可能会在调用select()之前使SIGINT到达,然后即使该信号确实在处理程序中设置了标志,也丢失"了该信号.
  5. pselect()调用返回时,检测是否已设置标志.
  6. 如果设置了全局sig_atomic_t标志,并且由于捕获到信号而从pselect返回,则启动另一个函数,该函数实际上将完成子进程的所有结尾并提示用户,等等.
  1. In your main, set the signal handler function using signal() like you've done.
  2. Block the SIGINT signal via sigprocmask(). This prevents a spurious signal from arriving before the call to pselect().
  3. Inside your signal handler only set a simple global flag that is a sig_atomic_t
  4. Use pselect() instead of select(). This will allow you to change the process signal mask to allow a SIGINT signal to arrive, and it will do-so in an atomic manner with respect to signals. Otherwise, you could have your SIGINT arrive before the call to select(), and then you have "lost" that signal, even though it does set the flag in the handler.
  5. When the pselect() call returns, detect whether the flag has been set.
  6. If the global sig_atomic_t flag was set, and you returned from pselect because of a caught signal, then launch another function that will actually do all the ending of the child-processes and prompt the user, etc.

执行这些步骤将简化您的信号处理代码,并由于信号到达的异步特性而减少竞争条件或其他意外结果的机会.

Doing these steps will simplify your signal-handling code and reduce the chances of race-conditions or other unexpected results because of the asynchronous nature of signal arrival.

如果您想了解有关pselect()的更多信息,您可以在此处找到不错的文章.

If you'd like some more information on pselect(), you there is a nice article on that here.

这篇关于C使用信号停止子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆