Created
May 30, 2024 17:40
-
-
Save a2gs/573a03fa0e6ba2ab18cb7d95a087839a to your computer and use it in GitHub Desktop.
simple sigalrm sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Andre Augusto Giannotti Scota (https://sites.google.com/view/a2gs/ andre.scota@gmail.com) */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <signal.h> | |
void handleAlarmSignal(int signum) | |
{ | |
printf("Timeout!\n"); | |
exit(1); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char cmd[100] = {0}; | |
unsigned int left = 0; | |
if(argc != 3){ | |
printf("Usage: %s <TIMER> <TIMEOUT>\n", argv[0]); | |
return(0); | |
} | |
signal(SIGALRM, handleAlarmSignal); | |
sprintf(cmd, "sleep %s", argv[1]); | |
alarm(atoi(argv[2])); | |
system(cmd); // using system() to spaw another process (alarm() and sleep() doesnt work well together) | |
left = alarm(0); | |
printf("Normal exit (%u seconds left).\n", left); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment