user157629
user157629

Reputation: 634

How does a program handle an interruption with several threads and signals?

Imagine I have a program with a main function that creates several threads. In each thread I redirect the CTRL + C interruption (SIGINT) to a functionA via signal(SIGINT, functionA) and in the main process I redirect the same interruption to a functionB (signal(SIGINT, functionB).

When the program is running and the interruption SIGINT is sent, what will the program do? It will execute functionA in all threads then functionB in the main process?

Upvotes: 1

Views: 101

Answers (1)

Glärbo
Glärbo

Reputation: 166

Signal handler action (SIG_IGN, SIG_DFL, or a handler function) is a per-process property, not a per-thread property.

This means that if say different threads use sigaction() to set the same signal action, then the latest one wins.

Furthermore, if you have multiple threads that do not block the signal, the kernel simply chooses one (at random, basically) to use to deliver the signal. That is, it is only delivered once, not once per thread.

Upvotes: 2

Related Questions