Guest User

Untitled

a guest
Jun 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <errno.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7.  
  8. #define NELEMS(x) (sizeof (x) / sizeof (x)[0])
  9.  
  10. static void testsignaled(void) {
  11. kill(getpid(), SIGINT);
  12. }
  13.  
  14. static void teststopped(void) {
  15. kill(getpid(), SIGSTOP);
  16. }
  17.  
  18. static void testcontinued(void) {
  19. kill(getpid(), SIGSTOP);
  20. /* Busy-work to keep us from exiting before the parent waits.
  21. * This is a race.
  22. */
  23. alarm(1);
  24. while(1) {}
  25. }
  26.  
  27. int main(void) {
  28. void (*test[])(void) = {testsignaled, teststopped, testcontinued};
  29. pid_t pid[NELEMS(test)];
  30. int i, status;
  31. for(i = 0; i < sizeof test / sizeof test[0]; ++i) {
  32. pid[i] = fork();
  33. if(0 == pid[i]) {
  34. test[i]();
  35. return 0;
  36. }
  37. }
  38. /* Pause to let the child processes to do their thing.
  39. * This is a race.
  40. */
  41. sleep(1);
  42. /* Observe the stoppage of the third process and continue it. */
  43. wait4(pid[2], &status, WUNTRACED, 0);
  44. kill(pid[2], SIGCONT);
  45. /* Wait for the child processes. */
  46. for(i = 0; i < NELEMS(test); ++i) {
  47. wait4(pid[i], &status, WCONTINUED | WUNTRACED, 0);
  48. printf("%d%s%s%sn", i, WIFCONTINUED(status) ? " CONTINUED" : "", WIFSIGNALED(status) ? " SIGNALED" : "", WIFSTOPPED(status) ? " STOPPED" : "");
  49. }
  50. return 0;
  51. }
  52.  
  53. pid_t pid = fork();
  54.  
  55. if (pid == 0) {
  56. /* child */
  57. sleep(200);
  58. }
  59. else {
  60. /* parent */
  61. kill(pid, SIGSTOP);
  62.  
  63. /* do wait(), waitpid() stuff */
  64. }
Add Comment
Please, Sign In to add comment