Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is SIGSEGV delivered to each thread?

I have a program in Linux which is multithreaded. There are certain memory areas in which I'm interested to see if they have been written within a certain time period. For that I give only read access to those memory pages and install a signal handler for SIGSEGV. Now my question is, will each thread call the signal handler for itself. Say Thread 1 writes to some forbidden memory area, will it be the one to execute the signal handler?

like image 963
MetallicPriest Avatar asked Jun 30 '11 10:06

MetallicPriest


People also ask

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.

Where should a signal be delivered for multi threaded?

A process-directed signal may be delivered to any one of the threads that does not currently have the signal blocked. If more than one of the threads has the signal unblocked, then the kernel chooses an arbitrary thread to which to deliver the signal.

How does signal handler work Linux?

The behavior of a process when a signal arrives, can be modified by establishing a signal handler using signal() or sigaction() . Standard Signals are not queued, that is, multiple signals sent to a process will be retrieved only as one. A signal set is used to work on multiple signals together.

How many signals are there in Linux?

The Linux kernel supports a range of 33 different real-time signals, numbered 32 to 64.


2 Answers

First of all

Signal dispositions are process-wide; all threads in a process share the same disposition for each signal. If one thread uses sigaction() to establish a handler for, say, SIGINT, then that handler may be invoked from any thread to which the SIGINT is delivered.

But read on

A signal may be directed to either the process as a whole or to a specific thread. A signal is thread-directed if

it is generated as the direct result of the execution of a specific hardware instruction within the context of the thread (SIGBUS, SIGFPE, SIGILL, and SIGSEGV)

I am quoting from TLPI.

like image 159
cnicutar Avatar answered Sep 19 '22 09:09

cnicutar


No, per the question title.

To the question body: For the particular signal that you are asking for, yes (otherwise: it depends). The thread causing a segfault will receive the signal.

See signal(7):

A signal may be generated (and thus pending) for a process as a whole (e.g.,
when sent using kill(2)) or for a specific thread (e.g., certain signals, such
as SIGSEGV and SIGFPE, generated as a consequence of executing a specific
machine-language instruction are thread directed [...].
like image 33
Damon Avatar answered Sep 22 '22 09:09

Damon