linux杀死指定进程中的线程,Linux C++多线程编程时如何查看一个进程中的某个线程是否存活...

#include void *func1()/*1秒钟之后退出*/

{

sleep(1);

printf("线程1(ID:0x%x)退出。/n",(unsigned int)pthread_self());

pthread_exit((void *)0);

}

void *func2()/*5秒钟之后退出*/

{

sleep(5);

printf("线程2(ID:0x%x)退出。/n",(unsigned int)pthread_self());

pthread_exit((void *)0);

}

void test_pthread(pthread_t tid) /*pthread_kill的返回值:成功(0) 线程不存在(ESRCH) 信号不合法(EINVAL)*/

{

int pthread_kill_err;

pthread_kill_err = pthread_kill(tid,0);

if(pthread_kill_err == ESRCH)

printf("ID为0x%x的线程不存在或者已经退出。/n",(unsigned int)tid);

else if(pthread_kill_err == EINVAL)

printf("发送信号非法。/n");

else

printf("ID为0x%x的线程目前仍然存活。/n",(unsigned int)tid);

}

int main()

{

int ret;

pthread_t tid1,tid2;

pthread_create(&tid1,NULL,func1,NULL);

pthread_create(&tid2,NULL,func2,NULL);

sleep(3);/*创建两个进程3秒钟之后,分别测试一下它们是否还活着*/

test_pthread(tid1);/*测试ID为tid1的线程是否存在*/

test_pthread(tid2);/*测试ID为tid2的线程是否存在*/

exit(0);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值