【发布时间】:2010-10-19 18:08:28
【问题描述】:
参考我之前关于GDB not pinpointing the SIGSEGV point的问题,
我的线程代码如下:
void *runner(void *unused)
{
do
{
sem_wait(&x);
...
if(/*condition 1 check*/)
{
sem_post(&x);
sleep(5);
sem_wait(&x);
if(/*repeat condition 1 check; after atleast 5 seconds*/)
{
printf("LEAVING...\n");
sem_post(&x);
// putting exit(0); here resolves the dilemma
return(NULL);
}
}
sem_post(&x);
}while(1);
}
主要代码:
sem_t x;
int main(void)
{
sem_init(&x,0,1);
...
pthread_t thrId;
pthread_create(&thrId,NULL,runner,NULL);
...
pthread_join(thrId,NULL);
return(0);
}
编辑:在运行线程代码中有一个 exit(0),会使故障消失。
堆栈损坏的原因可能是什么?
GDB 输出:(0xb7fe2b70 是运行线程 id)
LEAVING...
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7fe2b70 (LWP 2604)]
0x00000011 in ?? ()
Valgrind 输出:
==3076== Thread 2:
==3076== Jump to the invalid address stated on the next line
==3076== at 0x11: ???
==3076== by 0xA26CCD: clone (clone.S:133)
==3076== Address 0x11 is not stack'd, malloc'd or (recently) free'd
==3076==
==3076==
==3076== Process terminating with default action of signal 11 (SIGSEGV)
==3076== Bad permissions for mapped region at address 0x11
==3076== at 0x11: ???
==3076== by 0xA26CCD: clone (clone.S:133)
==3076== Address 0x11 is not stack'd, malloc'd or (recently) free'd
【问题讨论】:
-
您需要发布更多代码。所有多汁的部分都不见了。
-
这是 valgrind 报告的唯一错误吗?如果它报告其他人,从第一个开始。
-
在运行线程代码中有一个 exit(0) 甚至不是一个可以接受的解决方案。