POSIX 线程 – pthread_sigmask
2006-04-12 14:23
316 查看
POSIX 线程 – pthread_sigmask
在多线程的程序里,希望只在主线程中处理信号,可以使用
函数:
int pthread_sigmask (int how,
const sigset_t *set,
sigset_t *oset)
用作在主调线程里控制信号掩码。
How:
SIG_BLOCK: 结果集是当前集合参数集的并集
SIG_UNBLOCK: 结果集是当前集合参数集的差集
SIG_SETMASK: 结果集是由参数集指向的集
头文件: <signal.h>
错误: [EINVAL] how不是已定义值
提示: 除非信号在所有的线程里都阻塞,否则总能将异步信号传输给这个进程。
例子:
#include <pthread.h>
#include <stdio.h>
#include <sys/signal.h>
#define NUMTHREADS 3
void sighand(int signo);
void *threadfunc(void *parm)
{
pthread_t tid = pthread_self();
int rc;
printf("Thread %u entered/n", tid);
rc = sleep(30);
printf("Thread %u did not get expected results! rc=%d/n", tid, rc);
return NULL;
}
void *threadmasked(void *parm)
{
pthread_t tid = pthread_self();
sigset_t mask;
int rc;
printf("Masked thread %lu entered/n", tid);
sigfillset(&mask); /* Mask all allowed signals */
rc = pthread_sigmask(SIG_BLOCK, &mask, NULL);
if (rc != 0)
{
printf("%d, %s/n", rc, strerror(rc));
return NULL;
}
rc = sleep(15);
if (rc != 0)
{
printf("Masked thread %lu did not get expected results! "
"rc=%d /n", tid, rc);
return NULL;
}
printf("Masked thread %lu completed masked work/n",
tid);
return NULL;
}
int main(int argc, char **argv)
{
int rc;
int i;
struct sigaction actions;
pthread_t threads[NUMTHREADS];
pthread_t maskedthreads[NUMTHREADS];
printf("Enter Testcase - %s/n", argv[0]);
printf("Set up the alarm handler for the process/n");
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = sighand;
rc = sigaction(SIGALRM,&actions,NULL);
printf("Create masked and unmasked threads/n");
for(i=0; i<NUMTHREADS; ++i)
{
rc = pthread_create(&threads[i], NULL, threadfunc, NULL);
if (rc != 0)
{
printf("%d, %s/n", rc, strerror(rc));
return -1;
}
rc = pthread_create(&maskedthreads[i], NULL, threadmasked, NULL);
if (rc != 0)
{
printf("%d, %s/n", rc, strerror(rc));
return -1;
}
}
sleep(3);
printf("Send a signal to masked and unmasked threads/n");
for(i=0; i<NUMTHREADS; ++i)
{
rc = pthread_kill(threads[i], SIGALRM);
rc = pthread_kill(maskedthreads[i], SIGALRM);
}
printf("Wait for masked and unmasked threads to complete/n");
for(i=0; i<NUMTHREADS; ++i) {
rc = pthread_join(threads[i], NULL);
rc = pthread_join(maskedthreads[i], NULL);
}
printf("Main completed/n");
return 0;
}
void sighand(int signo)
{
pthread_t tid = pthread_self();
printf("Thread %lu in signal handler/n",
tid);
return;
}
程序返回:
Enter Testcase - ./pthread_sigmask_test
Set up the alarm handler for the process
Create masked and unmasked threads
Thread 3086597040 entered
Masked thread 3076107184 entered
Thread 3065617328 entered
Masked thread 3055127472 entered
Thread 3044637616 entered
Masked thread 3034147760 entered
Send a signal to masked and unmasked threads
Wait for masked and unmasked threads to complete
Thread 3086597040 in signal handler
Thread 3086597040 did not get expected results! rc=27
Thread 3065617328 in signal handler
Thread 3065617328 did not get expected results! rc=27
Thread 3044637616 in signal handler
Thread 3044637616 did not get expected results! rc=27
Masked thread 3076107184 completed masked work
Masked thread 3055127472 completed masked work
Masked thread 3034147760 completed masked work
Main completed
在多线程的程序里,希望只在主线程中处理信号,可以使用
函数:
int pthread_sigmask (int how,
const sigset_t *set,
sigset_t *oset)
用作在主调线程里控制信号掩码。
How:
SIG_BLOCK: 结果集是当前集合参数集的并集
SIG_UNBLOCK: 结果集是当前集合参数集的差集
SIG_SETMASK: 结果集是由参数集指向的集
头文件: <signal.h>
错误: [EINVAL] how不是已定义值
提示: 除非信号在所有的线程里都阻塞,否则总能将异步信号传输给这个进程。
例子:
#include <pthread.h>
#include <stdio.h>
#include <sys/signal.h>
#define NUMTHREADS 3
void sighand(int signo);
void *threadfunc(void *parm)
{
pthread_t tid = pthread_self();
int rc;
printf("Thread %u entered/n", tid);
rc = sleep(30);
printf("Thread %u did not get expected results! rc=%d/n", tid, rc);
return NULL;
}
void *threadmasked(void *parm)
{
pthread_t tid = pthread_self();
sigset_t mask;
int rc;
printf("Masked thread %lu entered/n", tid);
sigfillset(&mask); /* Mask all allowed signals */
rc = pthread_sigmask(SIG_BLOCK, &mask, NULL);
if (rc != 0)
{
printf("%d, %s/n", rc, strerror(rc));
return NULL;
}
rc = sleep(15);
if (rc != 0)
{
printf("Masked thread %lu did not get expected results! "
"rc=%d /n", tid, rc);
return NULL;
}
printf("Masked thread %lu completed masked work/n",
tid);
return NULL;
}
int main(int argc, char **argv)
{
int rc;
int i;
struct sigaction actions;
pthread_t threads[NUMTHREADS];
pthread_t maskedthreads[NUMTHREADS];
printf("Enter Testcase - %s/n", argv[0]);
printf("Set up the alarm handler for the process/n");
memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = sighand;
rc = sigaction(SIGALRM,&actions,NULL);
printf("Create masked and unmasked threads/n");
for(i=0; i<NUMTHREADS; ++i)
{
rc = pthread_create(&threads[i], NULL, threadfunc, NULL);
if (rc != 0)
{
printf("%d, %s/n", rc, strerror(rc));
return -1;
}
rc = pthread_create(&maskedthreads[i], NULL, threadmasked, NULL);
if (rc != 0)
{
printf("%d, %s/n", rc, strerror(rc));
return -1;
}
}
sleep(3);
printf("Send a signal to masked and unmasked threads/n");
for(i=0; i<NUMTHREADS; ++i)
{
rc = pthread_kill(threads[i], SIGALRM);
rc = pthread_kill(maskedthreads[i], SIGALRM);
}
printf("Wait for masked and unmasked threads to complete/n");
for(i=0; i<NUMTHREADS; ++i) {
rc = pthread_join(threads[i], NULL);
rc = pthread_join(maskedthreads[i], NULL);
}
printf("Main completed/n");
return 0;
}
void sighand(int signo)
{
pthread_t tid = pthread_self();
printf("Thread %lu in signal handler/n",
tid);
return;
}
程序返回:
Enter Testcase - ./pthread_sigmask_test
Set up the alarm handler for the process
Create masked and unmasked threads
Thread 3086597040 entered
Masked thread 3076107184 entered
Thread 3065617328 entered
Masked thread 3055127472 entered
Thread 3044637616 entered
Masked thread 3034147760 entered
Send a signal to masked and unmasked threads
Wait for masked and unmasked threads to complete
Thread 3086597040 in signal handler
Thread 3086597040 did not get expected results! rc=27
Thread 3065617328 in signal handler
Thread 3065617328 did not get expected results! rc=27
Thread 3044637616 in signal handler
Thread 3044637616 did not get expected results! rc=27
Masked thread 3076107184 completed masked work
Masked thread 3055127472 completed masked work
Masked thread 3034147760 completed masked work
Main completed
相关文章推荐
- POSIX 线程 – pthread_sigmask
- POSIX 线程 – pthread_sigmask
- POSIX 线程 – pthread_sigmask
- POSIX 线程 – pthread_sigmask
- POSIX 线程 – pthread_sigmask
- Linux 多线程编程( POSIX )( 二 )----->代码区 ( pthread_attr_t 线程属性实例 )
- [C++]POSIX Pthread和Windows API的线程创建时的一处不同
- posix多线程有感--线程高级编程(pthread_once)
- POSIX定时器timer_create()以及线程中的gettid() 和pthread_self()
- POSIX 线程API 之 pthread_barrier_xxx() 使用例子
- Linux 多线程编程( POSIX )( 二 )----->pthread_attr_t 线程属性
- 关于C++中posix pthread线程函数在类中封装的问题
- posix多线程有感--线程高级编程(pthread_once)
- posix多线程有感--线程高级编程(线程属性pthread_attr_t)---实时调度
- 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)
- posix多线程有感--线程高级编程(pthread_once)
- posix多线程有感--线程高级编程(线程属性pthread_attr_t)---实时调度
- posix 线程(一):线程模型、pthread 系列函数 和 简单多线程服务器端程序
- 关于C++中posix pthread线程函数在类中封装的问题
- 在指定的线程接收信号 pthread_sigmask