当前位置: 技术问答>linux和unix
pthread_create成功返回,但pthread_kill(thrdid, 0)返回说进程不存在?
来源: 互联网 发布时间:2015-11-28
本文导语: HP-UX下,pthread_create成功返回,取得线程id,但pthread_kill(thrdid, 0)返回错误说进程不存在,这是怎么回事?我实在想不明白。我的代码片断: for(...;...;...) { ....; alarm(0); ret = pthread_create(&thrID, NULL, ThreadF...
HP-UX下,pthread_create成功返回,取得线程id,但pthread_kill(thrdid, 0)返回错误说进程不存在,这是怎么回事?我实在想不明白。我的代码片断:
for(...;...;...)
{
....;
alarm(0);
ret = pthread_create(&thrID, NULL, ThreadFuncCalcV, (void *)thrParam);
printf("1 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
if(ret != 0)
fprintf(stdout, "1pthread_create fail ret = %d[thrID:%x]n", ret, thrID);
printf("2 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
alarm(2);
printf("3 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
ret = pthread_join(thrID, NULL);
printf("4 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
if(ret != 0)
fprintf(stdout, "2pthread_join fail ret = %d[thrID:%x]n", ret,thrID);
printf("5 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
alarm(0);
printf("6 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
ret = pthread_detach(thrID);
printf("7 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
if(ret != 0)
fprintf(stdout, "3pthread_detach fail ret = %d[thrID:%x]n", ret,thrID);
printf("8 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
....;
}
输出结果:
HP-UX@/home/src $ optd
1 pthread_kill(2, 0) : 0
2 pthread_kill(2, 0) : 0
3 pthread_kill(2, 0) : 0
4 pthread_kill(2, 0) : 3
5 pthread_kill(2, 0) : 3
6 pthread_kill(2, 0) : 3
7 pthread_kill(2, 0) : 3
3pthread_detach fail ret = 3[thrID:2]
8 pthread_kill(2, 0) : 3
1 pthread_kill(3, 0) : 3
2 pthread_kill(3, 0) : 3
3 pthread_kill(3, 0) : 3
4 pthread_kill(3, 0) : 3
5 pthread_kill(3, 0) : 3
6 pthread_kill(3, 0) : 3
7 pthread_kill(3, 0) : 3
3pthread_detach fail ret = 3[thrID:3]
8 pthread_kill(3, 0) : 3
1 pthread_kill(4, 0) : 3
2 pthread_kill(4, 0) : 3
3 pthread_kill(4, 0) : 3
4 pthread_kill(4, 0) : 3
5 pthread_kill(4, 0) : 3
6 pthread_kill(4, 0) : 3
7 pthread_kill(4, 0) : 3
3pthread_detach fail ret = 3[thrID:4]
8 pthread_kill(4, 0) : 3
1 pthread_kill(5, 0) : 3
2 pthread_kill(5, 0) : 3
3 pthread_kill(5, 0) : 3
4 pthread_kill(5, 0) : 3
5 pthread_kill(5, 0) : 3
6 pthread_kill(5, 0) : 3
7 pthread_kill(5, 0) : 3
3pthread_detach fail ret = 3[thrID:5]
8 pthread_kill(5, 0) : 3
1 pthread_kill(6, 0) : 3
2 pthread_kill(6, 0) : 3
3 pthread_kill(6, 0) : 3
4 pthread_kill(6, 0) : 3
5 pthread_kill(6, 0) : 3
6 pthread_kill(6, 0) : 3
7 pthread_kill(6, 0) : 3
3pthread_detach fail ret = 3[thrID:6]
8 pthread_kill(6, 0) : 3
1 pthread_kill(7, 0) : 0
2 pthread_kill(7, 0) : 0
3 pthread_kill(7, 0) : 0
for(...;...;...)
{
....;
alarm(0);
ret = pthread_create(&thrID, NULL, ThreadFuncCalcV, (void *)thrParam);
printf("1 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
if(ret != 0)
fprintf(stdout, "1pthread_create fail ret = %d[thrID:%x]n", ret, thrID);
printf("2 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
alarm(2);
printf("3 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
ret = pthread_join(thrID, NULL);
printf("4 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
if(ret != 0)
fprintf(stdout, "2pthread_join fail ret = %d[thrID:%x]n", ret,thrID);
printf("5 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
alarm(0);
printf("6 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
ret = pthread_detach(thrID);
printf("7 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
if(ret != 0)
fprintf(stdout, "3pthread_detach fail ret = %d[thrID:%x]n", ret,thrID);
printf("8 pthread_kill(%x, 0) : %dn", thrID, pthread_kill(thrID, 0));
....;
}
输出结果:
HP-UX@/home/src $ optd
1 pthread_kill(2, 0) : 0
2 pthread_kill(2, 0) : 0
3 pthread_kill(2, 0) : 0
4 pthread_kill(2, 0) : 3
5 pthread_kill(2, 0) : 3
6 pthread_kill(2, 0) : 3
7 pthread_kill(2, 0) : 3
3pthread_detach fail ret = 3[thrID:2]
8 pthread_kill(2, 0) : 3
1 pthread_kill(3, 0) : 3
2 pthread_kill(3, 0) : 3
3 pthread_kill(3, 0) : 3
4 pthread_kill(3, 0) : 3
5 pthread_kill(3, 0) : 3
6 pthread_kill(3, 0) : 3
7 pthread_kill(3, 0) : 3
3pthread_detach fail ret = 3[thrID:3]
8 pthread_kill(3, 0) : 3
1 pthread_kill(4, 0) : 3
2 pthread_kill(4, 0) : 3
3 pthread_kill(4, 0) : 3
4 pthread_kill(4, 0) : 3
5 pthread_kill(4, 0) : 3
6 pthread_kill(4, 0) : 3
7 pthread_kill(4, 0) : 3
3pthread_detach fail ret = 3[thrID:4]
8 pthread_kill(4, 0) : 3
1 pthread_kill(5, 0) : 3
2 pthread_kill(5, 0) : 3
3 pthread_kill(5, 0) : 3
4 pthread_kill(5, 0) : 3
5 pthread_kill(5, 0) : 3
6 pthread_kill(5, 0) : 3
7 pthread_kill(5, 0) : 3
3pthread_detach fail ret = 3[thrID:5]
8 pthread_kill(5, 0) : 3
1 pthread_kill(6, 0) : 3
2 pthread_kill(6, 0) : 3
3 pthread_kill(6, 0) : 3
4 pthread_kill(6, 0) : 3
5 pthread_kill(6, 0) : 3
6 pthread_kill(6, 0) : 3
7 pthread_kill(6, 0) : 3
3pthread_detach fail ret = 3[thrID:6]
8 pthread_kill(6, 0) : 3
1 pthread_kill(7, 0) : 0
2 pthread_kill(7, 0) : 0
3 pthread_kill(7, 0) : 0
|
在pthread_join调用返回时,线程就已结束啦。
你将测试代码贴出来,大家帮你分析分析原因。
你将测试代码贴出来,大家帮你分析分析原因。
|
是不是线程已经结束运行退出了,ThreadFuncCalcV的内容是什么?
|
在pthread_join调用返回时,线程就已结束了吧?
|
在多线程编程中象你这么使用alarm()实在不是一个好主意,因为你不能确定SIGALARM信号究竟会发给谁,很有可能你创建的线程已经被该信号终止了。另外pthread_join()会一直等待指定的线程结束,因此之后再对此线程有任何操作都是无意义的。