Linux 编程之【线程】pthread_kill

pthread_kill函数用于向指定线程发送信号,如果线程未处理信号,可能影响整个进程。参数sig为0可检查线程是否存活。使用时需注意信号处理,并在编译时链接libpthread.a。示例展示了线程间的交互和信号处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【函数原型】   
 int pthread_kill(pthread_t thread, int sig);
 
【返回值】
 0               成功
 ESRCH   线程不存在
 EINVAL   信号不合法

【说明】
向指定ID的线程发送sig信号,如果线程代码内不做处理,则按照信号默认的行为影响整个进程,
也就是说,如果你给一个线程发送了SIGQUIT,但线程却没有实现signal处理函数,则整个进程退出。

pthread_kill(threadid, SIGKILL)也一样,杀死整个进程。
如果要获得所需的处理方式,则需要在线程内注册signal(SIGKILL,sig_handler)。

所以,若参数sig的不为0,那一定要弄清到底要干什么,有必要的话,则需要在对应线程实现相应的
信号处理函数,否则,就会影响整个进程。

不过,参数sig为0是个特例,这是一个保留信号,可以用来判断线程是否活着。

pthread_t 类型的定义  typedef unsigned long int pthread_t;
由于pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用函数
pthread_create()创建线程,pthread_atfork()函数建立fork处理程序、pthread_kill()函数给线程
发送信号时,在编译时要加上 -lpthread 参数。
即,除了引用头文件 #include <pthread.h> 之外,需要使用如下命令编译:
gcc -o temp temp.c -lpthread

【示例】

<pre name="code" class="cpp">#include <stdio.h>
#include <pthread.h>
#include <errno.h>

pthread_t g_tid = 0;

static void *ptest(void *arg)
{
  int cnt = 0;
  int kill_rc;
	
  while(1)
  {
    cnt++;
		
    if(g_tid > 0)
    {
      kill_rc = pthread_kill(g_tid,0);
		  
      if(ESRCH == kill_rc)
      {
        printf("[chiild pthread] main pthread(%lu) is ESRCH(%d).\r\n",g_tid, ESRCH);
        break;    	
      }
      else if (EINVAL == kill_rc)
      {
        printf("[chiild pthread] main pthread(%lu) is EINVAL(%d).\r\n",g_tid, EINVAL);
        break;
      }
      else	
      {
        printf("[chiild pthread] main pthread(%lu) is ok(%d).\r\n",g_tid,kill_rc);
      }
    }
		
    printf("[chiild pthread] Child pthread ready to sleep 8 \r\n");
    sleep(8);
		
    if(cnt >= 2)
    {
      printf("[chiild pthread] Child pthread exit\r\n");
      break;
    }
  }

  return NULL;
}

int main(int argc, char *argv[])
{
  int kill_rc;
  pid_t pid;
  pthread_t tid = 0;

  pid = getpid();
  g_tid = pthread_self();
  printf("[main pthread] pid=%lu tid_mian=%lu \r\n", pid, g_tid);
	
  pthread_create(&tid,NULL,ptest,NULL);
  
  while(1)
  {
    kill_rc = pthread_kill(tid,0);
    if(ESRCH == kill_rc)
    {
    	printf("[main pthread] child pthread(%lu) is ESRCH(%d)\r\n",tid,ESRCH);
    	break;    	
    }
    else if (EINVAL == kill_rc)
    {
    	printf("[main pthread] child pthread(%lu) is EINVAL(%d)\r\n",tid,EINVAL);
    	break;
    }
    else	
    {
    	printf("[main pthread] child pthread(%lu) is ok(%d)\r\n",tid,kill_rc);
    }
    
    sleep(3);
  }
  
  exit(0);
}

【编译及执行】

# gcc -o temp temp.c -lpthread
# ./temp
[main pthread] pid=5815 tid_mian=3076183744 
[main pthread] child pthread(3076180800) is ok(0)
[chiild pthread] main pthread(3076183744) is ok(0).
[chiild pthread] Child pthread ready to sleep 8 
[main pthread] child pthread(3076180800) is ok(0)
[main pthread] child pthread(3076180800) is ok(0)
[chiild pthread] main pthread(3076183744) is ok(0).
[chiild pthread] Child pthread ready to sleep 8 
[main pthread] child pthread(3076180800) is ok(0)
[main pthread] child pthread(3076180800) is ok(0)
[main pthread] child pthread(3076180800) is ok(0)
[chiild pthread] Child pthread exit
[main pthread] child pthread(3076180800) is ESRCH(3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值