Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <pthread.h>
  6.  
  7. using namespace std;
  8.  
  9. int readers = 0;
  10. int writers = 0;
  11.  
  12. int write_active=0;
  13. int read_active=0;
  14.  
  15. int read_sleep=3;
  16. int write_sleep=3;
  17.  
  18. int allow_id=0;
  19.  
  20. int MAXTHREADS = 15;
  21.  
  22. pthread_mutex_t lock= PTHREAD_MUTEX_INITIALIZER;
  23.  
  24. pthread_cond_t read_lock;
  25. pthread_cond_t max_read_lock;
  26. pthread_cond_t write_lock;
  27. pthread_cond_t max_write_lock;
  28. pthread_cond_t read_active_lock;
  29. pthread_cond_t version2;
  30.  
  31. struct thread_data {
  32. int thread_id;
  33. int arrival_time;
  34. int reader0_writer1;
  35. thread_data(){thread_id=0;arrival_time=0;reader0_writer1=0;}
  36. };
  37.  
  38. void start_read(thread_data t) {
  39. pthread_mutex_lock(&lock);
  40. readers++;
  41. //cout << "****READ_start**** readers: " << readers<<"\n";
  42. while(t.thread_id != allow_id){
  43.     //cout <<"READ LOCK\n";
  44.     pthread_cond_wait(&version2,&lock);}
  45. while(readers>3){
  46.     pthread_cond_wait(&max_read_lock,&lock);}
  47. while(write_active>0){
  48.     pthread_cond_wait(&read_lock,&lock);}
  49. //cout <<"READ FREE\n";
  50. allow_id++;
  51. pthread_cond_broadcast(&version2);
  52. read_active++;
  53. pthread_mutex_unlock(&lock);
  54. }  
  55.  
  56. void finish_read(thread_data t) {
  57. pthread_mutex_lock(&lock);
  58. read_active--;
  59. readers--;
  60. //if(readers==0)
  61.     //pthread_cond_signal(&write_lock);
  62. if(readers<4)
  63.     pthread_cond_signal(&max_read_lock);
  64.  
  65. //cout<<"NEXT ID: "<<allow_id<<"\n";
  66.  
  67. pthread_cond_signal(&read_active_lock);
  68. //cout << "****READ_finish**** readers: " << readers<<"\n";
  69. pthread_mutex_unlock(&lock);
  70. }
  71.  
  72. void reading(thread_data t) {
  73. cout<<"thread id: "<<t.thread_id<<" has begun reading        [WAITING READERS: "<<readers<<"]\n";
  74. sleep(read_sleep);
  75. cout<<"thread id: "<<t.thread_id<<" has finished reading     [WAITING READERS: "<<readers-1<<"]\n";
  76.  
  77. }
  78.  
  79. void start_write(thread_data t) {
  80. pthread_mutex_lock(&lock);
  81. writers++;
  82. //cout << "****WRITE_start**** writers: " << writers<<"\n";
  83. while(t.thread_id != allow_id)
  84.     pthread_cond_wait(&version2,&lock);
  85. while(write_active>0)
  86.     pthread_cond_wait(&max_write_lock,&lock);
  87. while(read_active>0)
  88.     pthread_cond_wait(&read_active_lock,&lock);
  89. //while(readers>0)
  90.     //pthread_cond_wait(&write_lock,&lock);
  91. allow_id++;
  92. pthread_cond_broadcast(&version2);
  93. write_active=1;
  94. pthread_mutex_unlock(&lock);
  95. }
  96.  
  97. void writing(thread_data t) {
  98. cout<<"thread id: "<<t.thread_id<<" has begun writing        [WAITING WRITERS: "<<writers<<"]\n";
  99. sleep(write_sleep);
  100. cout<<"thread id: "<<t.thread_id<<" has finished writing     [WAITING WRITERS: "<<writers-1<<"]\n";
  101. }
  102.  
  103. void finish_write(thread_data t) {
  104. pthread_mutex_lock(&lock);
  105. write_active=0;
  106. writers--;
  107. pthread_cond_signal(&max_write_lock);
  108. pthread_cond_signal(&read_lock);
  109. //cout <<"READ UNLOCK\n";
  110. //cout << "****WRITE_finish**** writers: " << writers<<"\n";
  111.  
  112. //cout<<"NEXT ID: "<<allow_id<<"\n";
  113.  
  114. pthread_mutex_unlock(&lock);
  115. }
  116.  
  117. void *read(void *THR) {
  118.     thread_data *t=(thread_data *)THR;
  119.     start_read(*t);
  120.     reading(*t);
  121.     finish_read(*t);
  122. }
  123.  
  124. void *write(void *THR) {
  125.     thread_data *t=(thread_data *)THR;
  126.     start_write(*t);
  127.     writing(*t);
  128.     finish_write(*t);
  129. }
  130.  
  131. int main()
  132. {
  133. int rc=0;
  134. int i;
  135. int thread_count=0;
  136. ifstream infile("/home/mborovik/Documents/arrivalfile.txt");
  137. thread_data threader[MAXTHREADS];
  138.  
  139. pthread_t p[MAXTHREADS];
  140.  
  141. while(!infile.eof()) {
  142.     infile >> threader[thread_count].thread_id;
  143.     infile >> threader[thread_count].arrival_time;
  144.     infile >> threader[thread_count].reader0_writer1;
  145.         thread_count++;
  146. }
  147.  
  148. for (i=0;i<MAXTHREADS;i++){
  149.     if(i>0)
  150.     {
  151.         sleep(threader[i].arrival_time - threader[i-1].arrival_time);
  152.     }
  153.  
  154.     if (threader[i].reader0_writer1==0){
  155.         rc=pthread_create(&p[i],NULL,read,&threader[i]);
  156.     }
  157.  
  158.     if (threader[i].reader0_writer1==1){
  159.         rc=pthread_create(&p[i],NULL,write,&threader[i]);
  160.     }
  161.  
  162.     if(rc) { cout << "unable to create thread " << rc << "\n"; }
  163.     cout << "ID: " << threader[i].thread_id << " has arrived at time " << threader[i].arrival_time;
  164.     if(threader[i].reader0_writer1==0)
  165.         cout << " (reader)\n";
  166.     else
  167.         cout << " (writer)\n";
  168. }
  169.  
  170. for(i=0;i<MAXTHREADS;i++){
  171. pthread_join(p[i],NULL);
  172. }
  173.  
  174. pthread_mutex_destroy(&lock); // die die die!
  175. pthread_cond_destroy(&write_lock);
  176. pthread_cond_destroy(&read_lock);
  177. pthread_cond_destroy(&max_read_lock);
  178. pthread_cond_destroy(&max_write_lock);
  179.  
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement