Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6.  
  7. #define MAX_LENGTH 10
  8.  
  9.  
  10. char *buf[MAX_LENGTH];
  11.  
  12.  
  13. static void sigact_handler (int sig, siginfo_t *siginfo, void *context)
  14. {
  15.  
  16.   read(0, buf, MAX_LENGTH);
  17.   // printf("Read\n %d");
  18.  
  19.   write(1, buf, MAX_LENGTH);
  20.   // printf("Written\n\n");
  21.   printf("pid: %ld\nuid: %ld\n\n", (long) siginfo->si_pid, (long) siginfo->si_uid);
  22. }
  23.  
  24. int main (void)
  25. {
  26.   alarm(10);
  27.   printf("Set alarm.\nEnter a string:\n");
  28.  
  29.   struct sigaction mysigact;
  30.  
  31.   // set handler
  32.   mysigact.sa_sigaction = &sigact_handler;
  33.  
  34.   // says do not use sa_handler field, use the above sa_sigaction field
  35.   // sa_sigaction takes more arguments
  36.   mysigact.sa_flags = SA_RESTART; // switching between SA_SIGINFO and SA_RESTART  
  37.  
  38.   if (sigaction (SIGALRM, &mysigact, NULL) < 0)
  39.   {
  40.     perror("sigalrm");
  41.     return 1;
  42.   }
  43.  
  44.   while(1) sleep(10);
  45.  
  46.   return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement