RE: pthread issue.

From: t.n.vanderleeuw@chello.nl
Date: Mon May 22 2000 - 11:18:01 EST


On 22 May, Leeuw van der, Tim wrote:
> Hello,
>
> The only thing I can think of, in your program, is that your alarm signal
> handler is called before the mutex is initialized - in other words, that
> your alarm handler is called right after registering it, and before
> initializing the mutex, and before the alarm is set to 5 seconds.
> This is pure speculation, however, and it shouldn't be possible for it to
> happen. I haven't yet tried to run your testprogram.
>
> Do you see the message about "bored waiting for mutex"?
> If you print the PID of the current process from the alarm handler, you can
> also see which thread calls the signal handler.
>
> Success,
>
> --Tim

I've tried your program and modified it a little bit, and the crash
occurs because the thread that tries to unlock the mutex (in the signal
handler) is the same as that which tries to lock it.
If I set the alarm() in the main-thread, the signal will be delivered
to this main-thread and there will be no segmentation fault.
I'm curious from which thread Solaris calls the signal handler. Note
that I query getpid() but that is not a portable solution; You should
probably get the pthread-id.

--Tim

Here is the modified program, for reference:

#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>

void *thr_routine ( void *arg );
void handle_signal ( int sig_num );

#ifndef pthread_attr_default /* For native POSIX threads */
#define pthread_attr_default NULL
#endif

pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

int
main ()
{
        pthread_t thr_id;

        printf("%ld: starting thread-test.\n", getpid());
        
        /* Register the signal handler */
        signal ( SIGALRM, handle_signal );

        /* Set the timer alarm for 5 seconds */
        /* set the alarm here and it works */
        alarm (5);

        pthread_create ( &thr_id, pthread_attr_default,
                thr_routine, NULL );

        pthread_join ( thr_id, NULL );

        return 0;
}

void*
thr_routine ( void *arg )
{
        /* Initialize the signal hadnler */
        pthread_mutex_init ( &g_mutex, NULL );

        /* Set the timer alarm for 5 seconds */
        /* set the alarm here and it doesn't work */
        /* alarm (5); */

        /* Acquire the mutex */
        pthread_mutex_lock ( &g_mutex );

        printf ( "%ld: Trying to get the same mutex again ... \n" , getpid());

        /* try to acquire the mutex again*/
        pthread_mutex_lock ( &g_mutex );

        printf ( "Able to get the lock.\n" );

        return;
}

void
handle_signal ( int sig_num )
{
        printf ( "%ld: Got bored waiting for mutex. \n" , getpid());
        pthread_mutex_unlock ( &g_mutex );
        printf ( "Exiting ..... \n" );
        exit (1);
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Tue May 23 2000 - 21:00:22 EST