linux中使用pthread_kill函数测试线程是否存活的例子
2011-08-05 21:48
721 查看
http://hi.baidu.com/abcserver/blog/item/31e469f0cda3eaa7a50f52c5.html
/******************************* pthread_kill.c *******************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
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);
}
编译:gcc -o pthread_kill -lpthread pthread_kill.c
运行:./pthread_kill
///////////////////////// 运行结果 /////////////////////////////
线程1(ID:0xb7e95b90)退出。
ID为0xb7e95b90的线程不存在或者已经退出。
ID为0xb7694b90的线程目前仍然存活。
/******************************* pthread_kill.c *******************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
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);
}
编译:gcc -o pthread_kill -lpthread pthread_kill.c
运行:./pthread_kill
///////////////////////// 运行结果 /////////////////////////////
线程1(ID:0xb7e95b90)退出。
ID为0xb7e95b90的线程不存在或者已经退出。
ID为0xb7694b90的线程目前仍然存活。
相关文章推荐
- linux中使用pthread_kill函数测试线程是否存活的例子
- LINUX中使用PTHREAD_KILL函数测试线程是否存活的例子
- linux中使用pthread_kill函数测试线程是否存活的例子
- linux中使用pthread_kill函数测试线程是否存活的例子
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活 .
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活
- linux中使用pthread_kill函数测试线程是否存活
- 对已经detach的线程采用pthread_kill检测线程是否存活时会有问题
- pthread_kill函数检测一个线程是否还活着的程序
- Linux下c开发 之 线程通信与pthread_cond_wait()的使用
- Linux 线程通信与pthread_cond_wait()的使用
- [Linux线程]使用pthread_create函数的arg参数
- pthread 使用笔记2 判断线程是否存在