Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signal queuing in C

I have a simple program under Linux which sends SIGUSR1 signal to its child process in a cycle. But when I send e.g. 10 signals, sometimes happens, that the child received only 3 of them. Last sent signal is always SIGUSR2 and that is received every time.

Are the signals queuing, or when process didn't process the previous, it is simply overwritten? Is there a way I can send signals in a queue?

like image 440
Peter Krejci Avatar asked Mar 12 '11 20:03

Peter Krejci


People also ask

What is signal queue in Linux?

Signal Queue: Each process maintains a queue of signals it has received but not yet processed. A signal that has been blocked using a mask will be queued up. The process can access this queue through sigwait (), sigwaitinfo (), and similar functions.

What is a signal in C language?

Signals in C language. A signal is a software generated interrupt that is sent to a process by the OS because of when user press ctrl-c or another process tell something to this process. There are fix set of signals that can be sent to a process. signal are identified by integers. Signal number have symbolic names.

What is queue in C with example?

Queue in C is a data structure that is not like stack and one can relate the behavior of queue in real -life. Unlike stack which is opened from one end and closed at the other end which means one can enter the elements from one end only.

How to terminate a process that has received a SIGINT signal?

If user presses ctrl-c to terminate the process because of SIGINT signal sent and its default handler to terminate the process. A process can replace the default signal handler for almost all signals (but not SIGKILL) by its user’s own handler function.


2 Answers

What happens is the following:

  1. First signal received, namely SIGUSR1, handler is called and is running
  2. Second signal received, since handler from nr1 is still running, the signal nr2 gets pending and blocked.
  3. Third signal received, since handler from nr1 is still running, the signal 3 gets discarded.
  4. Fourth, fifth...etc signal of the same type as the signal nr1 are discarded.

Once signal handler is done with signal nr1, it will process signal nr2, and then signal handler will process the SIGUSR2.

Basically, pending signals of the same type are not queued, but discarded. And no, there is no easy way to "burst" send signals that way. One always assumes that there can be several signals that are discarded, and tries to let the handler do the work of cleaning and finding out what to do (such as reaping children, if all children die at the same time).

like image 153
Milan Avatar answered Oct 13 '22 03:10

Milan


If multiple signals of the same type are sent and not handled, they aren't queued. Say the program masks SIGUSR1, calls kill(getpid(), SIGUSR1) 10 times and unmasks SIGUSR1. It will receive SIGUSR1 just once.

like image 45
kubi Avatar answered Oct 13 '22 03:10

kubi