Solved

pthread_exit() thread process is still on ps list

Posted on 2006-12-01
5
363 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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 demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

896 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

17 Experts available now in Live!

Get 1:1 Help Now