ArticleDirectory
- Concept
- Pthread_sigmask
- Pthread_kill and sigwait
- Feedback
Concept
- According to POSIX, asynchronous (external) signals are sent to the entire process.
- All threads share the same setting, that is, the thread disposal method set through sigaction.
- Each thread has its own signal mask. The thread library determines the thread to which the signal is sent based on the mask.
- Because of the uniqueness of Linux thread implementation, external signals are always sent to specific threads.
Pthread_sigmask
- Pthread_sigmask is used to define the signal mask of a thread.
- Its interface is the same as sigprocmask
========================================================== ========================================================
# Include <pthread. h>
# Include <signal. h>
Int pthread_sigmask (INT how, const sigset_t * newmask, sigset_t * oldmask );
========================================================== ========================================================
Pthread_kill and sigwait
========================================================== ========================================================
# Include <pthread. h>
# Include <signal. h>
Int pthread_kill (pthread_t thread, int signo );
Int sigwait (const sigset_t * Set, int * sig );
========================================================== ========================================================
- Pthread_kill sends a signal to a specific thread.
- Sigwait tentatively calls the thread until a signal defined in the set is delivered to the call thread.
- When sigwait returns, the SIG stores the received signal number.
- The signal waiting for sigwait must be blocked in all threads, not just the call thread.In the multi-threadedProgramTo process signals only in the main thread, you can use
Function:
Int pthread_sigmask (INT how,
Const sigset_t * set,
Sigset_t * oset)
Used to control the signal mask in the main line.
How:
Sig_block: The result set is the union of the current set parameters.
Sig_unblock: The result set is the difference set of the current set parameter set.
Sig_setmask: The result set is the set pointed to by the parameter set.
Header file: <signal. h>
Error: [einval] How is not a defined value
Tip: unless the signal is blocked in all threads, the asynchronous signal is always transmitted to this process.
Example:
# Include <pthread. h>
# Include <stdio. h>
# Include <sys/signal. h>
# Define numthreads 3
Void sighand (INT signo );
Void * threadfunc (void * parm)
{
Pthread_t tid = pthread_self ();
Int RC;
Printf ("thread % u entered \ n", tid );
Rc = sleep (30 );
Printf ("thread % u did not get expected results! Rc = % d \ n ", tid, RC );
Return NULL;
}
Void * threadmasked (void * parm)
{
Pthread_t tid = pthread_self ();
Sigset_t mask;
Int RC;
Printf ("Masked thread % lu entered \ n", tid );
Sigfillset (& Mask);/* mask all allowed signals */
Rc = pthread_sigmask (sig_block, & Mask, null );
If (RC! = 0)
{
Printf ("% d, % s \ n", RC, strerror (RC ));
Return NULL;
}
Rc = sleep (15 );
If (RC! = 0)
{
Printf ("Masked thread % lu did not get expected results! "
"Rc = % d \ n", tid, RC );
Return NULL;
}
Printf ("Masked thread % lu completed masked work \ n ",
TID );
Return NULL;
}
Int main (INT argc, char ** argv)
{
Int RC;
Int I;
Struct sigaction actions;
Pthread_t threads [numthreads];
Pthread_t maskedthreads [numthreads];
Printf ("Enter testcase-% s \ n", argv [0]);
Printf ("set up the alarm handler for the Process \ n ");
Memset (& actions, 0, sizeof (Actions ));
Sigemptyset (& actions. sa_mask );
Actions. sa_flags = 0;
Actions. sa_handler = sighand;
Rc = sigaction (sigalrm, & actions, null );
Printf ("create masked and unmasked threads \ n ");
For (I = 0; I <numthreads; ++ I)
{
Rc = pthread_create (& threads [I], null, threadfunc, null );
If (RC! = 0)
{
Printf ("% d, % s \ n", RC, strerror (RC ));
Return-1;
}
Rc = pthread_create (& maskedthreads [I], null, threadmasked, null );
If (RC! = 0)
{
Printf ("% d, % s \ n", RC, strerror (RC ));
Return-1;
}
}
Sleep (3 );
Printf ("send a signal to masked and unmasked threads \ n ");
For (I = 0; I <numthreads; ++ I)
{
Rc = pthread_kill (threads [I], sigalrm );
Rc = pthread_kill (maskedthreads [I], sigalrm );
}
Printf ("Wait For masked and unmasked threads to complete \ n ");
For (I = 0; I <numthreads; ++ I ){
Rc = pthread_join (threads [I], null );
Rc = pthread_join (maskedthreads [I], null );
}
Printf ("main completed \ n ");
Return 0;
}
Void sighand (INT signo)
{
Pthread_t tid = pthread_self ();
Printf ("thread % lu in signal Handler \ n ",
TID );
Return;
}
Program return:
Enter testcase-./pthread_sigmask_test
Set up the alarm handler for the Process
Create masked and unmasked threads
Thread 3086597040 entered
Masked thread 3076107184 entered
Thread 3065617328 entered
Masked thread 3055127472 entered
Thread 3044637616 entered
Masked thread 3034147760 entered
Send a signal to masked and unmasked threads
Wait for masked and unmasked threads to complete
Thread 3086597040 in signal handler
Thread 3086597040 did not get expected results! Rc = 27
Thread 3065617328 in signal handler
Thread 3065617328 did not get expected results! Rc = 27
Thread 3044637616 in signal handler
Thread 3044637616 did not get expected results! Rc = 27
Masked thread 3076107184 completed masked work
Masked thread 3055127472 completed masked work
Masked thread 3034147760 completed masked work
Main completed
Feedback # Re: POSIX thread-pthread_sigmask reply to more comments
2010-03-27 by yswzing may have a race. During pthread_kill, the masked thread may not have executed the pthread_sigmask call.