详细解释signal和sigaction以及SIG_BLOCK

http://blog.youkuaiyun.com/beginning1126/article/details/8680757

signal,此函数相对简单一些,给定一个信号,给出信号处理函数则可,当然,函数简单,其功能也相对简单许多,简单给出个函数例子如下:

[cpp]  view plain  copy
  1.  1 #include <signal.h>  
  2.  2 #include <stdio.h>  
  3.  3 #include <unistd.h>  
  4.  4   
  5.  5 void ouch(int sig)  
  6.  6 {  
  7.  7     printf("I got signal %d\n", sig);  
  8.  8     // (void) signal(SIGINT, SIG_DFL);  
  9.  9     //(void) signal(SIGINT, ouch);  
  10. 10   
  11. 11 }  
  12. 12   
  13. 13   
  14. 14   
  15. 15 int main()  
  16. 16 {  
  17. 17     (void) signal(SIGINT, ouch);  
  18. 18   
  19. 19     while(1)  
  20. 20     {  
  21. 21         printf("hello world...\n");  
  22. 22         sleep(1);  
  23. 23     }  
  24. 24 }  
当然,实际运用中,需要对不同到signal设定不同的到信号处理函数,SIG_IGN忽略/SIG_DFL默认,这俩宏也可以作为信号处理函数。同时SIGSTOP/SIGKILL这俩信号无法捕获和忽略。注意,经过实验发现,signal函数也会堵塞当前正在处理的signal,但是没有办法阻塞其它signal,比如正在处理SIG_INT,再来一个SIG_INT则会堵塞,但是来SIG_QUIT则会被其中断,如果SIG_QUIT有处理,则需要等待SIG_QUIT处理完了,SIG_INT才会接着刚才处理。


sigaction,这个相对麻烦一些,函数原型如下:

int sigaction(int sig, const struct sigaction *act, struct sigaction *oact);

函数到关键就在于struct sigaction

[cpp]  view plain  copy
  1. stuct sigaction  
  2. {  
  3.       void (*)(int) sa_handle;  
  4.       sigset_t sa_mask;  
  5.       int sa_flags;  
  6. }  

[cpp]  view plain  copy
  1. 1 #include <signal.h>  
  2.   2 #include <stdio.h>  
  3.   3 #include <unistd.h>  
  4.   4   
  5.   5   
  6.   6 void ouch(int sig)  
  7.   7 {  
  8.   8     printf("oh, got a signal %d\n", sig);  
  9.   9   
  10.  10     int i = 0;  
  11.  11     for (i = 0; i < 5; i++)  
  12.  12     {  
  13.  13         printf("signal func %d\n", i);  
  14.  14         sleep(1);  
  15.  15     }  
  16.  16 }  
  17.  17   
  18.  18   
  19.  19 int main()  
  20.  20 {  
  21.  21     struct sigaction act;  
  22.  22     act.sa_handler = ouch;  
  23.  23     sigemptyset(&act.sa_mask);  
  24.  24     sigaddset(&act.sa_mask, SIGQUIT);  
  25.  25     // act.sa_flags = SA_RESETHAND;  
  26.  26     // act.sa_flags = SA_NODEFER;  
  27.  27     act.sa_flags = 0;  
  28.  28   
  29.  29     sigaction(SIGINT, &act, 0);  
  30.  30   
  31.  31   
  32.  32     struct sigaction act_2;  
  33.  33     act_2.sa_handler = ouch;  
  34.  34     sigemptyset(&act_2.sa_mask);  
  35.  35     act.sa_flags = 0;  
  36.  36     sigaction(SIGQUIT, &act_2, 0);  
  37.  37   
  38.         while(1)  
  39.         {  
  40.              sleep(1);  
  41.         }  
  42.  38     return;  
  43.   
  44.     }  

1. 阻塞,sigaction函数有阻塞的功能,比如SIGINT信号来了,进入信号处理函数,默认情况下,在信号处理函数未完成之前,如果又来了一个SIGINT信号,其将被阻塞,只有信号处理函数处理完毕,才会对后来的SIGINT再进行处理,同时后续无论来多少个SIGINT,仅处理一个SIGINT,sigaction会对后续SIGINT进行排队合并处理。

2. sa_mask,信号屏蔽集,可以通过函数sigemptyset/sigaddset等来清空和增加需要屏蔽的信号,上面代码中,对信号SIGINT处理时,如果来信号SIGQUIT,其将被屏蔽,但是如果在处理SIGQUIT,来了SIGINT,则首先处理SIGINT,然后接着处理SIGQUIT。

3. sa_flags如果取值为0,则表示默认行为。还可以取如下俩值,但是我没觉得这俩值有啥用。

SA_NODEFER,如果设置来该标志,则不进行当前处理信号到阻塞

SA_RESETHAND,如果设置来该标志,则处理完当前信号后,将信号处理函数设置为SIG_DFL行为


下面单独来讨论一下信号屏蔽,记住是屏蔽,不是消除,就是来了信号,如果当前是block,则先不传递给当前进程,但是一旦unblock,则信号会重新到达。

[cpp]  view plain  copy
  1. #include <signal.h>  
  2. #include <stdio.h>  
  3. #include <unistd.h>  
  4.   
  5.   
  6.   
  7. static void sig_quit(int);  
  8.   
  9. int main (void) {  
  10.     sigset_t new, old, pend;  
  11.       
  12.     signal(SIGQUIT, sig_quit);  
  13.   
  14.     sigemptyset(&new);  
  15.     sigaddset(&new, SIGQUIT);  
  16.     sigprocmask(SIG_BLOCK, &new, &old);  
  17.   
  18.     sleep(5);  
  19.   
  20.     printf("SIGQUIT unblocked\n");  
  21.     sigprocmask(SIG_SETMASK, &old, NULL);  
  22.   
  23.     sleep(50);  
  24.     return 1;  
  25. }  
  26.   
  27. static void sig_quit(int signo) {  
  28.     printf("catch SIGQUIT\n");  
  29.     signal(SIGQUIT, SIG_DFL);  
  30. }  

gcc -g -o mask mask.c 

./mask

========这个地方按多次ctrl+\

SIGQUIT unblocked

catch SIGQUIT
Quit (core dumped)

======================

注意观察运行结果,在sleep的时候,按多次ctrl+\,由于sleep之前block了SIG_QUIT,所以无法获得SIG_QUIT,但是一旦运行sigprocmask(SIG_SETMASK, &old, NULL);则unblock了SIG_QUIT,则之前发送的SIG_QUIT随之而来。

由于信号处理函数中设置了DFL,所以再发送SIG_QUIT,则直接coredump




















OpenWRT Linux 是一个针对嵌入式设备的轻量级Linux发行版,它通常用于路由器物联网设备。在OpenWRT上进行多线程并发操作并利用`signal`处理机制来控制特定线程的挂起释放,你可以参考下面这个简单的C语言示例: ```c #include <stdio.h> #include <pthread.h> #include <signal.h> #define NUM_THREADS 5 #define THREAD_NAME_FORMAT "Thread %d" // Signal handler function to be installed for our custom signal void sig_handler(int signum) { if (signum == SIGUSR1) { // Custom signal (e.g., SIGUSR1) printf("Received signal, pausing thread...\n"); pthread_mutex_lock(&mutex); // Acquire the mutex to synchronize access paused_threads++; // Increment the paused threads counter if (paused_threads == NUM_THREADS) { printf("All threads paused, releasing mutex and exiting...\n"); pthread_cond_signal(&cond); // Notify main thread to continue } pthread_mutex_unlock(&mutex); } else { printf("Unknown signal received.\n"); } } int main() { pthread_t threads[NUM_THREADS]; int paused_threads = 0; pthread_mutex_init(&mutex, NULL); // Initialize a mutex pthread_cond_init(&cond, NULL); // Initialize a condition variable // Install our custom signal handler struct sigaction sa; sa.sa_handler = sig_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sigaction(SIGUSR1, &sa, NULL); for (int i = 0; i < NUM_THREADS; ++i) { const char *thread_name = THREAD_NAME_FORMAT, *name = thread_name + strlen(thread_name) - 2; pthread_create(&threads[i], NULL, worker_thread, (void*)name); } // Main loop: Wait for all threads to finish or a signal is caught while (!all_threads_done()) { pthread_cond_wait(&cond, &mutex); // Wait until signaled to continue printf("Main thread checking status, %d of %d threads are running\n", running_threads, NUM_THREADS); } // Clean up resources for (int i = 0; i < NUM_THREADS; ++i) { pthread_join(threads[i], NULL); } pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0; } // Worker thread function that performs some work and listens for signals void* worker_thread(void *arg) { char *thread_name = arg; printf("Starting %s...\n", thread_name); while (1) { // Simulate work // ... [Your actual work here] // Check if we should pause signal(SIGUSR1, SIG_IGN); // Block SIGUSR1 temporarily if (paused_threads > 0) { printf("%s is paused.\n", thread_name); pthread_mutex_lock(&mutex); --paused_threads; if (paused_threads == 0) { printf("%s resumed after other threads.\n", thread_name); signal(SIGUSR1, sig_handler); // Reactivate signal handling pthread_mutex_unlock(&mutex); } } else { // Normal work cycle // ... [Continue your work] } } } // Helper functions to check thread statuses bool all_threads_done() { // Implement logic to check if all threads have finished their tasks // This could involve checking thread join status or a shared flag // For simplicity, let's assume it always returns false in this example return false; } int running_threads = 0;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值