Chinaunix首页 | 论坛 | 博客
  • 博客访问: 316596
  • 博文数量: 42
  • 博客积分: 451
  • 博客等级: 下士
  • 技术积分: 890
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-03 18:24
文章分类

全部博文(42)

文章存档

2015年(1)

2013年(9)

2012年(19)

2011年(13)

分类: C/C++

2012-08-15 14:59:11

signal_kill_test.c

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<signal.h>
  3. #include<pthread.h>

  4. void thread()
  5. {
  6.         signal(SIGUSR1, pthread_exit);
  7.         int i = 0;
  8.         while(1)
  9.         {
  10.                 printf("thread%d: %d second\n",pthread_self(), i);
  11.                 sleep(5);
  12.                 i += 5;
  13.         }
  14. }

  15. void main()
  16. {
  17.         pthread_t thid[3];
  18.         pthread_create(&thid[0], NULL, (void*)thread, NULL);
  19.         pthread_detach(thid[0]);
  20.         pthread_create(&thid[1], NULL, (void*)thread, NULL);
  21.         pthread_detach(thid[1]);
  22.         sleep(6);
  23.         int num = pthread_kill(thid[0], SIGUSR1);
  24.         printf("send signal and return %d\n", num);
  25.         sleep(100);
  26. }
gcc signal_kill_test.c -lpthread
./a.out
# ./a.out
thread1192220416: 0 second
thread1202710272: 0 second
thread1192220416: 5 second
thread1202710272: 5 second
send signal and return 0
thread1192220416: 10 second
thread1192220416: 15 second
thread1192220416: 20 second
thread1192220416: 25 second
thread1192220416: 30 second
thread1192220416: 35 second
thread1192220416: 40 second

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