Solved

pthread_exit() thread process is still on ps list

Posted on 2006-12-01
5
367 Views
Last Modified: 2012-08-14
HI, I have a program I am threading. The main process spawns a thread that in turn spawns several other threads. Periodically I need the first thread to test if the multiple other threads have finished and if not to kill them and restart the list of threads. I have a signal handler that is catching my pthread_kill() call and then calling pthread_exit() from the thread, but I seem to just keep adding entries to the process list. Is there some other cleanup the 'parent' thread needs to do in order to remove the child thread from the process list?
0
Comment
Question by:PhilC
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:billtouch
ID: 18056950
I got a bit confused at your explanation.  Can you post the relevant code here? I think you could be in some sort of a deadlock.

Bill
0
 
LVL 34

Accepted Solution

by:
Duncan Roe earned 500 total points
ID: 18058570
You need to call pthread_join(3p) to actually make the thread go away. That's like a process calling wait(2) when a child exits.
How do you see there are extra processes? Vanilla ps or ps with a thread option? The old linux threads showed up with vanilla ps, but not Native Posix Tnhread Library (about libc-2.3.3 onwards). I'd recommend you move to NPTL if you haven't already done so.
0
 

Author Comment

by:PhilC
ID: 18068268
Linux 2.6.9...

  every few minutes I want my main thread to create several other threads that will do some work

//Parent
do
{
   current_time = time(&current_time);

    if (current_time - last_thread_connection_time > 60)//CHECK_CONNECTION_INTERVAL)
    {
       for(iIndex=0; iIndex<number_to_start_I; iIndex++)
      {
         pthread_create(&(*ptThreadArray[iIndex]), NULL, ConnectChildThreadFunc,(void*)iIndex);
      }
      last_thread_connection_time = time(&last_thread_connection_time);
   }
}while(1);

//child
static void *ConnectChildThreadFunc(void* arg)
{
...

return NULL;
}

One of my problems is how to know the child thread is done and not hanging. And how to kill it off from the parent if it is hanging. If I call pthread_join() on it and it is hanging then my main thread will hang too? no?

Thanks.
0
 
LVL 34

Expert Comment

by:Duncan Roe
ID: 18071796
Yes it will hang if the thread is stuck. But is that likely to happen once the code is fully debugged?
If you think you need to cater for it because of the nature of what the threads do, then you will indeed have to cater for it. A strategy could be: have one thread with the job of cleaning up successfully finishing threads. These finishing threads send a message to the cleaner thread as they are about to terminate; the cleaner thread then joins them. Have another thread to clean up the recalcitrants based say on a timeout. This scavenger thread would kill off these threads, maybe first with SIGTERM and later with SIGKILL or maybe just SIGKILL right off.
Sounds like wrting parts of an operating system? That's thread programming.
0
 

Author Comment

by:PhilC
ID: 18072227
lol thanks. I ended up putting an alarm in the child threads and if they are not finished then BAM! Force them to.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Debian 3 - initrd rebuild 3 74
centOS 7 GUI is now showing. 20 1,349
add some character at the end of line in vi 7 78
awk sed 8 39
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now