Guest User

Untitled

a guest
Dec 7th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <errno.h>
  2. #include <sys/types.h>
  3. #include <sys/wait.h>
  4. #include <sys/resource.h>
  5. #include <sys/time.h>
  6. #include <signal.h>
  7.  
  8. void sigHandler(int);
  9. void user1(int);
  10. void user2(int);
  11. pid_t pid, ppid;
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.  
  16. signal(2, sigHandler);
  17.  
  18. signal(SIGUSR1, user1);
  19. signal(SIGUSR2, user2);
  20. if((pid = fork()) < 0)
  21. {
  22. perror("fork failed");
  23. exit(1);
  24. }else if(!pid)
  25. {
  26. printf("spawned child PID# %d\n", getpid());
  27. while(1)
  28. {
  29. int n;
  30. n = rand() % 2;
  31. if(n == 1)
  32. kill(getpid(), SIGUSR1);
  33. else
  34. kill(getpid(), SIGUSR2);
  35. sleep(2);
  36. }
  37. }else{
  38. printf("waiting...\n");
  39. pause();
  40. }
  41. return 0;
  42. }
  43.  
  44. void sigHandler(int sigNum)
  45. {
  46. sleep(1);
  47. printf("outta here.\n");
  48. exit(0);
  49.  
  50. }
  51.  
  52. void user1(int sigNum)
  53. {
  54. printf("received a SIGUSR1 interrupt from process %d\n", getpid());
  55.  
  56. }
  57.  
  58. void user2(int sigNum)
  59. {
  60. printf("received a SIGUSR2 interrupt from process %d\n", getpid());
  61.  
  62. }
Add Comment
Please, Sign In to add comment