rw_mutex.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Assignment 4b : Reader and writer using mutex
  3. Name : Rushikeh Mukund Fanse
  4. Roll No : 16
  5. Div : H
  6. */
  7. #include<iostream>
  8. #include<cstdio>
  9. #include<cstdlib>
  10. #include<pthread.h>
  11. #include<unistd.h>
  12. using namespace std;
  13. pthread_mutex_t rc_mutex,w_mutex;
  14. int rc;
  15. void *reader_lock(int t)
  16. {
  17. printf("reader %d is trying to enter\n",t);
  18. pthread_mutex_lock(&rc_mutex);
  19. rc++;
  20. printf("reader %d inside the library\n",t);
  21. if(rc==1)
  22. {
  23. printf("readers reading the book\n");
  24. pthread_mutex_lock(&w_mutex);
  25. }
  26. pthread_mutex_unlock(&rc_mutex);
  27. }
  28. void *reader_unlock(int t)
  29. {
  30. pthread_mutex_lock(&rc_mutex);
  31. rc--;
  32. printf("reader %d left the library\n",t);
  33. if(rc==0)
  34. {
  35. printf("no readers reading the book\n");
  36. pthread_mutex_unlock(&w_mutex);
  37. }
  38. pthread_mutex_unlock(&rc_mutex);
  39. }
  40. void *writer_lock()
  41. {
  42. printf("----------------------writer is trying to enter in library\n");
  43. pthread_mutex_lock(&w_mutex);
  44. printf("----------------------writers is writing the book\n");
  45. }
  46. void *writer_unlock()
  47. {
  48. pthread_mutex_unlock(&w_mutex);
  49. printf("----------------------writer is done writing he leaves\n");
  50. }
  51. void *reader(void*i)
  52. {
  53. int tid;
  54. tid=(long)i;
  55. reader_lock(tid);
  56. sleep(2);
  57. reader_unlock(tid);
  58. }
  59. void *writer(void*)
  60. {
  61. writer_lock();
  62. sleep(2);
  63. writer_unlock();
  64. }
  65. int main()
  66. {
  67. pthread_t t1[5],t2;
  68. int i,j=1;
  69. pthread_mutex_init(&rc_mutex,NULL);
  70. pthread_mutex_init(&w_mutex,NULL);
  71. for(i=0;i<5;i++)
  72. pthread_create(&t1[i],NULL,reader,(void*)(intptr_t)i);
  73. pthread_create(&t2,NULL,writer,NULL);
  74. for(i=0;i<5;i++)
  75. pthread_join(t1[i],NULL);
  76. pthread_join(t2,NULL);
  77. pthread_mutex_destroy(&rc_mutex);
  78. pthread_mutex_destroy(&w_mutex);
  79. return 0;
  80. }
  81. /*
  82. rishi@rishi-PC:~$ ./a.out
  83. reader 1 is trying to enter
  84. reader 2 is trying to enter
  85. reader 0 is trying to enter
  86. reader 3 is trying to enter
  87. reader 4 is trying to enter
  88. reader 1 inside the library
  89. readers reading the book
  90. ----------------------writer is trying to enter in library
  91. reader 2 inside the library
  92. reader 0 inside the library
  93. reader 3 inside the library
  94. reader 4 inside the library
  95. reader 1 left the library
  96. reader 2 left the library
  97. reader 3 left the library
  98. reader 4 left the library
  99. reader 0 left the library
  100. no readers reading the book
  101. ----------------------writers is writing the book
  102. ----------------------writer is done writing he leaves
  103. */