pthread issue.

From: Manju (manju@yahoo.com)
Date: Mon May 22 2000 - 00:45:42 EST


Hey folks,
        Just now I started to program threads on linux. I found a problem while
experimenting with pthreads. What I just wanted to know is "If the owner of the
mutex tries to lock that same mutex again, what will be the result?". This
resulted in deadlock. Thats ok.
        The problem really is, I wrote a signal handler for alarm signal ( I
wanted to wait for only certain time ) and in the signal handler function I am
unlocking the mutex and exiting. Here it segmentation faults. Si I wanted to
know why exactly this happens?
BTW on Solaris, I didn't face this "segmentation" problem.

** As Iam not subscribed to this list, please send me a mail. **

NOTE: Here is the code also for your reference.
        ( Yeah, I have read the man page on pthreads, but does that mean
trying to unlock a mutex from signal handler should "segmentation
fault",moreover this is not the case on Solaris ;( )

#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;

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

        pthread_join ( thr_id, NULL );

        return 0;
}

void*
thr_routine ( void *arg )
{
        /* Register the signal handler */
        signal ( SIGALRM, handle_signal );

        /* Initialize the signal hadnler */
        pthread_mutex_init ( &g_mutex, NULL );
        
        /* Set the timer alarm for 5 seconds */
        alarm (5);

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

        printf ( "Trying to get the same mutex again ... \n" );

        /* 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 ( "Got bored waiting for mutex. \n" );
        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:21 EST