Advertisement
Guest User

Untitled

a guest
May 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9.  
  10. pid_t pid;
  11. _Bool timeout = 0;
  12.  
  13. void handle_sig(int signum, siginfo_t *info, void *ucontext)
  14. {
  15.     if(info->si_code == CLD_EXITED)
  16.     {
  17.         printf("ok\n");
  18.         fflush(stdout);
  19.         exit(0);
  20.     }
  21.     if(info->si_code == CLD_KILLED && timeout == 0)
  22.     {
  23.         printf("signaled\n");
  24.         fflush(stdout);
  25.         exit(1);
  26.     }
  27. }
  28.  
  29. int main(int argc, char *argv[])
  30. {
  31.     pid = fork();
  32.     if(pid == 0)
  33.     {
  34.         char **args = argv + 2;
  35.         execvp(argv[2], args);
  36.     }
  37.     else
  38.     {
  39.         struct sigaction action_sig;
  40.         memset(&action_sig, 0, sizeof(action_sig));
  41.         action_sig.sa_sigaction = handle_sig;
  42.         action_sig.sa_flags = SA_SIGINFO;
  43.         sigaction(SIGCHLD, &action_sig, NULL);
  44.  
  45.         if(atoi(argv[1]) == 0)
  46.         {
  47.             kill(getpid(), SIGALRM);
  48.         }
  49.  
  50.         sleep(atoi(argv[1]));
  51.         timeout = 1;
  52.         kill(pid, SIGTERM);
  53.         waitpid(pid, 0, 0);
  54.         printf("timeout\n");
  55.         fflush(stdout);
  56.         exit(2);
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement