camino
camino

Reputation: 10584

sleep function will abort when received a signal?

#include <signal.h>
#include <stdio.h>

void ints(int i )
{
   printf("ints \n");
}


int main(void)
{
    signal(SIGINT, ints);
   sleep(10);  
}

input Ctrl+C , the program will terminate immediately with output:

^ints

I was wondering why,in my opinion,the program should terminate after 10 seconds no matter how many times Ctrl+C is input.

Upvotes: 1

Views: 3820

Answers (1)

Vijay Agrawal
Vijay Agrawal

Reputation: 3821

sleep() is one of those functions that is never re-started when interrupted.

interestingly, it also does not return a EINT as one would expect.

It instead returns success with the time remaining to sleep.

See: http://www.kernel.org/doc/man-pages/online/pages/man7/signal.7.html for details on other APIs that do not restart when interrupted

Upvotes: 3

Related Questions