Priority help

i have to create a scenario of 5 readers and 3 writers trying to access the critical section. The conditions of readers and writers is that there can only be 3 readers in the critical section at once while there can be only 1 writer in at once and they cant be mixed together. Each of the reader and writers have switch case statements in them so that they break after a certain amount of statements have completed to simulate context switching.
For some reason once a reader gets inside a writer never will get it causing the writer to starve and readers to continue entering and exiting.

for example of what should happen,
R1,R2,R3 enter critical section W1 is called next in line after a reader is already inside,
since there are 3 readers in the critical section the W1 is blocked.
Once R1, R2, R3 are called 4 times they leave the critical section, then W1 can enter.
Note: there is R4, R5 and W2,W3 that can also be called
If R4 or R5 is called after W1 is called while being blocked, R4 or R5 is blocked till W1 enters critical section and exits.
If W2 or W3 is called after W1 is called while being blocked, they will wait in line behind W1 has entered and exit the critical(which means called 3 times by the main function)

main.cpp
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
#include "cs.h"
#include<time.h>
#include<iostream>

using namespace std;


std::ofstream outf;
int main ()
{
	
	outf.open("Results.txt");	
	int random;
	int i;
	srand(unsigned(NULL));

	for ( i = 0;i < 200;i++ )
	{
	
	random = rand() %8;
	
		switch (random)
		{
		case 0 : 
			reader1();
				 break;
		case 1 : 			
			reader2();
				 break;
		case 2 : 			
			reader3();
				 break;
		case 3 : 			
			reader4();
				 break;
		case 4 : 			
			reader5();
				 break;
		case 5 : 			
			writer1();
				 break;
		case 6 : 			
			writer2();
				 break;
		case 7 : 			
			writer3();
				 break;
		}

		}

	
outf.close();
system("pause");
return 0;
}


functions.cpp

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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "cs.h"
#include <iostream>

	int mutex = 1;
	int wrt = 1;
	int readcount=0;
	int writecount=0;
	int reader1count=0;
	int reader2count=0;
	int reader3count=0;
	int reader4count=0;
	int reader5count=0;
	int writer1count=0;
	int writer2count=0;
	int writer3count=0;

		
//READER 1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void reader1()
{
	switch (reader1count)
	{
		case 0:
		outf << "reader1 wants to access Critical Section\n";
		P(mutex);
		readcount ++;
		reader1count++;
			break;
		case 1:
		if(readcount ==0)
		{
			outf << "reader1 is the first and only reader\n";
			P(wrt);
		}
		reader1count++;
		outf<<"reader1 is entering the Critical Section\n";
		V(mutex);
		reader1count++;
			break;
		case 2: 
		if(readcount<4)
		{
		if (writecount<1)
		{
		if(readcount>=4)
		{
			outf << "PANIC:reader1 sees four or more Readers\n";
			break;
		}
		if (writecount>=1)
		{
			outf << "PANIC:reader1 sees a Writer\n";
			break;
		}
		// Crictal Section
		outf<<"reader1 is inside the Critical Section\n";
		if(readcount>=2)
		{
			outf << "reader1 sees "<<readcount<<" Readers inside the Critical Section\n";
		}		
		//Crictal Section
		reader1count++;
		}
		}
		else 
			reader1count--;
			break;
		case 3:
		P(mutex);
		readcount --;
		if (readcount==0)
		{
			V(wrt);
			outf << "Room is empty after reader1 leaves\n";
		}
		outf << "reader1 is exitting the Critical Section\n";
		V(mutex);
		reader1count=0;
			break;
	}
}

//READER 2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void reader2()
{
	switch (reader2count)
	{
		case 0:
		outf << "reader2 wants to access Critical Section\n";
		P(mutex);
		readcount ++;
		reader2count++;
			break;
		case 1:
		if(readcount ==0)
		{
			outf << "reader2 is the first and only reader\n";
			P(wrt);
		}
		reader2count++;
		outf<<"reader2 is entering the Critical Section\n";
		V(mutex);
		reader2count++;
			break;
		case 2: 
		if(readcount<4)
		{
		if (writecount<1)
		{
		if(readcount>=4)
		{
			outf << "PANIC:reader2 sees four or more Readers\n";
			break;
		}
		if (writecount>=1)
		{
			outf << "PANIC:reader2 sees a Writer\n";
			break;
		}
		// Crictal Section
		outf<<"reader2 is inside the Critical Section\n";
		if(readcount>=2)
		{
			outf << "reader2 sees "<<readcount<<" Readers inside the Critical Section\n";
		}		
		//Crictal Section
		reader2count++;
		}
		}
		else 
			reader2count--;
			break;
		case 3:
		P(mutex);
		readcount --;
		if (readcount==0)
		{
			V(wrt);
			outf << "Room is empty after reader2 leaves\n";
		}
		outf << "reader2 is exitting the Critical Section\n";
		V(mutex);
		reader2count=0;
			break;
	}
}
	


//WRITER 1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void writer1()
{
	switch (writer1count)
	{
		case 0 :
		outf <<" writer1 wants to access the Critical Section\n";
		writecount++;
		P(wrt);
		writer1count++;
			break;
		case 1 : 
			
		if(readcount<1)
		{
		if(writecount<2)
		{	
			outf << "writer3 is entering the Critical Section\n";		
		//Critical Section
		outf << "writer1 is inside the Critical Section\n";
		if(readcount>=1)
		{
			outf << "PANIC:writer1 found a Reader inside the Critical Section\n";
		}

		if(writecount>=2)
		{
			outf << "PANIC:writer1 found an another Writer inside the Critical Section\n";
		}
		//Critical Section
		writer1count++;
		}
		}
		else 
			writer1count--;
		break;
		case 2 :
		V(wrt);
		writecount--;
		outf << "writer1 has left the Critical Section\n";
		writer1count=0;
			break;
	}
}
//WRITER 2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void writer2()
{
	switch (writer2count)
	{
		case 0 :
		outf <<" writer2 wants to access the Critical Section\n";
		writecount++;
		P(wrt);
		writer2count++;
			break;
		case 1 : 
		outf << "writer3 is entering the Critical Section\n";
		if(readcount<1)
		{
		if(writecount<2)
		{

		//Critical Section
		outf << "writer2 is inside the Critical Section\n";
		if(readcount>=1)
		{
			outf << "PANIC:writer2 found a Reader inside the Critical Section\n";
		}

		if(writecount>=2)
		{
			outf << "PANIC:writer2 found an another Writer inside the Critical Section\n";
		}
		//Critical Section
		writer2count++;
		}
		}
		else 
			writer2count--;
		break;
		case 2 :
		V(wrt);
		writecount--;
		outf << "writer2 has left the Critical Section\n";
		writer2count=0;
			break;
	}
}


void P( int a)//Wait
{
	a--;
}

void V(int a)//Signal
{
	a++;
}

// had to remove all while loops because at execution it was infinitely looping and not producing anything in results.txt 


header.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef CS_H
#define CS_H

#include<fstream>



	extern std::ofstream outf;//file out
	void reader1();
	void reader2();
	void reader3();
	void reader4();
	void reader5();
	void writer1();
	void writer2();
	void writer3();
	void P(int);//wait
	void V(int);//signal


#endif 




Note: i took out reader 3-5 and writer 3 because the post was too long
Last edited on
That's really complicated.

What's a P?

Why haven't you usied arrays of readers/writers instead of a lot of seperate variables? I find the code impossible to read.
Topic archived. No new replies allowed.