信号处理

发布于 2024-10-08 19:28:37 字数 261 浏览 0 评论 0原文

情况是:我用信号函数注册了一个信号处理程序。 问题:

  1. 处理程序是否总是独立于进程状态被调用? (我的意思是它停止了,等等)。那里会发生什么?(取决于状态)

  2. 处理程序函数是否被系统注册为一些“特殊函数”(即,当处理程序运行时,未收到其他信号并被放入堆栈或类似的东西中。或者也许它们是如果没有,想象一下,当处理程序 strats 时,进程收到另一个信号,然后再次调用该处理程序,尽管“第一个”尚未完成其任务,依此类推。

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 stack 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光无声 2024-10-15 19:28:37
  1. “当进程停止时,在进程继续之前,发送到该进程的任何其他信号都不会被传递,除了 SIGKILL (...) 的默认操作SIGCONT 是在首先处理任何未决的未阻塞信号之后,在进程停止的位置恢复执行。” (Unix 标准,信号概念部分。)

  2. 通常,没有什么特别的事情发生。当信号 X 在 Y 的信号处理程序中被捕获时,执行将简单地转移到 X 的处理程序,之后 Y 的处理程序恢复执行。

以下程序演示了这种行为。 raise(sig) 向调用进程发送信号(类似于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;
}

这是“安全的”,因为进程只会接受来自具有相同用户 ID(或 root)的进程的信号,因此您只能搬起石头砸自己的脚。

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文