Afaeld

Ping pong signals C

Nov 5th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <signal.h>
  5. #include <sys/wait.h>
  6. #include <sys/mman.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9.  
  10. void SIGUSR1_handler(int);
  11. void SIGUSR2_handler(int);
  12.  
  13. int receive1 = 0;
  14. int receive2 = 0;
  15. static int *scorePlayer1;
  16. static int *scorePlayer2;
  17.  
  18.  
  19. int main(int argc, char const *argv[])
  20. {
  21.     int frappe = 0;
  22.     srand(time(NULL));
  23.     scorePlayer1 = mmap(NULL,sizeof *scorePlayer1,PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0); //Memoire partagée entre les process
  24.     *scorePlayer1 = 0;
  25.     scorePlayer2 = mmap(NULL,sizeof *scorePlayer2,PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  26.     *scorePlayer2 = 0;
  27.     signal(SIGUSR1,SIGUSR1_handler);
  28.     signal(SIGUSR2,SIGUSR2_handler);
  29.  
  30.  
  31.     pid_t pid = fork(); //Create child process
  32.  
  33.  
  34.  
  35.  
  36.     switch (pid)
  37.     {
  38.  
  39.         case 0: //Fils
  40.  
  41.             while(*scorePlayer1 <=13 && *scorePlayer2 <= 13)
  42.             {
  43.  
  44.  
  45.                     while(receive1 ==0) //Permet de synchroniser les process
  46.                     {}
  47.                     if(*scorePlayer2 == 13)
  48.                     {
  49.                         printf("J'ai gagné ! pourquoi ai-je un pere aussi nul ?\n");
  50.                         fflush(stdout);
  51.                         kill(getppid(),SIGTERM);
  52.                         exit(EXIT_SUCCESS);
  53.                     }
  54.                     frappe = (rand() % 100);
  55.                     if(frappe < 50) //Reussi son coup
  56.                     {      
  57.  
  58.  
  59.  
  60.                     }
  61.                     else //Loupe la balle
  62.                     {
  63.                         printf("Le joueur fils loupe sa frappe, le pere gagne 1 point\n");
  64.                         fflush(stdout);
  65.                         *scorePlayer1 = *scorePlayer1 +1;
  66.                         printf("Score actuel %d (pere) à %d (fils) \n",*scorePlayer1,*scorePlayer2);
  67.                         fflush(stdout);
  68.                     }
  69.                     printf("PONG \n");
  70.                     fflush(stdout);
  71.                     *compteur = *compteur +1;
  72.                     receive1 = 0;
  73.                     sleep(1);
  74.                     kill(getppid(),SIGUSR2);
  75.             }
  76.  
  77.             break;
  78.         default: //Pere
  79.             receive2 = 1;
  80.             printf("Le pere engage le match\n");
  81.             fflush(stdout);
  82.  
  83.             while(*scorePlayer1 <= 13 && *scorePlayer2 <= 13)
  84.             {
  85.  
  86.                     while(receive2 == 0) //Permet de synchroniser les process
  87.                     {}
  88.                     if(*scorePlayer1 == 13)
  89.                     {
  90.                         printf("J'ai gagné ! pourquoi ai-je fait un enfant aussi nul ?\n");
  91.                         fflush(stdout);
  92.                         kill(pid,SIGTERM);
  93.                         exit(EXIT_SUCCESS);
  94.                     }
  95.                     frappe = (rand() % 100);
  96.                     if(frappe < 50) //Reussis son coup
  97.                     {
  98.  
  99.                     }
  100.                     else //Rate la balle
  101.                     {
  102.                         printf("Le joueur pere loupe sa frappe, le fils gagne 1 point\n");
  103.                         fflush(stdout);
  104.                         *scorePlayer2 = *scorePlayer2 +1;
  105.                         printf("Score actuel %d (pere) à %d (fils) \n",*scorePlayer1,*scorePlayer2);
  106.                         fflush(stdout);
  107.                     }
  108.                     printf("PING \n");
  109.                     fflush(stdout);
  110.                     receive2 = 0;
  111.                     sleep(1);
  112.                     kill(pid,SIGUSR1);
  113.                
  114.  
  115.         }
  116.  
  117.  
  118.         break;
  119.  
  120.  
  121.     }
  122.  
  123.  
  124.  
  125.     return 0;
  126. }
  127.  
  128. void SIGUSR1_handler(int signum)
  129. {
  130.     if(signum == SIGUSR1)
  131.     {
  132.             receive1 = 1;
  133.     }
  134.  
  135. }
  136. void SIGUSR2_handler(int signum)
  137. {
  138.     if(signum == SIGUSR2)
  139.     {
  140.         receive2 = 1;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment