reader/ writer problem using semaphores and test and set

Hello,

I am working on a project.. I have a reader writer program I am writing but we are not allowed to use mutex only binary semaphore with test and set. I created the test and set function but when it comes to implement it using the semaphore for the reader and writer I am not sure how to go about and implement the p(s) and v(s). I am not asking anyone to do the work but maybe you can tell me what I am doing wrong and where I should focus my efforts. The program has to be a giant switch statement. The object is to implement several readers and writers using synchronization where more than on reader can be in the critical section at the same time, only one writer can be in the critical section at a time. Each process should be context switched out which is shy we are using the switch statements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  typedef struct {
  int S = 0;	
  void p();// wait
  void v();//signal
} s;//semaphore
 
int counter, r1counter=0, r2counter=0, r3counter=0, wrt1counter=0, wrt2counter=0;// shared 

bool Test_and_set(bool *target){
 	bool rv= *target;// old value,  is it true or false?
	*target = true; // new value set to true
	 return rv; // return original value, returne if the flag was initially true or false
} 

bool lock = true;
   
void p(s){//wait
	while(true){
	  	while (Test_and_set(&lock)){
               ; /* do nothing */
               lock=false;
               s--;
 	   }      	
  	}       
}
   
void v(s){//signal
	 
   	   while(Test_and_set(&lock))//check the door...
           ; /* if true do nothing-*/
        lock= true
        s++;
}      
//(Reader 1 function)
int r1(){
        switch (r1counter){
	          case 0: while (true){
   	               p(s);//check the door, if locked do nothing and come back
   	                lock = false;
   	                // entering the critical section and lock the door
   	            cout<<"I am the reader."<<endl; 
   	                v(s)// leave the critical section and unlock the door
                }	   
	r1counter++ ;  	
	        break;
          
}
//(Writer 1 function)
int wrt(){
        switch (wrt1counter){
	          case 0: while (true)
		      p(s);  
                      // here the writer is checking if anyone is in the critical section
// if not it enters the critical section and says..
		      cout<<"I am writing in the CS, there is no one else here "<<endl;
              v(s);// here, once it leaves the critical section it should "unlock" the door so another reader or writer can enter the critical section
	        }
   wrt1counter++
} 


int main() {

cout<<"Reader Writer problem"<<endl;

	while (true){
		for (int i = 0; i<5000000;i++){
			
    		int coin = rand()%5;
    		
		       switch (coin){
	         		case 0: r1();
	        			   break;
	        	//	case 1: r2();
	        	//		   break;
	        	//	case 2: r3();
	          	//			break;
	        	//	case 3: wrt1();
	           	//			break;  
            	      //	case 3: wrt2();
	           	//			break;  
			   }       
	    }
          
   }  
system ("pause");
return 0;
}
Topic archived. No new replies allowed.