Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- pid_t pid;
- void handle_alrm(int signum)
- {
- kill(pid, SIGTERM);
- printf("timeout\n");
- exit(2);
- }
- int main(int argc , char *argv[])
- {
- pid = fork();
- if(pid == 0)
- {
- char **args = argv + 2;
- execvp(argv[2], args);
- }
- else
- {
- int seconds = atoi(argv[1]);
- struct sigaction action_sig;
- memset(&action_sig, 0, sizeof(action_sig));
- action_sig.sa_handler = handle_alrm;
- action_sig.sa_flags = SA_RESTART;
- sigaction(SIGALRM, &action_sig, NULL);
- alarm(seconds);
- int status;
- waitpid(pid, &status, 0);
- alarm(0);
- if (WIFEXITED(status))
- {
- printf("ok\n");
- exit(0);
- }
- if (WIFSIGNALED(status))
- {
- printf("signaled\n");
- exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement