Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between SIGUSR1 and SIGUSR2

Tags:

c

unix

signals

Reading the manuscript for signals, I came across the signal table and saw there are two defined signals for usr SIGUSR1 and SIGUSR2. Is there any difference between the two? If so when should I choose to use one over the other? If there is no difference why are ther two signals serving the same purpose?

like image 360
Ravid Goldenberg Avatar asked Dec 10 '14 14:12

Ravid Goldenberg


2 Answers

You have two signals for your own needs. It is guaranteed that the system will never deliver them to your processes, so you can put any semantic you want on them. Why two? Purely arbitrary; only one would have certainly been stingy, so they gave us two, but they could have given much more. This is an arbitrary degree of freedom concerning signal-based communication.

I think that most of the time this is sufficient. I personally never used more than both user-defined at the same time... Such a signal is used, for example, to warn a server who is doing some consuming task that something special is to do "urgently": read something in a communication channel, stop the current task, etc. If you need more than two user-defined signals, this probably means that you made a bad design and overstrained their usage. Remember that using catching signals induces some concurrency in your own code, which has a serious impact on the design of it.

like image 153
Jean-Baptiste Yunès Avatar answered Nov 10 '22 19:11

Jean-Baptiste Yunès


As far as I'm concerned there is no difference between them (beside their values). Both are reserved to the user and they must choose how to use them. They might serve the same purpose (signal something) but they can be used to signal different things.

There is a really nice example of that here. You can focus only on the signal creation and handling. Note that both SIGUSR1 and SIGUSR2 are handled by the same handler but you could give them different meanings.

like image 35
delirium Avatar answered Nov 10 '22 17:11

delirium