处理异步信号在多线程程序 [英] Dealing With Asynchronous Signals In Multi Threaded Program

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

问题描述

Linux的编程接口本书中提到的方法与异步信号在多线程程序进行处理:


  

      
  • 所有的线程阻塞所有异步信号的过程
      可能会收到。最简单的方法来做到
      这是为了阻止在信号
      任何其他线程之前主线程
      创建。每个随后创建
      线程将继承主副本
      线程的信号掩码。

  •   
  • 创建,使用接收输入信号的单一专用线程
       sigwaitinfo() sigtimedwait()
      调用sigwait()

  •   

  
  

这种方法的优点在于
  异步产生的信号是
  同步接收。由于接受
  输入信号,专用线
  可以安全地修改共享变量
  (下互斥控制)和呼叫
  非异步安全功能。它也可以
  信号条件变量和emply
  其他线程和进程间通信
  和同步机制。


现在的问题:


  1. 当内核想传递信号,它可以选择内部处理任意一个线程。从那里可以知道传送信号到专用的线程?

  2. 的pthread API是非aync安全功能。所以我们如何能够讯号处理器中使用它们?


解决方案

在内核提供了一个过程导向的信号,它选择一个线程的不具有信号封锁的。这意味着,它从未选择的任何除了从信号处理线程(就像它具有它被阻止在 sigwaitinfo()或相似而畅通的信号)的螺纹的。换句话说:内核知道哪里来传递信号,因为你已经安排好的事情,这样的信号处理线程是以往任何时候都允许将信号传送到唯一的线索

您做的的使用pthreads的API,或者在信号处理程序的任何非异步信号安全功能。概述该解决方案不处理信号处理程序中的信号 - 它处理信号处理线程的正常执行流程中的信号后, sigwaitinfo()的回报。这允许它访问非异步信号安全的功能,这是整点。

The Linux Programming Interface Book has mentioned a method for dealing with asynchronous signals in a multi threaded program:

  • All threads block all of the asynchronous signals that the process might receive. The simplest way to do this is to block the signals in the main thread before any other thread are created. Each subsequently created thread will inherit a copy of the main thread's signal mask.
  • create a single dedicated thread that accepts incoming signals using sigwaitinfo(), sigtimedwait() or sigwait().

The advantage of this approach is that asynchronously generated signals are received synchronously. As it accepts incoming signals, the dedicated thread can safely modify shared variables (under mutex control) and call non-async-safe functions. It can also signal condition variables, and emply other thread and process communication and synchronization mechanisms.

Now the questions:

  1. when kernel wants to deliver signals it choose one of the threads inside process arbitrary. from where it can know to deliver signal to the dedicated thread?
  2. pthread API is non-aync-safe functions. so how we can use them inside signal handler?

解决方案

When the kernel delivers a process-directed signal, it chooses one of the threads that does not have the signal blocked. This means that it never chooses any of the threads apart from the signal-handling thread (which acts like it has the signal unblocked while it is blocked in sigwaitinfo() or similar). In other words: the kernel knows where to deliver the signal, because you have arranged things such that the signal-handling thread is the only thread that is ever allowed to deliver the signal to.

You do not use the pthreads API, or any non-async-signal-safe functions in a signal handler. The solution outlined does not handle the signals within signal handlers - it handles the signals within the normal execution flow of the signal-handling thread, after sigwaitinfo() returns. This allows it to access non-async-signal-safe functions, which is the whole point.

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

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