简体   繁体   English

C使用信号停止子进程

[英]C using Signals to stop child processes

My current program is creating child processes and giving them work (CPU intensive work). 我当前的程序是创建子进程并为其提供工作(CPU密集型工作)。 The main() sits there and waits for the child processes to send data via pipes (using select). main()坐在那里,等待子进程通过管道(使用select)发送数据。

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. 我想做的是,当程序正在处理数据时,我可以按CTRL + C来停止子进程的工作,并询问用户是否要退出或恢复工作。

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. 我已经有适当的代码,但是工作不正常。

In main I have signal(SIGINT, pausar); 我主要有signal(SIGINT, pausar); to handle SIGINT (CTRL+C). 处理SIGINT(CTRL + C)。

This is the pausar() function: 这是pausar()函数:

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]);
        }
    }
}

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). 问题是有时我按CTRL + C,但控制台中什么也没显示(但是进程STOP,因为我正在关注进程管理器)。 The other problem is that after I enter 'C' to resume work, I get errors in select() and the children never resume work. 另一个问题是,在我输入“ C”以恢复工作之后,我在select()中出错,并且孩子们再也没有恢复工作。

Using select() and signal-handler at the same time is prone to race conditions - a signal could occur during the select() call, but also in every other line of code. 同时使用select()和signal-handler很容易出现竞争情况-在select()调用期间以及每隔一行代码中都可能发生信号。

If your are on linux: create an event socket with signalfd() and add this socket to the read set passed to select() . 如果您使用的是Linux:请使用signalfd()创建一个事件套接字, signalfd()将此套接字添加到传递给select()的读取集中。 Signals are then handled at a fixed point in your code and you do not need to worry about race conditions. 然后,在代码中的固定点处处理信号,而您不必担心争用条件。

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 . 其次,在信号处理程序中调用signal()并不是一个好主意……它不是异步信号安全函数

What you can do is the following: 您可以执行以下操作:

  1. In your main, set the signal handler function using signal() like you've done. 在您的主机中,像完成操作一样,使用signal()设置信号处理函数。
  2. Block the SIGINT signal via sigprocmask() . 通过sigprocmask()阻止SIGINT信号。 This prevents a spurious signal from arriving before the call to pselect() . 这样可以防止在调用pselect()之前出现虚假信号。
  3. Inside your signal handler only set a simple global flag that is a sig_atomic_t 在信号处理程序内部, 设置一个简单的全局标志sig_atomic_t
  4. Use pselect() instead of select() . 使用pselect()而不是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. 这将允许您更改过程信号掩码以允许SIGINT信号到达,并且它将以原子方式相对于信号这样做。 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. 否则,您可能会在调用select()之前让SIGINT到达,然后“丢失”该信号,即使它确实在处理程序中设置了该标志也是如此。
  5. When the pselect() call returns, detect whether the flag has been set. pselect()调用返回时,检测该标志是否已设置。
  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. 如果设置了全局sig_atomic_t标志,并且由于捕获到信号而从pselect返回,则启动另一个函数,该函数实际上将完成子进程的所有结尾并提示用户,等等。

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

If you'd like some more information on pselect() , you there is a nice article on that here . 如果您想了解有关pselect()更多信息,请在此处找到一篇不错的文章

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

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