Linux 信号可靠性,同步,异步,多线程信号等介绍

鉴于网上超多关于Linux信号处理相关的文章,本篇关于基本的信号知识不再普及,只提出一些平时不常关注或者关注不到的一些方面:

1. 信号可靠性:此可靠指的是信号是否会排队,并不是指信号会丢失(其实在也可以理解为不排队的信号就会丢失)。SIGRTMIN以下的信号不会被排队处理,即只传递一次,如果进程已经有信号还未被处理,后面再来同样的信号即丢失,其他的不同信号还是进入排队处理。SIGRTMIN以上的信号,都会进入队列

2. 信号异步处理:进程注册的信号处理函数将在进程内核态返回用户态时被调用,相对于进程主线程或其他线程来说,是并行处理的

3. 信号同步处理:进程可以选择以同步的方式来处理信号,比如由一个特定的线程来处理

4. 多线程中的信号:对于内核来说,没有进程与线程的区分,所有都是进程;对于进程来说,所有线程共享进程地址空间,包括注册的信号,即子线程会继承主线程所注册的信号环境

5. 谁来执行信号处理函数:异步处理时由最小号的线程来处理;同步处理时由用户特定的调用同步函数的线程来执行

6. 谁可以注册信号:谁都可以注册,主线程子线程都可以;注册与处理不是相关联的

7. 在多个地方注册信号了处理函数,将怎么处理:信号处理函数将以最后调用的信号注册函数为准

关于信号API:

异步处理 sigaction:

void sig_handler(int signum)
{...
}struct sigaction action;
memset(&action, 0, sizeof(action));  
action.sa_handler = sig_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
/*  restart property */
action.sa_flags |= SA_RESTART;
sigaction(SIGUSR2, &action, NULL);

同步处理 sigwaitinfo:

void* sub_sig_handle_thread()
{sigset_t   waitset, oset;siginfo_t  info;int        rc;pthread_t  ppid = pthread_self();pthread_detach(ppid);sigemptyset(&waitset);sigaddset(&waitset, SIGRTMIN);sigaddset(&waitset, SIGUSR1);while (1)  {rc = sigwaitinfo(&waitset, &info);if (rc != -1) {printf("<--sigwaitinfo() fetch the signal %d\n", rc);sig_handler(info.si_signo);} else {printf("<--sigwaitinfo() return err: %d; %s\n", errno, strerror(errno));}}
}

在某个独立的线程中,循环调用sigwaitinfo检测是否有未处理的信号,有则主动调用信号处理函数

多线程信号继承:

1. 每个线程都有自己独立的signal mask,但所有线程共享进程的signal action

2. 主线程调用pthread_sigmask创建信号屏蔽策略,将被所有子线程继承

3. pthread_kill 发送的目的线程需要与调用线程再同一个进程之中

4. 跨进程发送信号使用kill 指定进程PID

5. 同步处理时,子线程sigaddset的信号必须是主线程sigaddset的子集

同步、异步处理共存:

1. 同样的信号同时都被同步异步所注册,同步优先级更高,将以同步方式处理

2. 不同信号可以被不同方式处理,比如同步处理SIGUSR1的同时可以异步处理SIGUSR2

测试例程:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include #define MUTIL_THREAD    1void sig_handler(int signum)
{static int j = 0;static int k = 0;pthread_t  sig_ppid = pthread_self(); // used to show which thread the signal is handled in.printf("<-- get sig %d\n", signum);if (signum == SIGUSR1) {printf("<--thread %ld, receive SIGUSR1 No. %d\n", sig_ppid, j);j++;//SIGRTMIN should not be considered constants from userland, //there is compile error when use switch case} else if (signum == SIGRTMIN) {printf("<--thread %ld, receive SIGRTMIN No. %d\n", sig_ppid, k);k++;} else if (signum == SIGUSR2) {printf("<--thread %ld, receive SIGUSR2\n", sig_ppid);}
}void* worker_thread()
{pthread_t  ppid = pthread_self();pthread_detach(ppid);int ret = 0;struct timeval tv = {10, 0}; //10swhile (1) {printf("I'm thread %ld, I'm alive\n", ppid);tv.tv_sec = 10;ret = select(0, NULL, NULL, NULL, &tv);if(ret < 0)printf("thread %ld interrupt by signal\n", ppid);//sleep(10);}
}void* sigmgr_thread()
{sigset_t   waitset, oset;siginfo_t  info;int        rc;pthread_t  ppid = pthread_self();pthread_detach(ppid);#if MUTIL_THREADsigemptyset(&waitset);sigaddset(&waitset, SIGRTMIN);sigaddset(&waitset, SIGUSR1);
#elsestruct sigaction action;memset(&action, 0, sizeof(action));  action.sa_handler = sig_handler;sigemptyset(&action.sa_mask);action.sa_flags = 0;/*  restart property */action.sa_flags |= SA_RESTART;sigaction(SIGUSR2, &action, NULL);
#endifwhile (1)  {rc = sigwaitinfo(&waitset, &info);if (rc != -1) {printf("<--sigwaitinfo() fetch the signal %d\n", rc);sig_handler(info.si_signo);} else {printf("<--sigwaitinfo() return err: %d; %s\n", errno, strerror(errno));}}
}int main()
{sigset_t bset, oset;int             i;pid_t           pid = getpid();pthread_t       ppid[7];struct sigaction action;memset(&action, 0, sizeof(action));  action.sa_handler = sig_handler;sigemptyset(&action.sa_mask);action.sa_flags = 0;/*  restart property */action.sa_flags |= SA_RESTART;#if MUTIL_THREADsigaddset(&action.sa_mask, SIGRTMIN);sigaddset(&action.sa_mask, SIGUSR1);   //handle by sub-thread
//    sigaddset(&action.sa_mask, SIGUSR2);if(pthread_sigmask(SIG_BLOCK, &action.sa_mask, &oset) != 0)printf("!! Set pthread mask failed\n");sigaction(SIGUSR2, &action, NULL); //handle by main
#elsesigaction(SIGUSR1, &action, NULL);sigaction(SIGRTMIN, &action, NULL); //only main
#endifprintf("main pid %d, %ld\n", pid, pthread_self());// Create the dedicated thread sigmgr_thread() which will handle // SIGUSR1 and SIGRTMIN synchronouslypthread_create(&ppid[0], NULL, sigmgr_thread, NULL);// Create 5 worker threads, which will inherit the thread mask of// the creator main threadfor (i = 0; i < 5; i++) {pthread_create(&ppid[i+1], NULL, worker_thread, NULL);}//wait sub-thread to regist signalsleep(3);// send out 50 SIGUSR1 and SIGRTMIN signalsfor (i = 0; i < 5; i++) {printf("\n-->main thread, send SIGUSR1 No. %d\n", i);kill(pid, SIGUSR1);printf("-->main thread, send SIGRTMIN No. %d\n", i);kill(pid, SIGRTMIN);printf("-->main thread, send SIGUSR2 No. %d\n", i);kill(pid, SIGUSR2);sleep(1);}struct timeval tv = {50, 0};int ret = select(0, NULL, NULL, NULL, &tv);if(ret < 0)printf("main interrupt by signal\n");return 0;
}

运行结果:

main pid 12018, 140114995099392
I'm thread 140114978449152, I'm alive
I'm thread 140114970056448, I'm alive
I'm thread 140114953271040, I'm alive
I'm thread 140114944878336, I'm alive
I'm thread 140114961663744, I'm alive

-->main thread, send SIGUSR1 No. 0
-->main thread, send SIGRTMIN No. 0
-->main thread, send SIGUSR2 No. 0
<-- get sig 12
<--thread 140114995099392, receive SIGUSR2    --->主线程处理USR2
<--sigwaitinfo() fetch the signal 10
<-- get sig 10
<--thread 140114986841856, receive SIGUSR1 No. 0   -->同步处理信号
<--sigwaitinfo() fetch the signal 34
<-- get sig 34
<--thread 140114986841856, receive SIGRTMIN No. 0   -->同步处理信号

-->main thread, send SIGUSR1 No. 1
-->main thread, send SIGRTMIN No. 1
-->main thread, send SIGUSR2 No. 1
<--sigwaitinfo() fetch the signal 10
<-- get sig 10
<--thread 140114986841856, receive SIGUSR1 No. 1
<--sigwaitinfo() fetch the signal 34
<-- get sig 34
<--thread 140114986841856, receive SIGRTMIN No. 1
<-- get sig 12
<--thread 140114995099392, receive SIGUSR2


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部