Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be notified when a thread has been terminated for some error

I am working on a program with a fixed number of threads in C using posix threads.

How can i be notified when a thread has been terminated due to some error?

Is there a signal to detect it?

If so, can the signal handler create a new thread to keep the number of threads the same?

like image 702
CodeRed Avatar asked May 04 '12 09:05

CodeRed


4 Answers

  1. Make the threads detached
  2. Get them to handle errors gracefully. i.e. Close mutexs, files etc...

Then you will have no probolems.

Perhaps fire a USR1 signal to the main thread to tell it that things have gone pear shaped (i was going to say tits up!)

like image 115
Ed Heal Avatar answered Oct 24 '22 07:10

Ed Heal


Create your threads by passing the function pointers to an intermediate function. Start that intermediate function asynchronously and have it synchronously call the passed function. When the function returns or throws an exception, you can handle the results in any way you like.

like image 31
Mahmoud Al-Qudsi Avatar answered Oct 24 '22 08:10

Mahmoud Al-Qudsi


With the latest inputs you've provided, I suggest you do something like this to get the number of threads a particular process has started-

#include<stdio.h>
#define THRESHOLD 50

int main ()
{
    unsigned count = 0;
    FILE *a;

    a = popen ("ps H `ps -A | grep a.out | awk '{print $1}'` | wc -l", "r");
    if (a == NULL)
        printf ("Error in executing command\n");

    fscanf(a, "%d", &count );

    if (count < THRESHOLD)
    {
        printf("Number of threads = %d\n", count-1);
            // count - 1 in order to eliminate header.
            // count - 2 if you don't want to include the main thread

        /* Take action. May be start a new thread etc */
    }

    return 0;
}

Notes:

  • ps H displays all threads.

  • $1 prints first column where PID is displayed on my system Ubuntu. The column number might change depending on the system

  • Replace a.out it with your process name

  • The backticks will evaluate the expression within them and give you the PID of your process. We are taking advantage of the fact that all POSIX threads will have same PID.

like image 1
Pavan Manjunath Avatar answered Oct 24 '22 08:10

Pavan Manjunath


I doubt Linux would signal you when a thread dies or exits for any reason. You can do so manually though.

First, let's consider 2 ways for the thread to end:

  • It terminates itself
  • It dies

In the first method, the thread itself can tell someone (say the thread manager) that it is being terminated. The thread manager will then spawn another thread.

In the second method, a watchdog thread can keep track of whether the threads are alive or not. This is done more or less like this:

Thread:
    while (do stuff)
        this_thread->is_alive = true
        work

Watchdog:
    for all threads t
        t->timeout = 0
    while (true)
        for all threads t
            if t->is_alive
                t->timeout = 0
                t->is_alive = false
            else
                ++t->timeout
                if t->timeout > THRESHOLD
                    Thread has died! Tell the thread manager to respawn it
like image 1
Shahbaz Avatar answered Oct 24 '22 06:10

Shahbaz