S
syed
I am trying to write a little program that goes to sleep for 5 seconds.
However, if the user presses Ctrl-C, it interrupts the sleep and prints the
unslept amount.
For example:
unix> ./sleeper
sleeping for 5 seconds
unslept amount is 2 (User hits Ctrl-C after 3 seconds)
unix>
What's wrong with the code below?
-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
unsigned int unslept;
void handler (int sig)
{
printf("unslept amount is %d",unslept);
exit(0);
}
int main()
{
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
return 0;
}
However, if the user presses Ctrl-C, it interrupts the sleep and prints the
unslept amount.
For example:
unix> ./sleeper
sleeping for 5 seconds
unslept amount is 2 (User hits Ctrl-C after 3 seconds)
unix>
What's wrong with the code below?
-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
unsigned int unslept;
void handler (int sig)
{
printf("unslept amount is %d",unslept);
exit(0);
}
int main()
{
signal(SIGINT,handler);
printf("sleeping for 5 seconds\n");
unslept = sleep(5);
return 0;
}