Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. /*
  2. Question Name:Readers Writer Problem
  3. Submitted By:Zerk Shaban
  4. Roll Number:P14-6012
  5. Section:CS-A
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <pthread.h>
  11. #include <semaphore.h>
  12. #include <unistd.h>
  13. //Declaring the objects of type semaphore
  14. sem_t writer,reader,mutex;
  15. //declared the readercount varible to manage multiple users to read from same file
  16. // will be more cleared when I will explain its use while using below
  17. int readercount=0;
  18. //buffer where we are going to wrtie the data and from where the data will be read
  19. char buf[30];
  20. //function through which we will write in buffer it is invoked by thread1 object
  21. //below
  22. void *Write(){
  23. //as per the rules of the probles only one writer can write into a file at one time
  24. //so we will put the lock on the critical section of writing
  25. sem_wait(&writer);
  26. //writing into the file
  27. sprintf(buf,"%s","My name is zerk shaban");
  28. //after we havee written the data into the file we will exit the file or other words
  29. //we will unlock the lock
  30. sem_post(&writer);
  31. }
  32. //function that will be used to read from the buffer
  33. void *Read(){
  34. //as we face the proble which letting multiple threads to use from the same file so first
  35. //we will lock the whole critical section
  36. sem_wait(&mutex);
  37. //we have to keep the record of the first user and the other users so the first user can read
  38. //only when the value of readercount equals to one for that we will increment the value from
  39. //zero to one
  40. readercount++;
  41. //to seprate the first reader from the others we will pu the first one into lock
  42. sem_wait(&reader);
  43. if(readercount==1){
  44. printf("First reader reading %s\n",buf);
  45. }
  46. //after first read have read from the file as we know for reading of first user the value
  47. //of the reader count should be 1 so we will decrement the value to zero because the
  48. //first user is done with the reading
  49. readercount--;
  50. //after reading the data we will unlock the critical section
  51. sem_post(&reader);
  52. //as the value would have deacred from one to zero now other users will be allowed to read from
  53. // the same file at the same time
  54. if(readercount==0){
  55. printf("Other reader reading the same file %s\n",buf);
  56. }
  57. sem_post(&mutex);
  58. }
  59.  
  60. int main(){
  61. //initlizing the values to the objects
  62. sem_init(&mutex,0,1);
  63. sem_init(&writer,0,1);
  64. sem_init(&reader,0,1);
  65. //Declaring the variables for threads
  66. pthread_t thread1,thread2;
  67. //creating the threads for invoking the write and read function
  68. pthread_create(&thread1,NULL,Write,NULL);
  69. pthread_create(&thread2,NULL,Read,NULL);
  70. //putting the join for threads
  71. pthread_join(thread1,NULL);
  72. pthread_join(thread2,NULL);
  73. //after using we will reallocate the momory that was given to it
  74. sem_destroy(&mutex);
  75. sem_destroy(&reader);
  76. sem_destroy(&writer);
  77.  
  78. }
  79.  
  80. Record Page AdsClose
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement