Advertisement
Guest User

Untitled

a guest
Nov 9th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include <sys/types.h>
  5. #include <stdlib.h>
  6. #include <sys/wait.h>
  7.  
  8. pid_t pid;
  9.  
  10. void f_parent()
  11. {
  12.     sleep(1);
  13.     printf("Ping..");
  14.     kill(pid, SIGUSR2);
  15.     fflush(stdout);
  16. }
  17.  
  18. void f_child()
  19. {
  20.     sleep(1);
  21.     printf("Pong\n");
  22.     kill(getppid(), SIGUSR1);
  23. }
  24.  
  25. int main()
  26. {
  27.     int status;
  28.     printf("Gioco del Ping-Pong tra padre e figlio\n\n");
  29.     signal(SIGUSR1, f_parent);
  30.     pid = fork();
  31.     switch(pid)
  32.     {
  33.         case -1:
  34.         perror("Fork failed: ");
  35.         exit(1);
  36.         break;
  37.  
  38.         case 0: // Figlio
  39.         signal(SIGUSR2, f_child);
  40.         while(1);
  41.         break;
  42.  
  43.         default: // Padre
  44.         sleep(1);
  45.         kill(getpid(), SIGUSR1);
  46.         wait(&status);
  47.         break;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement