signal

Hi,

In manual, a lot of functions related to signal are found. However, I
encounter the trouble to understand the signals as well as how to use them.

I have some questions as following:
How to send a signal to a thread, at the same time, how to catch a
signal of a thread? And how about SIGUSR1, SIGUSR2? Can I create new
threads or destory threads in the signal handler?


Thanks.

Belinda <yye@is2.dal.ca> wrote:

Hi,

In manual, a lot of functions related to signal are found. However, I
encounter the trouble to understand the signals as well as how to use them.

I have some questions as following:
How to send a signal to a thread, at the same time, how to catch a
signal of a thread? And how about SIGUSR1, SIGUSR2? Can I create new
threads or destory threads in the signal handler?

This is a messy and complicated subject.

From outside a process, you can’t generally send a signal to a specific
thread – they are delivered to a process. From within a process, a
signal can be delivered to a particular thread, using pthread_kill().
If a signal is sent to a process, a thread is picked to receive it,
this will be a thread that does not have the signal masked.

How you handle signals is also messy – you can use signal() or
sigaction() to apply a signal handler or ignore signals. You can
use pthread_sigmask() to mask signals for the calling thread.

pthread_create() is documented as being safe to use in a signal
handler, so you can create threads in a signal handler. Same for
pthread_cancel(). BUT… the termination of threads with pthread_cancel()
(or even worse, pthread_abort()) would be something I would tend to
avoid. It introduces complexities that can be quite messy to deal with.
(Cancellation handlers, clean-up of mutexes that are locked by the
canceled thread, clean up of memory it might have allocated, etc.)

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.