Guest User

Untitled

a guest
Feb 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/sem.h>
  5.  
  6. #if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)
  7. /* jest zdefiniowane w sys/sem.h */
  8. #else
  9. union semun
  10. {
  11.         int val;                        //value for SETVAL
  12.         struct semid_ds *buf;        //buffer for IPC_STAT, IPC_SET
  13.         unsigned short int *array;        //array for GETALL, SETALL
  14.         struct seminfo *__buf;        //buffer for IPC_INFO
  15. }ustaw;
  16.  
  17. int ptr[5];
  18. #endif
  19.  
  20.  
  21. #define TRZYMA_WIDELEC 1
  22. #define CZEKA 2
  23. #define POSILEK 3
  24. #define ODPOCZYWA 4
  25.  
  26. int main()
  27. {
  28.         int i=0, end=1, semafor, widelec =0;
  29.         key_t klucz;
  30.         struct sembuf operacja;
  31.  
  32.         klucz = ftok( ".", 'a');
  33.         semafor = semget( klucz , 5, IPC_CREAT | 0666 );
  34.  
  35.         ustaw.val = 1;
  36.         semctl(semafor, 1, SETALL, ustaw.val);
  37.         for(i=0; i<5; i++)
  38.         {
  39.                 semctl(semafor, i, GETVAL, ustaw);
  40.                 printf("%d", ustaw.val);
  41.         }
  42.  
  43.         for(i=0;i<5;i++)
  44.         {
  45.                 ptr[i]=CZEKA;
  46.         }
  47.  
  48.         for (i=0; i< 5; i++)
  49.         {
  50.                 if( fork() == 0)
  51.                 {
  52.                         while(end==1)
  53.                         {
  54.                                 printf("%d\n", i);
  55.                         //      if(ptr[i] == CZEKA)
  56.                         //      {
  57.                                         semctl(semafor, i, GETVAL, ustaw);
  58.                                         if(ustaw.val ==1)
  59.                                         {
  60.                                                             if (semop(semafor, &operacja, 1) == -1)
  61.                                                             {
  62.                                                                 fprintf(stderr, "blad blokowania semafora\\n");
  63.                                                                 exit(1);
  64.                                                                 }
  65.  
  66.                                                 operacja.sem_num = i;
  67.                                                 operacja.sem_op = -1;        //zablokuj
  68.                                                 operacja.sem_flg = 0;        //operacja blokujaca
  69.  
  70.                                                            if (semop(semafor, &operacja, 1) == -1)
  71.                                                             {
  72.                                                                 fprintf(stderr, "blad blokowania semafora\\n");
  73.                                                                 exit(1);
  74.                                                                 }
  75.                                 //              ptr[i] == TRZYMA_WIDELEC;
  76.                                                 printf ("mi siÄ udalo\n");
  77.                                         }
  78.                                         else
  79.                                         {
  80.                                                 printf("mi sie nie udlo\n");
  81.                                 //              sleep(2);
  82.                                         }
  83.                                 //sprawdz czy moĹźesz zabrac widelec
  84.                                 //nr i
  85.                                 //jak nie to czeka
  86.  
  87.                         //      }
  88.                         //      else if(ptr[i] == TRZYMA_WIDELEC)
  89.                         //      {
  90.                                 //sprawdz czy moĹźesz zabrac widelec i+1
  91.                                 //jak nie to poczekaj, sprawdz ponownie
  92.                                 //jak nie to czekaj od nowa
  93.                         //      }
  94.                         //      else if(ptr[i] == POSILEK)
  95.                         //      {
  96.                                 //poczekaj na zjedzenie
  97.                                 //odkladaj widelce
  98.                                 //idz spac
  99.                         //      }
  100.                         //      else
  101.                         //      {
  102.                                 //pospij
  103.                                 //przejdz do czekaj
  104.                         //      }
  105. //                              operacja.sem_num = i;
  106. //                              operacja.sem_op = -1;        //zablokuj
  107. //                              operacja.sem_flg = 0;        //operacja blokujaca
  108. //                      //      semop(semafor, &opercaja,)
  109.                         //      sleep(1);
  110.                         }
  111.                 }
  112.         }
  113.         while(end ==1)
  114.         {
  115.                 for(i=0; i<5; i++)
  116.                         printf("Filozof nr %d jest w stanie %d\n", i, ptr[i]);
  117.                 sleep(1);
  118.         }
  119.  
  120.         return 1;
  121. }
Add Comment
Please, Sign In to add comment