Solved

pthread_exit() thread process is still on ps list

Posted on 2006-12-01
5
357 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

7 Experts available now in Live!

Get 1:1 Help Now