开发者

Signal handling

开发者 https://www.devze.com 2023-01-30 23:58 出处:网络
The case is: I register a signal handler with signal function. The question: Is handler always called independently of the process state? (I mean its stopped, etc.). What happens there?(dependently

The case is: I register a signal handler with signal function. The question:

  1. Is handler always called independently of the process state? (I mean its stopped, etc.). What happens there?(dependently of the state)

  2. Are handler functions registered as some "special functions" by system (i.e. when handeler runs other signals are not recieved and are put into the s开发者_开发知识库tack or smth like that. Or maybe they are simply ignored.) If not, imagine that when handler strats, process gets another signal, then this handler is called again inspite of "the first" hasn't completed its task yet and so on.


  1. "While a process is stopped, any additional signals that are sent to the process shall not be delivered until the process is continued, except SIGKILL (...) The default action for SIGCONT is to resume execution at the point where the process was stopped, after first handling any pending unblocked signals." (Unix standard, section Signal Concepts.)

  2. Ordinarily, nothing special happens. When a signal X is caught while in a signal handler for Y, execution is simply transferred to the handler for X, after which the handler for Y resumes execution.

The following program demonstrates this behavior. raise(sig) sends a signal to the calling process (it's like kill(getpid(), sig)).

void hello(int unused)
{
    printf("Hello, ");
    raise(SIGUSR2);
    printf("!\n");
}

void world(int unused)
{
    printf("world");
}

int main()
{
    signal(SIGUSR1, hello);
    signal(SIGUSR2, world);
    raise(SIGUSR1);

    return 0;
}

This is "safe" because a process will accept signals only from processes with the same user ID (or root), so you can only shoot yourself in the foot this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消