use the following command:
ulimit -c unlimited
The core file will be created.
let us know the output of the following command:
gdb binaryName coreFileName
Main Topics
Browse All TopicsHi,
I'm getting a segmentation fault when trying to run pthread_join on Linux.
On my program, the main function calls several threads that do all the work.
to prevent the main function from exiting prematurely, the main uses pthread_join.
When waiting for the first thread in line, there is a segmentation fault.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Core dump file is not created. The where command output is as follow:
Program received signal SIGSEGV, Segmentation fault.
0x007c777b in __deallocate_stack () from /lib/tls/libpthread.so.0
(gdb) where
#0 0x007c777b in __deallocate_stack () from /lib/tls/libpthread.so.0
#1 0x007c7d2b in __free_tcb () from /lib/tls/libpthread.so.0
#2 0x007c8d9f in pthread_join () from /lib/tls/libpthread.so.0
#3 0x0804b73a in OT_WaitForThreads (nCount=4, pHandles=0x81efef8, bWaitAll=1, dwMilliseconds=0) at OTThread.c:80
#4 0x0804a89e in EngineCleanUp () at Engine.c:106
#5 0x0804a962 in main () at Engine.c:138
(gdb)
That seems to indicate that your heap got corrupted. That could have happened anywhere and anytime. Check whether you can spot any obvious buffer overflows, or other write operations to unallocated (or simply wrong) memory. If you can't spot it immediately, I suggest using a memory debugger to find the problem for you.
Answer for b:
There is no return value. The application crashes within the function.
Answer for c:
I derify that ThreadArray holds the number of the threads expected. the code you asked for is attached.
BTW - Since the general code in the application is crosspatform and since in Windows i seo problem with this application i'm pretty sure this is pure Linux / Unix pthread_join related issue.
>> There is no return value. The application crashes within the function.
Of course there is a return value ... It returns int.
The crash you mentioned was not in pthread_create, but in pthread_join. pthread_create happens before pthread_join (or at least it should), and my remark was about pthread_create's return value not being checked. As long as you don't do so, you can't know whether the creation of the thread failed or not (which could cause problems later, including the crash you experience).
>> I derify that ThreadArray holds the number of the threads expected.
And are all entries in the array valid ?
>> the code you asked for is attached.
You must have forgotten it, as I can't see it ;)
>> i'm pretty sure this is pure Linux / Unix pthread_join related issue.
I'm not ... There's a LOT of differences between a Windows and a Linux platform, so just because the problem occurs on one platform and not on the other, does not mean that you can immediately point to one function as the problem.
Hre is the code:
for(i = 0 ; i < MAX_THREAD ; i++)
{
if(ThreadArray[i] == NULL)
{
waitForAllThreads = 0;
break;
}
}
if( 0 != waitForAllThreads)
{
// DON'T wait for the listen thread it is block by the read command
// Make sure Listen thread is always the last one on the handle array
OT_WaitForThreads(MAX_THRE
}
LoggerAddMessage( PROXY_LOG_LEVEL_ANALYSIS , FLUSH_TO_DISK,"EngineInit - Start ...\n" ) ;
QueueInit(&messageQ);
QueueInit(&writeQ);
QueueInit(&transmitQ);
strcpy(messageQ.qName , "MessageQ");
messageQ.hMutex = OT_CreateMutex(messageQ.qN
strcpy(writeQ.qName , "WriteQ");
writeQ.hMutex = OT_CreateMutex(writeQ.qNam
strcpy(transmitQ.qName , "TransmitQ");
transmitQ.hMutex = OT_CreateMutex(transmitQ.q
ThreadArray[WORKER_THREAD]
&messageQ, // argument to thread function
&ThreadArray[WORKER_THREAD
ThreadArray[WRITER_THREAD]
&writeQ, // argument to thread function
&ThreadArray[WRITER_THREAD
ThreadArray[TRANSMIT_THREA
&transmitQ, // argument to thread function
&ThreadArray[TRANSMIT_THRE
ThreadArray[CMT_THREAD] = OT_CreateThread( CMTThread, // thread function
&messageQ, // argument to thread function
&ThreadArray[TRANSMIT_THRE
// Make sure Listen thread is always the last one on the handle array - refer to the comment in the EngineCleanup
ThreadArray[LISTEN_THREAD]
&messageQ, // argument to thread function
&ThreadArray[LISTEN_THREAD
I assume that MAX_THREAD is equal to 5, and that WORKER_THREAD, WRITER_THREAD, TRANSMIT_THREAD, CMT_THREAD and LISTEN_THREAD are 0, 1, 2, 3 and 4 resp. ?
Ok.
I can see a typo here though :
>> ThreadArray[CMT_THREAD] = OT_CreateThread( CMTThread, // thread function
>> &messageQ, // argument to thread function
>> &ThreadArray[TRANSMIT_THRE
which should be :
ThreadArray[CMT_THREAD] = OT_CreateThread( CMTThread, // thread function
&messageQ, // argument to thread function
&ThreadArray[CMT_THREAD]);
If the problem still persists after that, then do not forget to check the return value of each pthread_create, and to fix this :
>> if(ThreadArray[i] == NULL)
as I remarked earlier.
If all of that doesn't get you closer to the solution, then it's time to bring out the memory debugger :)
Business Accounts
Answer for Membership
by: Infinity08Posted on 2009-04-07 at 01:14:11ID: 24084867
(a) How is OT_THREAD_HANDLE defined ? It should be pthread_t.
(b) You don't check the return value of pthread_create ... How will you know if it failed ?
(c) Did you verify that pHandles actually contains nCount valid pthread_t's referring to existing threads ? Can you show the code that actually calls these functions.