Advertisement
k0mZ

Untitled

Dec 21st, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. /*Pristup bazi podataka obavlja se radi upisa i čitanja od strane više procesa.
  2.  
  3. U jednom trenutku može*/
  4.  
  5. /*postojati više procesa koji čitaju sadržaj iz baze podataka procedurom read_database(), ali ako jedan*/
  6. /*proces upisuje sadržaj u bazu podataka procedurom write_database(), nijednom drugom procesu nije*/
  7. /*dozvoljen pristup bazi podataka radi upisa i čitanja.
  8.  
  9. Prednost imaju procesi koji čitaju sadržaj, tako da*/
  10. /*dok god ima procesa koji čitaju iz baze podataka, proces koji treba da upisuje podatke mora da čeka.*/
  11.  
  12. /*Korišćenjem programskog jezika C napisati Linux program koji korišćenjem procesa i poznatih IPC*/
  13. /*mehanizama simulira prethodno opisani algoritam. (Sinhronizacion problem Čitaoci – pisci).*/
  14.  
  15. #include <stdio.h>
  16. #include <pthread.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <semaphore.h>
  20. #define N 20
  21.  
  22. char baza[N][2];
  23.  
  24. pthread_mutex_t mutex;
  25.  
  26. pthread_cond_t notEmpty;
  27. pthread_cond_t notFull;
  28.  
  29. int freeSpace = N;
  30.  
  31.  
  32. int head = 0;
  33. int tail = 0;
  34.  
  35. void* readDatabase(void* a)
  36. {
  37. while(1)
  38. {
  39. pthread_mutex_lock(&mutex);
  40.  
  41. while(freeSpace == N)
  42. pthread_cond_wait(&notEmpty,&mutex);
  43.  
  44. char t;
  45. t = baza[tail][0];
  46. tail = (tail+1) % N;
  47.  
  48. printf("procitano %c sa poz %d\n",t,tail);
  49. fflush(stdout);
  50.  
  51. freeSpace++;
  52.  
  53. pthread_cond_signal(&notFull);
  54. pthread_mutex_unlock(&mutex);
  55.  
  56. sleep(1);
  57.  
  58. }
  59.  
  60. }
  61.  
  62. void* writeDatabase(void* a)
  63. {
  64. while(1)
  65. {
  66.  
  67. pthread_mutex_lock(&mutex);
  68.  
  69. while(freeSpace <= 0)
  70. pthread_cond_wait(&notFull,&mutex);
  71.  
  72.  
  73. head = (head+1)%N;
  74. baza[head][0] = 'r';
  75. baza[head][1] = '\0';
  76.  
  77.  
  78. printf("Upisano u bazu %c na poz %d\n",'r',head);
  79. fflush(stdout);
  80.  
  81. freeSpace--;
  82.  
  83. pthread_cond_signal(&notEmpty);
  84. pthread_mutex_unlock(&mutex);
  85.  
  86. sleep(1);
  87. }
  88.  
  89. }
  90.  
  91. int main(int argc,char* argv[])
  92. {
  93. pthread_t nit1;
  94. pthread_t nit2;
  95. pthread_t nit3;
  96. pthread_t nit4;
  97.  
  98. pthread_t nit5;
  99. pthread_t nit6;
  100.  
  101. pthread_cond_init(&notEmpty,NULL);
  102. pthread_cond_init(&notFull,NULL);
  103. pthread_mutex_init(&mutex,NULL);
  104.  
  105.  
  106. pthread_create(&nit2,NULL,readDatabase,NULL);
  107. pthread_create(&nit1,NULL,writeDatabase,NULL);
  108. pthread_create(&nit3,NULL,writeDatabase,NULL);
  109. pthread_create(&nit4,NULL,writeDatabase,NULL);
  110.  
  111. pthread_create(&nit5,NULL,readDatabase,NULL);
  112. pthread_create(&nit6,NULL,readDatabase,NULL);
  113.  
  114. pthread_join(nit1,NULL);
  115. pthread_join(nit2,NULL);
  116. pthread_join(nit3,NULL);
  117. pthread_join(nit4,NULL);
  118.  
  119. pthread_mutex_destroy(&mutex);
  120. pthread_cond_destroy(&notEmpty);
  121.  
  122. pthread_cond_destroy(&notFull);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement