Using sleep and choosing with signals

I want to use the function select()to wait 1 second, since my program uses signals to manage things, so it sleep()will return prematurely. The strange thing is that when used, select()it also returns prematurely.

I call select like this

struct timeval timeout;    
timeout.tv_sec = 10;  
timeout.tv_usec = 1000000; 
select (0 ,NULL, NULL, NULL, &timeout);

but whenever a signal comes in, it returns (I use a nano stopwatch for the signal)

Does anyone know why?

+3
source share
2 answers

Try something like this:

struct timespec timeout;
timeout.tv_sec = 10;
timeout.tv_nsec = 0;
while (nanosleep(&timeout, &timeout) && errno == EINTR);

The “remaining time” indicator nanosleepwill make sure that you restart the sleep mode with the necessary amount of remaining time, if it is interrupted.

+5

man 7 signal :

, :

   * the call is automatically restarted after the signal handler
     returns; or

   * the call fails with the error EINTR.

,             SA_RESTART (. sigaction (2)). UNIX sys-            ; , Linux.

,             , SA_RESTART            ; EINTR

, , -1 errno == EINTR, - .

+4

Source: https://habr.com/ru/post/1796378/


All Articles