IPC:signal basics(3)

Example of signal thread/signal handler

In main thread and other threads,

sigset_t sigset;

sigfillset(&sigset);

sigdelset(&sigset, SIGSEGV);

pthread_sigmask(SIG_BLOCK, &sigset, NULL);

in main thread,

signal(SIGSEGV, clean_up);

if(sigsetjmp(env, 1) != 0) {

          // do something

          exit(EXIT_FAILURE);

}

in signal thread,

sigset_t sigset;

sigfillset(&sigset);

sigdelset(&sigset, SIGSEGV);

pthread_sigmask(SIG_BLOCK, &sigset, NULL);

for(;;) {

if (sigwait(&sigset, &signo) == 0) {

                    // do something;

          }

}

In clean_up(signo)

Option 1 (simple signal handler):

// do something

signal(signo, SIG_DFL);

raise(signo);

option 2(siglongjmp):

signal(signo, SIG_DFL);

siglognjmp(env, 1); 

Note:

  1. signal_thread is not able to catch SIGSEGV, generated due to program errors (it targets to the erroneous thread itself).
  2. signal handler is asynchronous, therefore, inspection and manupulation of shared variables need to be protected.
  3. signal handler is possible to be executed by any thread.
  4. the code after siglongjmp will be continue executed by the same thread which calles signal hander..but the code is a part of main thread..
    1. I have seen an extra SIGSEGV generated, if it is other thread, not main thread, executes the content in sigsetjmp().
  5. even the internal mutex, used between threads, is locked prior SIGSEGV is generated. signal handler, or the code in sigsetjmp can still accquire the mutex, without errors (pthread_mutex_lock() returns 0, no errno set)…the mutex is with default atrributes, no recurrsive. qnx doc says the behavior is unpredictable…
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s