Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pthread_exit() thread process is still on ps list

Posted on 2006-12-01
5
Medium Priority
?
410 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 35

Accepted Solution

by:
Duncan Roe earned 2000 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 35

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

926 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