Advertisement
YellowAfterlife

Unix, creating and stopping processes

May 6th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. /*
  2. 8. SIGPIPE, SIGUSR2, SIGTTOU. В родительском процессе установить обработчик SIGCHLD и распечатать причину отправки сигнала SIGCHLD (si_code).
  3. Сборка:
  4. gcc one.c -o one.out
  5. gcc two.c -o two.out
  6. ./two.out
  7. */
  8. /// one.c:
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <signal.h>
  12. // procedure to make process end 'by itself':
  13. void do_exit() {
  14.     exit(1);
  15. }
  16.  
  17. int main(int agrc, char *argv[])
  18. {
  19.     if(signal(SIGPIPE, do_exit) < 0) {
  20.         perror("Error setting signal");
  21.     }
  22.     for(;;) { // wait until stopped by host.
  23.         sleep(1);
  24.     }
  25. }
  26. /// two.c:
  27. // includes:
  28. #include <stdlib.h> // exit(code)
  29. #include <stdio.h>  // printf(...), scanf(...)
  30. #include <unistd.h> // execl()
  31. #include <signal.h> // sig, kill, ...
  32.  
  33. // Signal handler for SIGCHLD
  34. void sighandler_child(int sig) {
  35.     printf("SIGCHLD Signal: %d\n", sig);
  36.     /* not sure what else can be done here */
  37. }
  38.  
  39. int main(int agrc, char *argv[]) {
  40.     int code, input; // return code, input number
  41.     pid_t pid; // process id of child process
  42.     // display sort of menu:
  43.     printf("What will you do?\n");
  44.     printf("1 - SIGPIPE (end process 'on its own')\n");
  45.     printf("2 - SIGUSR2 (end process)\n");
  46.     printf("3 - SIGTTOU (end process by keyboard)\n");
  47.     scanf("%i", &input);
  48.     pid = fork(); // fork process
  49.     switch (pid) { // do depending on result of fork
  50.     case -1: // fork fail
  51.         printf(" ~ Failed to fork");
  52.         exit(1);
  53.     case 0: // child process:
  54.         if ((execl("one.out", "1", NULL)) == -1) break;
  55.         // Error in execl.
  56.         perror("\n ~ Error in execl()");
  57.         printf("\n ~ Did you try 'gcc one.c -o one.out'?");
  58.         exit(1);
  59.     default: // parent process:
  60.         // set SIGCHLD handler:
  61.         signal(SIGCHLD, sighandler_child);
  62.         switch(input) { // send ending code depending on input
  63.             case 1: kill(pid, SIGPIPE); break;
  64.             case 2: kill(pid, SIGUSR1); break;
  65.             case 3: kill(pid, SIGTTOU); break;
  66.         }
  67.         wait(&code); // wait until child process finishes
  68.         switch (code) { // display text depending on returned code:
  69.             case SIGPIPE: printf("Process ended by SIGSEGV."); break;
  70.             case SIGUSR2: printf("Process ended by SIGTERM."); break;
  71.             case SIGTTOU: printf("Process ended by SIGTSTP."); break;
  72.             default: printf("Process ended.");
  73.         }
  74.         printf("Returned code %d\n", code);
  75.         break;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement