Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <string>
- #include <pthread.h>
- using namespace std;
- int readers = 0;
- int writers = 0;
- int write_active=0;
- int read_active=0;
- int read_sleep=3;
- int write_sleep=3;
- int allow_id=0;
- int MAXTHREADS = 15;
- pthread_mutex_t lock= PTHREAD_MUTEX_INITIALIZER;
- pthread_cond_t read_lock;
- pthread_cond_t max_read_lock;
- pthread_cond_t write_lock;
- pthread_cond_t max_write_lock;
- pthread_cond_t read_active_lock;
- pthread_cond_t version2;
- struct thread_data {
- int thread_id;
- int arrival_time;
- int reader0_writer1;
- thread_data(){thread_id=0;arrival_time=0;reader0_writer1=0;}
- };
- void start_read(thread_data t) {
- pthread_mutex_lock(&lock);
- readers++;
- //cout << "****READ_start**** readers: " << readers<<"\n";
- while(t.thread_id != allow_id){
- //cout <<"READ LOCK\n";
- pthread_cond_wait(&version2,&lock);}
- while(readers>3){
- pthread_cond_wait(&max_read_lock,&lock);}
- while(write_active>0){
- pthread_cond_wait(&read_lock,&lock);}
- //cout <<"READ FREE\n";
- allow_id++;
- pthread_cond_broadcast(&version2);
- read_active++;
- pthread_mutex_unlock(&lock);
- }
- void finish_read(thread_data t) {
- pthread_mutex_lock(&lock);
- read_active--;
- readers--;
- //if(readers==0)
- //pthread_cond_signal(&write_lock);
- if(readers<4)
- pthread_cond_signal(&max_read_lock);
- //cout<<"NEXT ID: "<<allow_id<<"\n";
- pthread_cond_signal(&read_active_lock);
- //cout << "****READ_finish**** readers: " << readers<<"\n";
- pthread_mutex_unlock(&lock);
- }
- void reading(thread_data t) {
- cout<<"thread id: "<<t.thread_id<<" has begun reading [WAITING READERS: "<<readers<<"]\n";
- sleep(read_sleep);
- cout<<"thread id: "<<t.thread_id<<" has finished reading [WAITING READERS: "<<readers-1<<"]\n";
- }
- void start_write(thread_data t) {
- pthread_mutex_lock(&lock);
- writers++;
- //cout << "****WRITE_start**** writers: " << writers<<"\n";
- while(t.thread_id != allow_id)
- pthread_cond_wait(&version2,&lock);
- while(write_active>0)
- pthread_cond_wait(&max_write_lock,&lock);
- while(read_active>0)
- pthread_cond_wait(&read_active_lock,&lock);
- //while(readers>0)
- //pthread_cond_wait(&write_lock,&lock);
- allow_id++;
- pthread_cond_broadcast(&version2);
- write_active=1;
- pthread_mutex_unlock(&lock);
- }
- void writing(thread_data t) {
- cout<<"thread id: "<<t.thread_id<<" has begun writing [WAITING WRITERS: "<<writers<<"]\n";
- sleep(write_sleep);
- cout<<"thread id: "<<t.thread_id<<" has finished writing [WAITING WRITERS: "<<writers-1<<"]\n";
- }
- void finish_write(thread_data t) {
- pthread_mutex_lock(&lock);
- write_active=0;
- writers--;
- pthread_cond_signal(&max_write_lock);
- pthread_cond_signal(&read_lock);
- //cout <<"READ UNLOCK\n";
- //cout << "****WRITE_finish**** writers: " << writers<<"\n";
- //cout<<"NEXT ID: "<<allow_id<<"\n";
- pthread_mutex_unlock(&lock);
- }
- void *read(void *THR) {
- thread_data *t=(thread_data *)THR;
- start_read(*t);
- reading(*t);
- finish_read(*t);
- }
- void *write(void *THR) {
- thread_data *t=(thread_data *)THR;
- start_write(*t);
- writing(*t);
- finish_write(*t);
- }
- int main()
- {
- int rc=0;
- int i;
- int thread_count=0;
- ifstream infile("/home/mborovik/Documents/arrivalfile.txt");
- thread_data threader[MAXTHREADS];
- pthread_t p[MAXTHREADS];
- while(!infile.eof()) {
- infile >> threader[thread_count].thread_id;
- infile >> threader[thread_count].arrival_time;
- infile >> threader[thread_count].reader0_writer1;
- thread_count++;
- }
- for (i=0;i<MAXTHREADS;i++){
- if(i>0)
- {
- sleep(threader[i].arrival_time - threader[i-1].arrival_time);
- }
- if (threader[i].reader0_writer1==0){
- rc=pthread_create(&p[i],NULL,read,&threader[i]);
- }
- if (threader[i].reader0_writer1==1){
- rc=pthread_create(&p[i],NULL,write,&threader[i]);
- }
- if(rc) { cout << "unable to create thread " << rc << "\n"; }
- cout << "ID: " << threader[i].thread_id << " has arrived at time " << threader[i].arrival_time;
- if(threader[i].reader0_writer1==0)
- cout << " (reader)\n";
- else
- cout << " (writer)\n";
- }
- for(i=0;i<MAXTHREADS;i++){
- pthread_join(p[i],NULL);
- }
- pthread_mutex_destroy(&lock); // die die die!
- pthread_cond_destroy(&write_lock);
- pthread_cond_destroy(&read_lock);
- pthread_cond_destroy(&max_read_lock);
- pthread_cond_destroy(&max_write_lock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement