Why does pausing the signal make the program sleep forever?

The APUE book states that: if the signal appears after the test sig_int_flag, but before the call, pausethis process can sleep forever.

I don’t know why, can someone tell me? Many thanks.

int sig_int();                 /* my signal handling function */
int sig_int_flag;              /* set nonzero when signal occurs */

int main() {
    signal(SIGINT, sig_int)    /* establish handler */
    .
    .
    .
    while (sig_int_flag == 0)
        pause();               /* go to sleep, waiting for signal */
}

int sig_int() {
    signal(SIGINT, sig_int);   /* reestablish handler for next time */
    sig_int_flag = 1;          /* set flag for main loop to examine */
}
+4
source share
1 answer

If the interrupt signal is issued at the exact time that you are describing:

  • flag checked false: loop input
  • the signal is reset by setting the flag 1, but too late (test completed)
  • since the cycle has already been entered, it is called pause(), and the program waits

, CTRL + C/SIGINT , , , .

, sleep:

while (sig_int_flag == 0)
{
     printf("Hit CTRL+C in the next 10 seconds to trigger the bug\n");
     sleep(10);
     pause();               /* go to sleep, waiting for signal */
}

pause() :

while (sig_int_flag == 0)
{
     sleep(1);
}

SIGINT , while sleep, , , , 1 , , , , , sleep , , , , pause , SIGINT.

+1

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


All Articles