Reduce time between each kill() function in C
Hi does anyone know a way to send signal SIGUSR1/2 without loss to another pid ?
the problem is when i 开发者_StackOverflow中文版put kill(pid, SIGUSR1) in a while loop, my other program recieve only few of them, its seems there is a loss and i have to wait between two calls to kill(). Im actually forced to used usleep() in my loop in order to receive all the SIGUSR signals ive sent. I'd like to recieve all of them as fast as it is possible.
Thx.
You need to use sigaction()
rather than signal()
because sigaction()
can control which signals are blocked while the current signal is being handled. Also, your receiver has to process each signal received; that is relatively costly compared to sending a signal, so your killer (signalling) process can outrun the 'killed' (receiving) process with ease. You should think about why you are needing to send so many signals. They are a mechanism to use in special circumstances.
There is no way for guaranteed delivery of exactly same amount of signals. If you send two or more SIGUSR1 while other process blocked and can't handle first of them, or processing previous instance of this signal, or have this signal blocked, all but one signals are lost: there is bitmask for pending signals in kernel, not array of counters.
You can only hope to receive as much of them as possible.
There are the so-called real-time signals. These have a queue and unless you overflow the queue you are guaranteed to receive one signal for each signal sent. These are the signals in the range 34-64 or to be portable, SIGRTMIN - SIGRTMAX.
However, if your assignment is to use SIGUSR1 and SIGUSR2 then maybe you're supposed to be learning about unreliable data transmission. The same sort of problems happen in electrical circuits. If a latch is already set by an electrical signal then a second signal has no effect until the latch is read and cleared. This is why serial and parallel ports have clocks and/or acknowledgement signals.
精彩评论