如何在Linux上执行异步信号处理程序? [英] How are asynchronous signal handlers executed on Linux?

查看:152
本文介绍了如何在Linux上执行异步信号处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道异步信号处理程序的执行究竟是如何在Linux上工作。首先,我不清楚为的线程执行信号处理程序。第二,我想知道,遵循以使线程执行信号处理程序的步骤。

I would like to know exactly how the execution of asynchronous signal handlers works on Linux. First, I am unclear as to which thread executes the signal handler. Second, I would like to know the steps that are followed to make the thread execute the signal handler.

在第一个问题,我看了两个不同的,看似矛盾,解释:

On the first matter, I have read two different, seemingly conflicting, explanations:


  1. Linux内核,由安德里斯布劳威尔,§ 5.2接收信号状态的:

当信号到达时,过程被中断,当前的寄存器被保存,并且该信号处理被调用。当信号处理函数返回时,被中断的活动将继续进行。

When a signal arrives, the process is interrupted, the current registers are saved, and the signal handler is invoked. When the signal handler returns, the interrupted activity is continued.

href=\"http://stackoverflow.com/q/6223191/196844\">的StackOverflow问题处理异步信号在多线程程序使我想到的 SCO Unix的的:

  • The StackOverflow question "Dealing With Asynchronous Signals In Multi Threaded Program" leads me to think that Linux's behavior is like SCO Unix's:

    当一个信号被发送到一个过程中,如果被抓住它,它会被一个处理,且只有一个,任一下列条件满足线程

    When a signal is delivered to a process, if it is being caught, it will be handled by one, and only one, of the threads meeting either of the following conditions:


        
    1. 阻塞在 调用sigwait (A线2)系统调用其参数的确实的包括捕获信号的类型。

    1. A thread blocked in a sigwait(2) system call whose argument does include the type of the caught signal.

    一个线程的信号掩码的的包括捕获信号的类型。

    A thread whose signal mask does not include the type of the caught signal.

    其他注意事项:

    • A thread blocked in sigwait(2) is given preference over a thread not blocking the signal type.
    • If more than one thread meets these requirements (perhaps two threads are calling sigwait(2)), then one of them will be chosen. This choice is not predictable by application programs.
    • If no thread is eligible, the signal will remain ``pending'' at the process level until some thread becomes eligible.

    此外,Linux的信号处理模型由摩西·酒吧国家异步信号输送到第一线程发现没有阻塞该信号。,这是我间$ p $角意味着该信号被输送到具有其sigmask一些螺纹的的包括信号

    Also, "The Linux Signals Handling Model" by Moshe Bar states "Asynchronous signals are delivered to the first thread found not blocking the signal.", which I interpret to mean that the signal is delivered to some thread having its sigmask not including the signal.

    哪一个是正确的?

    在第二个事情,会发生什么情况的堆栈和选择线程寄存器的内容?假设线程到运行的信号处理程序的 T 的是执行 do_stuff()函数的中间。是螺纹的 T 的'直接用于执行信号处理程序的堆栈(即信号蹦床的地址被压入的 T 的的栈和控制流程进到信号处理程序)?或者,可以采用一个单独的堆栈?它是如何工作的?

    On the second matter, what happens to the stack and register contents for the selected thread? Suppose the thread-to-run-the-signal-handler T is in the middle of executing a do_stuff() function. Is thread T's stack used directly to execute the signal handler (i.e. the address of the signal trampoline is pushed onto T's stack and control flow goes to the signal handler)? Alternatively, is a separate stack used? How does it work?

    推荐答案

    这两种解释实在是不矛盾的,如果你考虑到一个事实,即Linux的黑客往往混淆了一个线程与进程的区别,主要是由于试图pretend线程历史性的错误可以被实现为共享内存的进程。 : - )

    These two explanations really aren't contradictory if you take into account the fact that Linux hackers tend to be confused about the difference between a thread and a process, mainly due to the historical mistake of trying to pretend threads could be implemented as processes that share memory. :-)

    随着中说,解释#2是更详细的,完整和正确的。

    With that said, explanation #2 is much more detailed, complete, and correct.

    至于栈和寄存器的内容,每个线程可以注册自己备用信号处理栈,并且该过程可以针对每个信号基础哪个信号将在交替信号处理栈传递选择。中断的情况下(寄存器,信号掩码等)将被保存在 ucontext_t 的(可能是候补)结构堆叠的线头,随着蹦床回信地址。与 SA_SIGINFO 标志安装信号处理程序能够检查这个 ucontext_t 结构如果他们喜欢,但唯一的便携式的东西,他们可以用它做的是检查(也可能是修改)保存的信号掩码。 (我不知道,如果修改它是由标准的认可,但它是非常有用的,因为它使信号处理程序,以原子取代返回时被中断的code的信号掩码,例如离开阻断,因此它可以信号再发生这种事。)

    As for the stack and register contents, each thread can register its own alternate signal-handling stack, and the process can choose on a per-signal basis which signals will be delivered on alternate signal-handling stacks. The interrupted context (registers, signal mask, etc.) will be saved in a ucontext_t structure on the (possibly alternate) stack for the thread, along with the trampoline return address. Signal handlers installed with the SA_SIGINFO flag are able to examine this ucontext_t structure if they like, but the only portable thing they can do with it is examine (and possibly modify) the saved signal mask. (I'm not sure if modifying it is sanctioned by the standard, but it's very useful because it allows the signal handler to atomically replace the interrupted code's signal mask upon return, for instance to leave the signal blocked so it can't happen again.)

    这篇关于如何在Linux上执行异步信号处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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