Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <semaphore.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <pthread.h>
  7. #include <fcntl.h>
  8. #include <semaphore.h>
  9. #include <signal.h>
  10. #include <unistd.h>
  11.  
  12. #define N 5
  13. #define THINKING 0
  14. #define HUNGRY 1
  15. #define EATING 2
  16. #define LEFT ( i + N - 1) % N
  17. #define RIGHT ( i + 1 ) % N
  18.  
  19. pthread_t p1, p2, c1, c2, c3; // uchwyty do watkow
  20.  
  21. volatile int index_filozofa;
  22.  
  23. int state[N];
  24. sem_t *mutex;
  25. sem_t *stol[N];
  26. sem_t *tworzenie_filozofow_mutex;
  27.  
  28. void handler(int sig);
  29.  
  30. void take_forks(int i, int id);
  31. void put_forks(int i);
  32. void test(int i);
  33. void *filozof();
  34. void filozofowanie(int id, int i);
  35. void think(int id);
  36. void eat(int id);
  37.  
  38. void *filozof() {
  39.     int id = (rand() + 23) % 15;
  40.  
  41.     // tylko na potrzeby parametryzowania zmienna index_filozofa
  42.     sem_wait(tworzenie_filozofow_mutex);
  43.     sem_post(tworzenie_filozofow_mutex);
  44.  
  45.     filozofowanie(id, index_filozofa);
  46. }
  47.  
  48. void filozofowanie(int id, int i) {
  49.     while (1) {
  50.         think(id);
  51.         take_forks(i, id);
  52.         eat(id);
  53.         put_forks(i);
  54.     }
  55. }
  56.  
  57. void take_forks(int i, int id) {
  58.     sem_wait(mutex);
  59.     state[i] = HUNGRY;
  60.     printf("Filozof: %d głodny\n", id);
  61.     test(i);
  62.     sem_post(mutex);
  63.     sem_wait(stol[i]);
  64. }
  65.  
  66. void put_forks(int i) {
  67.     sem_wait(mutex);
  68.     state[i] = THINKING;
  69.     test(LEFT);
  70.     test(RIGHT);
  71.     sem_post(mutex);
  72. }
  73.  
  74. void test(int i) {
  75.     if (state[i] == HUNGRY &&
  76.         state[LEFT] != EATING &&
  77.         state[RIGHT] != EATING) {
  78.  
  79.         state[i] = EATING;
  80.         sem_post(stol[i]);
  81.     }
  82. }
  83.  
  84. void think(int id) {
  85.     int sec = rand() % 3;
  86.     printf("Filozof: %d myśli przez %d sekund\n", id, sec);
  87.     sleep(sec);
  88.     printf("Filozof: %d przestał myśleć\n", id);
  89. }
  90.  
  91. void eat(int id) {
  92.     int sec = rand() % 5;
  93.     printf("Filozof: %d zajada sie przez %d sekund\n", id, sec);
  94.     sleep(sec);
  95.     printf("Filozof: %d przestał jesc\n", id);
  96. }
  97.  
  98. int main() {
  99.     int i;
  100.     char *sems[5];
  101.     srand(time(NULL));
  102.  
  103.     // nazwy dla filozofow, nie robione automatycznie bo za duzo roboty w porownaniu do zysku ze sklejaniem i zapamietywaniem stringow z liczbami w C
  104.     sems[0] = "a1";
  105.     sems[1] = "b1";
  106.     sems[2] = "c1";
  107.     sems[3] = "d1";
  108.     sems[4] = "e1";
  109.  
  110.     index_filozofa = 0;
  111.  
  112.     signal(SIGINT, handler);
  113.  
  114.     if ((mutex = sem_open ("/filomutex", O_CREAT, 0660, 1)) == SEM_FAILED) {
  115.         perror ("sem_czytania sem_open"); exit (1);
  116.     }
  117.  
  118.     if ((tworzenie_filozofow_mutex = sem_open("/tworzeniefilo", O_CREAT, 0660, 1)) == SEM_FAILED) {
  119.         perror("filozofowie sem_open"); exit(1);
  120.     }
  121.  
  122.     // mutex na aktualizacje zmiennej liczba_czytelnikow
  123.     for (i = 0; i < 5; ++i) {
  124.         if ((stol[i] = sem_open (sems[i], O_CREAT, 0660, 1)) == SEM_FAILED) {
  125.             perror ("/sstol sem_open"); exit (1);
  126.         }
  127.     }
  128.  
  129.     sem_wait(tworzenie_filozofow_mutex);
  130.  
  131.     pthread_create(&p1, NULL, filozof, NULL);
  132.     ++index_filozofa;
  133.     pthread_create(&p2, NULL, filozof, NULL);
  134.     ++index_filozofa;
  135.     pthread_create(&c1, NULL, filozof, NULL);
  136.     ++index_filozofa;
  137.     pthread_create(&c2, NULL, filozof, NULL);
  138.     ++index_filozofa;
  139.     pthread_create(&c3, NULL, filozof, NULL);
  140.  
  141.     sem_post(tworzenie_filozofow_mutex);
  142.  
  143.     pthread_join(p1, NULL);
  144.     pthread_join(p2, NULL);
  145.     pthread_join(c1, NULL);
  146.     pthread_join(c2, NULL);
  147.     pthread_join(c3, NULL);
  148.  
  149.     sem_close(tworzenie_filozofow_mutex);
  150.     sem_close(mutex);
  151.     for (i = 0; i < 5; ++i) {
  152.         sem_close(stol[i]);
  153.         sem_unlink(sems[i]);
  154.         sem_destroy(stol[i]);
  155.     }
  156.  
  157.     sem_unlink("/tworzeniefilo");
  158.     sem_unlink("/filomutex");
  159.  
  160.     sem_destroy(tworzenie_filozofow_mutex);
  161.     sem_destroy(mutex);
  162.  
  163.     return 0;
  164. }
  165.  
  166. void handler(int sig) {
  167.     pthread_cancel(p1);
  168.     pthread_cancel(p2);
  169.     pthread_cancel(c1);
  170.     pthread_cancel(c2);
  171.     pthread_cancel(c3);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement