fork download
  1. #include<stdio.h>
  2. #include<pthread.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5.  
  6. pthread_mutex_t mutex, wrt;
  7. int s, rcount = 0;
  8.  
  9. void *writer(void *arg){
  10. pthread_mutex_lock(&wrt);
  11. int n = rand() % 10;
  12. int d = ((int)arg);
  13. printf("--------------------------------------------------\n");
  14. printf("W%d Wait for Random time between 0ns and 10ns = %d\n", d, n);
  15. sleep(n);
  16. printf("Enter the number of time W%d want to write:\n", d);
  17. int t;
  18. scanf("%d", &t);
  19. printf("Now W%d is writing... i.e. ADDING...\n", d);
  20. int j;
  21. for(j=0; j<t; j++){
  22. printf("Enter the %dth INTEGER value to write:\n", (j+1));
  23. int u;
  24. scanf("%d", &u);
  25. s = s + u;
  26. }
  27. printf("UPDATED value of Shared variable = %d \n", s);
  28. printf("--------------------------------------------------\n");
  29. pthread_mutex_unlock(&wrt);
  30. }
  31.  
  32. void *reader(void *arg){
  33. //Entry Part
  34. pthread_mutex_lock(&mutex);
  35. rcount++;
  36. if(rcount==1){
  37. pthread_mutex_lock(&wrt);//No writer should come
  38. }
  39. pthread_mutex_unlock(&mutex);//so next reader can come
  40. //Exit Part
  41. int n = rand() % 10;
  42. int d = ((int)arg);
  43. printf("R%d wait for Random time between 0ns and 10ns = %d\n", d, n);
  44. sleep(n);
  45. printf("Enter the number of time R%d want to read:\n", d);
  46. int t;
  47. scanf("%d", &t);
  48. printf("Now R%d is reading....\n", d);
  49. int j;
  50. for(j=0; j<t; j++){
  51. printf("R%d read the shared value = %d\n", d, s);
  52. }
  53. printf("Number of Readers present = %d\n", rcount);
  54. pthread_mutex_lock(&mutex);
  55. rcount--;
  56. if(rcount==0){//Now writer can come if they want
  57. pthread_mutex_unlock(&wrt);
  58. }
  59. pthread_mutex_unlock(&mutex);
  60. }
  61.  
  62. void main(){
  63. printf("Enter the 'INTEGER' Initial value of share variable: \n");
  64. scanf("%d", &s);
  65. printf("---------------------------------------------\n");
  66. int rn, wn, i;
  67. printf("Enter the no. of Reader:\n");
  68. scanf("%d", &rn);
  69. for(i=0; i<rn; i++){
  70. printf("R%d\n", i);
  71. }
  72. printf("---------------------------------------------\n");
  73. printf("Enter the no. of Writer:\n");
  74. scanf("%d", &wn);
  75. for(i=0; i<wn; i++){
  76. printf("W%d\n", i);
  77. }
  78. printf("---------------------------------------------\n");
  79.  
  80. pthread_t r[rn], w[wn];
  81. pthread_mutex_init(&wrt, NULL);
  82. pthread_mutex_init(&mutex, NULL);
  83.  
  84. if(rn<0 || wn<0){
  85. printf("Sorry: You have Entered NEGATIVE number of READER | WRITER\n");
  86. printf("Program is Terminating....\n");
  87. return;
  88. }else if(rn == 0){
  89. printf("Sorry: You have not taken any READER\n");
  90. printf("No READER thread will be creaded\n");
  91. }else if(wn == 0){
  92. printf("Sorry: You have not taken any WRITER\n");
  93. printf("No WRITER thread will be creaded\n");
  94. }else{
  95. printf("Thread Creating....\n");
  96. }
  97. printf("---------------------------------------------\n");
  98.  
  99. if(wn==rn){
  100. for(i=0; i<wn; i++){
  101. pthread_create(&w[i], NULL, &writer, (int *)i);
  102. pthread_create(&r[i], NULL, &reader, (int *)i);
  103. }
  104. for(i=0; i<wn; i++){
  105. pthread_join(w[i], NULL);
  106. pthread_join(r[i], NULL);
  107. }
  108. }else if(wn>rn){
  109. for(i=0; i<rn; i++){
  110. pthread_create(&w[i], NULL, &writer, (int *)i);
  111. pthread_create(&r[i], NULL, &reader, (int *)i);
  112. }
  113. for(i=rn; i<wn; i++){
  114. pthread_create(&w[i], NULL, &writer, (int *)i);
  115. }
  116. for(i=0; i<rn; i++){
  117. pthread_join(w[i], NULL);
  118. pthread_join(r[i], NULL);
  119. }
  120. for(i=rn; i<wn; i++){
  121. pthread_join(w[i], NULL);
  122. }
  123. }else{
  124. for(i=0; i<wn; i++){
  125. pthread_create(&w[i], NULL, &writer, (int *)i);
  126. pthread_create(&r[i], NULL, &reader, (int *)i);
  127. }
  128. for(i=wn; i<rn; i++){
  129. pthread_create(&r[i], NULL, &reader, (int *)i);
  130. }
  131. for(i=0; i<wn; i++){
  132. pthread_join(w[i], NULL);
  133. pthread_join(r[i], NULL);
  134. }
  135. for(i=wn; i<rn; i++){
  136. pthread_join(r[i], NULL);
  137. }
  138. }
  139. printf("-------------After joining the thread---------\n");
  140. printf("Final value of share variable = %d\n", s);
  141. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
Enter the 'INTEGER' Initial value of share variable: 
---------------------------------------------
Enter the no. of Reader:
---------------------------------------------
Enter the no. of Writer:
---------------------------------------------
Sorry: You have not taken any READER
No READER thread will be creaded
---------------------------------------------
-------------After joining the thread---------
Final value of share variable = 0