Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4.  
  5.  
  6.  
  7. int czytelnicyK = 0, pisarzeK = 0, pisarzeB=0, czytelnicyB=0;
  8. int i;
  9.  
  10. pthread_mutex_t biblioteka;
  11. pthread_cond_t czytelnicy, pisarze;
  12.  
  13. void *czytelnik( ) {
  14. while(1)
  15. {
  16. pthread_mutex_lock(&biblioteka);
  17. czytelnicyK++;
  18. printf("ReaderQ: %d WriterQ: %d [In R: %d W: %d]\n",czytelnicyK,pisarzeK,czytelnicyB,pisarzeB);
  19. while (!(pisarzeK == 0))
  20. pthread_cond_wait(&czytelnicy, &biblioteka);
  21. pthread_mutex_unlock(&biblioteka);
  22. czytelnicyB++;
  23. printf("Czytelnik %d wchodzi...\n",czytelnicyB);
  24. czytelnicyK--;
  25. printf("ReaderQ: %d WriterQ: %d [In R: %d W: %d]\n",czytelnicyK,pisarzeK,czytelnicyB,pisarzeB);
  26. sleep(1);
  27. pthread_mutex_lock(&biblioteka);
  28. czytelnicyB--;
  29. printf(" Czytelnik %d wychodzi...\n",czytelnicyB + 1);
  30. printf("ReaderQ: %d WriterQ: %d [In R: %d W: %d]\n",czytelnicyK,pisarzeK,czytelnicyB,pisarzeB);
  31. if (czytelnicyB == 0)
  32. pthread_cond_signal(&pisarze);
  33. pthread_mutex_unlock(&biblioteka);
  34.  
  35. }
  36. }
  37.  
  38. void *pisarz() {
  39.  
  40. while(1)
  41. {
  42.  
  43. pthread_mutex_lock(&biblioteka);
  44. pisarzeK++;
  45. printf("ReaderQ: %d WriterQ: %d [In R: %d W: %d]\n",czytelnicyK,pisarzeK,czytelnicyB,pisarzeB);
  46. while (!((czytelnicyB == 0) && (pisarzeB == 0)))
  47. pthread_cond_wait(&pisarze, &biblioteka);
  48. pthread_mutex_unlock(&biblioteka);
  49. pisarzeK--;
  50. pisarzeB++;
  51. printf(" Pisarz %d wchodzi...\n",pisarzeK+1);
  52. printf("ReaderQ: %d WriterQ: %d [In R: %d W: %d]\n",czytelnicyK,pisarzeK,czytelnicyB,pisarzeB);
  53. sleep(1);
  54. pthread_mutex_lock(&biblioteka);
  55.  
  56. pisarzeB--;
  57. printf(" Pisarz %d wychodzi...\n",pisarzK+1);
  58. printf("ReaderQ: %d WriterQ: %d [In R: %d W: %d]\n",czytelnicyK,pisarzeK,czytelnicyB,pisarzeB);
  59. if(pisarzeK>0)
  60. pthread_cond_signal(&pisarze);
  61. else
  62. pthread_cond_broadcast(&czytelnicy);
  63. pthread_mutex_unlock(&biblioteka);
  64.  
  65. }
  66. }
  67.  
  68. int main(int argc, char* argv[]) {
  69.  
  70. int ilosc_czyt=atoi(argv[1]), ilosc_pis=atoi(argv[2]);
  71. pthread_t c[ilosc_czyt], p[ilosc_pis];
  72.  
  73. pthread_mutex_init(&biblioteka, 0);
  74.  
  75.  
  76. pthread_cond_init(&czytelnicy, 0);
  77. pthread_cond_init(&pisarze, 0);
  78. for(i=0; i<ilosc_czyt; i++)
  79. pthread_create(&c[i], NULL, czytelnik, NULL);
  80. for(i=0; i<ilosc_pis; i++)
  81. pthread_create(&p[i], NULL, pisarz, NULL);
  82.  
  83. for(i=0; i<ilosc_pis; i++)
  84. pthread_join(p[i], NULL);
  85. for(i=0; i<ilosc_czyt; i++)
  86. pthread_join(c[i], NULL);
  87. pthread_cond_destroy(&czytelnicy);
  88. pthread_cond_destroy(&pisarze);
  89. pthread_mutex_destroy(&biblioteka);
  90.  
  91. return 0;
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement