Linux多线程之pthread_kill

int pthread_kill(thread_t tid, int sig)

将信号sig发送到由tid指定的线程,tid所指定的线程必须与调用线程在同一个进程中。如果sig为零,将执行错误检查,但并不实际发送信号。此错误检查可用来检查tid的有效性。

返回值  成功之后返回0,否则返回非零。


      1 #define _MULTI_THREADED
      2 #include <pthread.h>
      3 #include <stdio.h>
      4 #include <signal.h>
      5 #include <string.h>
      6 #include "check.h"
      7
      8 #define NUMTHREADS 3
      9 void sighand(int signo);
     10
     11 void *threadfunc(void *parm)
     12 {
     13   pthread_t             self = pthread_self();
     14   pthread_t             tid;
     15   int                   rc;
     16
     17   tid = self;
     18 //  pthread_getunique_np(&self, &tid);
     19   printf("Thread 0x%.8x %.8x entered\n", tid);
     20   errno = 0;
     21   rc = sleep(30);
     22   if (rc != 0 && errno == EINTR) {
     23     printf("Thread 0x%.8x %.8x got a signal delivered to it\n",
     24            tid);
     25     return NULL;
     26   }
     27   printf("Thread 0x%.8x %.8x did not get expected results! rc=%d, errno=%d\n",
     28          tid, rc, errno);
     29   return NULL;
     30 }
     31
     32 int main(int argc, char **argv)
     33 {
     34   int                     rc;
     35   int                     i;
     36   struct sigaction        actions;
     37   pthread_t               threads[NUMTHREADS];
     38
     39   printf("Enter Testcase - %s\n", argv[0]);
     40
     41   printf("Set up the alarm handler for the process\n");
     42   memset(&actions, 0, sizeof(actions));
     43   sigemptyset(&actions.sa_mask);
     44   actions.sa_flags = 0;
     45   actions.sa_handler = sighand;
     46
     47   rc = sigaction(SIGALRM,&actions,NULL);
     48   checkResults("sigaction\n", rc);
     49
     50   for(i=0; i<NUMTHREADS; ++i) {
     51     rc = pthread_create(&threads[i], NULL, threadfunc, NULL);
     52     checkResults("pthread_create()\n", rc);
     53   }
     54
     55   sleep(3);
     56   for(i=0; i<NUMTHREADS; ++i) {
     57     rc = pthread_kill(threads[i], SIGALRM);
     58     checkResults("pthread_kill()\n", rc);
     59   }

     60
     61   for(i=0; i<NUMTHREADS; ++i) {
     62     rc = pthread_join(threads[i], NULL);
     63     checkResults("pthread_join()\n", rc);
     64   }
     65   printf("Main completed\n");
     66   return 0;
     67 }
     68
     69 void sighand(int signo)
     70 {
     71   pthread_t             self = pthread_self();
     72   pthread_t             tid;
     73
     74   //pthread_getunique_np(&self, &tid);
     75   tid = self;
     76   printf("Thread 0x%.8x %.8x in signal handler\n",
     77          tid);
     78   return;
     79 }


编译  gcc -o pthread_kill -lpthread pthread_kill.c

运行结果

Enter Testcase - ./pthread_kill
Set up the alarm handler for the process
Thread 0xb7f0cba0 00000000 entered
Thread 0xb750bba0 00000000 entered
Thread 0xb6b0aba0 00000000 entered
Thread 0xb7f0cba0 00000000 in signal handler
Thread 0xb7f0cba0 00000000 got a signal delivered to it
Thread 0xb750bba0 00000000 in signal handler
Thread 0xb750bba0 00000000 got a signal delivered to it
Thread 0xb6b0aba0 00000000 in signal handler
Thread 0xb6b0aba0 00000000 got a signal delivered to it
Main completed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值