Re: pthread issue.

From: Kip Matthew Macy (kip@eventdriven.org)
Date: Mon May 22 2000 - 17:40:54 EST


Read the man page again:

<snip>

ASYNC-SIGNAL SAFETY
       The mutex functions are not async-signal safe. What this
       means is that they should not be called from a signal han­
       dler. In particular, calling pthread_mutex_lock or
       pthread_mutex_unlock from a signal handler may deadlock
       the calling thread.

</snip>

I know that this is not the behaviour you are observing, but if a function
is not defined as being async-signal safe, its behaviour when called from
a signal handler is undefined.

Manju wrote:

> 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/

-
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