Chinaunix首页 | 论坛 | 博客
  • 博客访问: 480758
  • 博文数量: 144
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1190
  • 用 户 组: 普通用户
  • 注册时间: 2013-10-08 20:16
文章分类

全部博文(144)

文章存档

2017年(1)

2015年(5)

2014年(108)

2013年(30)

我的朋友

分类: LINUX

2013-10-08 20:37:41

对于多线程编程时候,对于signal传递的信号处理函数对于各个线程是共享的,虽然各个线程可以设置自己的屏蔽字。如下面所示代码。两个线程中,每个线程有一个signal接受信号,但是实际上执行时每个线程会执行两个signal信号处理函数。 
也可以用  pthread_sigmask(SIG_SETMASK, &set, NULL);设置自己线程的屏蔽字,不接收某个信号。注意::如果没有设置屏蔽字,并且也没有设置signal函数接收SIGUSR1与
SIGUSR12时,用kill发送信号会导致进程直接退出。
#include
#include

#include
#include
#include

void thread_handler_first();
void thread_handler_second();
void sign_handler(int sign);

int main()
{
    pthread_t thread_first, thread_second;
    if(pthread_create(&thread_first, NULL, (void *)*thread_handler_first, NULL) != 0)
    {
      printf("create child thread fail\n"); exit(EXIT_FAILURE);
    }
    else
      printf("create first child thread success\n");
    if(pthread_create(&thread_second, NULL, (void *)*thread_handler_second, NULL) != 0)
    {
      printf("create child thread fail\n"); exit(EXIT_FAILURE);
    }
    else
      printf("create second child thread success\n");
    sleep(2);
    printf("Now the main thread prepare to send SIGUSR1 and SIGUSR2 to first child thread\n");
    if(pthread_kill(thread_first, SIGUSR1) || pthread_kill(thread_first, SIGUSR2))
    {
      printf("send SIGUSR1 and SIGUSR2 to first child thread fail\n"); exit(EXIT_FAILURE);
    }
    else
     printf("send SIGUSR1 and SIGUSR2 to first child thread success\n");
    sleep(1);
    printf("Now the main thread prepare to send SIGUSR1 and SIGUSR2 to second child thread\n");
    if(pthread_kill(thread_second, SIGUSR1) || pthread_kill(thread_second, SIGUSR2))
    {
      printf("send SIGUSR1 and SIGUSR2 to second child thread fail\n"); exit(EXIT_FAILURE);
    }
    else
     printf("send SIGUSR1 and SIGUSR2 to second child thread success\n");
    sleep(1);

    printf("Now the main thread prepare to send SIGKILL to both first and second child thread\n");
    if(pthread_kill(thread_first, SIGKILL) != 0)
    {
      printf("send SIGKILL to first child thread fail\n"); exit(EXIT_FAILURE);
    }
    else
     printf("send SIGKILL to first child thread success\n");
    if(pthread_kill(thread_first, SIGKILL) != 0)
    {
      printf("send SIGKILL to second child thread fail\n"); exit(EXIT_FAILURE);
    }
    else
     printf("send SIGKILL to second child thread success\n");
    sleep(1);
    pthread_join(thread_first, NULL);
    pthread_join(thread_second, NULL);
    exit(EXIT_SUCCESS); 
}

void thread_handler_first()
{
    int i;
    __sigset_t set;
    signal(SIGUSR1, sign_handler);
    sigfillset(&set);
    sigdelset(&set, SIGUSR1);
 sigdelset(&set, SIGUSR2);
    pthread_sigmask(SIG_SETMASK, &set, NULL);
    for(i = 0; i < 5; i++)
    {
      printf("this is first thread:%u...\n", pthread_self()); pause();
    }
}

void thread_handler_second()
{
    int i;
    signal(SIGUSR2, sign_handler);
//    signal(SIGUSR1, sign_handler);
    for(i = 0; i < 5; i++)
    {
      printf("This is second thread:%u...\n",pthread_self()); pause();
    }
}

void sign_handler(int sign)
{
     printf("\nthread:%u, receive signal:%u---\n", pthread_self(), sign);
}


执行结果如下:

create first child thread success
create second child thread success
This is second thread:3069864816...
this is first thread:3078257520...
Now the main thread prepare to send SIGUSR1 and SIGUSR2 to first child thread
send SIGUSR1 and SIGUSR2 to first child thread success

thread:3078257520, receive signal:12---

thread:3078257520, receive signal:10---
this is first thread:3078257520...
Now the main thread prepare to send SIGUSR1 and SIGUSR2 to second child thread
send SIGUSR1 and SIGUSR2 to second child thread success

thread:3069864816, receive signal:12---

thread:3069864816, receive signal:10---
This is second thread:3069864816...
Now the main thread prepare to send SIGKILL to both first and second child thread
Killed


阅读(1765) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~