signal_kill_test.c
- #include<stdio.h>
- #include<signal.h>
- #include<pthread.h>
- void thread()
- {
- signal(SIGUSR1, pthread_exit);
- int i = 0;
- while(1)
- {
- printf("thread%d: %d second\n",pthread_self(), i);
- sleep(5);
- i += 5;
- }
- }
- void main()
- {
- pthread_t thid[3];
- pthread_create(&thid[0], NULL, (void*)thread, NULL);
- pthread_detach(thid[0]);
- pthread_create(&thid[1], NULL, (void*)thread, NULL);
- pthread_detach(thid[1]);
- sleep(6);
- int num = pthread_kill(thid[0], SIGUSR1);
- printf("send signal and return %d\n", num);
- sleep(100);
- }
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) |