mutli-threads with different singnal mask demo
Posted on 2011-11-29 13:32 cornflower 阅读(367) 评论(0) 编辑 收藏 举报/*
* Howto
*1. gcc -o multi_thread_signal multi_thread_signal.c -lpthread
*2. ./multi_thread_signal &
*3. ps -aux to see the PID of multi_thread_signal application
*4. use kill -10 <PID_OF_multi_thread_signal_APP> to send signal SIGUSR1 to others thread
*5. use kill -12 <PID_OF_multi_thread_signal_APP> to send signal SIGUSR2 to ic2 thread
*6. use kiill -3 <PID_OF_multi_thread_signal_APP> to send signal SIGQUIT to spi thread
*7. use kill -2 <PID_OF_multi_thread_signal_APP> to kill all threads and end the process
* */
#include <pthread.h>
#include <stdio.h>
#include <sys/signal.h>
void spi_sghandler(int sig)
{
printf("spi_thread %ld caught signal %d\n", (long int)pthread_self(), sig);
}
void ic2_sghandler(int sig)
{
printf("ic2_thread %ld caught signal %d\n", (long int)pthread_self(), sig);
}
void others_sghandler(int sig)
{
printf("Others_thread %ld caught signal %d\n", (long int)pthread_self(), sig);
}
/*SPI thread support SIGQUIT SIGINT and signal action is */
void* spi_threads (void* unused)
{
int rc =0;
struct sigaction act;
sigset_t new;
/*register signal handler first befor signal can be action*/
act.sa_handler = spi_sghandler;
sigaction(SIGQUIT, &act, NULL);
sigaddset(&new, SIGQUIT);
pthread_sigmask(SIG_UNBLOCK, &new, NULL);
printf("It's spi %ld\n",(long int) pthread_self());
while(1)
{
rc = sleep(100);
printf("SPI wake_up%d\n",rc);
}
return NULL;
}
/*ic2 thread support SIGINT and SIGUSR2 */
void* ic2_threads (void* unused)
{
int rc;
struct sigaction act;
sigset_t new;
/*register signal handler first befor signal can be action*/
act.sa_handler = ic2_sghandler;
sigaction(SIGUSR2, &act, NULL);
sigaddset(&new, SIGUSR2);
pthread_sigmask(SIG_UNBLOCK, &new, NULL);
printf("It's ic2 %ld\n",(long int) pthread_self());
while(1)
{
rc=sleep(100);
printf("ic2 wake_up%d\n",rc);
}
return NULL;
}
/*Others thread support SIGINT and SIGUSR1 */
void* others_threads (void* unused)
{
int rc = 0;
struct sigaction act;
sigset_t new;
/*register signal handler first befor signal can be action*/
act.sa_handler = others_sghandler;
sigaction(SIGUSR1, &act, NULL);
sigaddset(&new, SIGUSR1);
pthread_sigmask(SIG_UNBLOCK, &new, NULL);
printf("Others %ld\n",(long int) pthread_self());
while(1)
{
rc=sleep(100);
printf("Others wake_up%d\n",rc);
}
return NULL;
}
/* The main program. */
int main ()
{
pthread_t thread_spi;
pthread_t thread_ic2;
pthread_t thread_others;
int rc =0;
sigset_t new;
sigfillset(&new);/*support all signal*/
sigdelset(&new, SIGINT);/*re_move SIG_INT for we try to BLOCK*/
/*Main thead Just Support SIG_INT only */
pthread_sigmask(SIG_BLOCK, &new, NULL);
/* sub-threads can inherit signal from main thread*/
pthread_create (&thread_spi, NULL, &spi_threads, NULL);
pthread_create (&thread_ic2, NULL, &ic2_threads, NULL);
pthread_create (&thread_others, NULL, &others_threads, NULL);
while(1)
{
rc=sleep(100);
/*if rc not zero, we are wakeup by signal*/
printf("Main wake_up %d\n",rc);
}
return 0;
}