Chinaunix首页 | 论坛 | 博客
  • 博客访问: 515654
  • 博文数量: 174
  • 博客积分: 8001
  • 博客等级: 中将
  • 技术积分: 1840
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-04 19:30
文章分类

全部博文(174)

文章存档

2011年(1)

2010年(24)

2009年(149)

我的朋友

分类: LINUX

2009-04-17 10:54:23

Each thread has its own signal mask, but the signal disposition is shared by all threads in the process. This means that individual threads can block signals, but when a thread modifies the action associated with a given signal, all threads share the action. Thus, if one thread chooses to ignore a given signal, another thread can undo that choice by restoring the default disposition or installing a signal handler for the signal.
你可以为每个线程设置它的掩码,但是由于signal disposition是共享的,也就是说你无法为一个线程安装自己的信号处理函数。

#include <signal.h>

int pthread_sigmask(int how, const sigset_t
 *restrict set,
                    sigset_t *restrict oset); 
Returns: 0 if OK, error number on failure


A thread can wait for one or more signals to occur by calling sigwait.

#include <signal.h>

int sigwait(const sigset_t *restrict set, int
 *restrict signop);
Returns: 0 if OK, error number on failure

The set argument specifies the set of signals for which the thread is waiting. On return, the integer to which signop points will contain the number of the signal that was delivered.

If one of the signals specified in the set is pending at the time sigwait is called, then sigwait will return without blocking. Before returning, sigwait removes the signal from the set of signals pending for the process. To avoid erroneous behavior, a thread must block the signals it is waiting for before calling sigwait. The sigwait function will atomically unblock the signals and wait until one is delivered. Before returning, sigwait will restore the thread's signal mask. If the signals are not blocked at the time that sigwait is called, then a timing window is opened up where one of the signals can be delivered to the thread before it completes its call to sigwait.

sigwait函数将等待指定的信号到来,调用时,它会自动将指定的信号从屏蔽中去除;返回前,它将恢复原来的屏蔽集。你应该在调用前屏蔽指定的信号。

The advantage to using sigwait is that it can simplify signal handling by allowing us to treat asynchronously-generated signals in a synchronous manner. We can prevent the signals from interrupting the threads by adding them to each thread's signal mask. Then we can dedicate specific threads to handling the signals. These dedicated threads can make function calls without having to worry about which functions are safe to call from a signal handler, because they are being called from normal thread context, not from a traditional signal handler interrupting a normal thread's execution.

sigwait的一个优点是使得异步信号以同步的方式被处理,即在线程的上下文中被处理。而不是传统的使用signal handler打断线程的执行。

If multiple threads are blocked in calls to sigwait for the same signal, only one of the threads will return from sigwait when the signal is delivered. If a signal is being caught (the process has established a signal handler by using sigaction, for example) and a thread is waiting for the same signal in a call to sigwait, it is left up to the implementation to decide which way to deliver the signal. In this case, the implementation could either allow sigwait to return or invoke the signal handler, but not both.

如果多个线程在等待一个信号,只有一个线程能够收到该信号。如果一个信号被一个信号处理函数俘获,同时被等待,那么它们只有一个会被满足。

向一个线程发送信号

#include <signal.h>

int pthread_kill(pthread_t thread, int signo);
Returns: 0 if OK, error number on failure

We can pass a signo value of 0 to check for existence of the thread. If the default action for a signal is to terminate the process, then sending the signal to a thread will still kill the entire process.
如果该信号的默认处理是终止进程,那么整个进程将被终止。


阅读(1254) | 评论(0) | 转发(0) |
0

上一篇:miniplayer流程架构

下一篇:内存映射

给主人留下些什么吧!~~