Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

different signal handler for thread and process?. Is it possible

Have few questions regarding Signaling.

1) when the process has few more threads along with main thread, and if the signal is raised, which thread will stop its processing and continue with signal handler ? Is it main thread or other than main thread ?

2) Is it possible to keep different handler for the same signal between main thread and specific thread ?

like image 345
Whoami Avatar asked Sep 04 '11 01:09

Whoami


People also ask

Are signal handlers per thread or per process?

Signal management in multithreaded processes is shared by the process and thread levels, and consists of the following: Per-process signal handlers. Per-thread signal masks. Single delivery of each signal.

Can each thread have their own signal handler?

Each thread can have its own set of signals that will be blocked from delivery. The sigthreadmask subroutine must be used to get and set the calling thread's signal mask. The sigprocmask subroutine must not be used in multi-threaded programs; otherwise, unexpected behavior may result.

Are signal handlers shared across threads?

Each thread has its own signal mask. This lets a thread block some signals while it uses memory or another state that is also used by a signal handler. All threads in a process share the set of signal handlers set up by sigaction(2) and its variants.

Do signal handlers run concurrently?

Signal handlers run concurrently with main program (in same process). <= 1 pending signal per type per process No Queue! Just a bit per signal type. Signals of type S discarded while process has S signal pending.


1 Answers

Signals can be sent to either a process or a particular thread. For signals sent to the process, the signal will be delivered as soon as there is at least one thread where that signal isn't blocked, and if there's more than one such thread, it may be delivered to any one of them (unpredictable which one). For signals sent to a particular thread, they're delivered as soon as that thread does not have the signal blocked.

Using the raise function to raise a signal sends the signal to the thread that called raise, not the whole process. Signals automatically generated as a result of things the thread does (like SIGSEGV SIGFPE, and SIGPIPE) are also delivered to that particular thread.

Signals generated from the terminal (SIGINT, SIGTSTP, SIGQUIT) are delivered to the whole process.

There is no way to install separate signal handlers for each thread, but the signal handler for a signal may be able to examine which thread it's running in. If you know the signal did not interrupt an async-signal-unsafe function, you could call pthread_self to get the current thread id. Otherwise, one ugly but safe method is to take the address of errno and look up which thread you're in based on that (you'll have to keep a mapping table yourself and ensure that access to this table is async-signal-safe).

like image 122
R.. GitHub STOP HELPING ICE Avatar answered Sep 18 '22 01:09

R.. GitHub STOP HELPING ICE