My program isn't doing anything.

I wrote my program which is supposed to tell who is in the critical section and if there are 1 reader and writers exit or 2 writers exit. It's supposed to write everything to a txt file. It hasn't been working.

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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <iostream>
#include <fstream>

//declare  global variables
int read1count, read2count, read3count, write1count, write2count = 0;
int readnumber, writenumber = 0;

using namespace std;

int main () 
{
   ofstream outputFile ("Solution.txt");

    int s=1;
    int k;
    bool lock; //init to false
    bool unlock= true; //init to true
	//output answer to Project2 doc
	
	//declaration of readers and writers
	int read1, read2, read3, write1, write2 = 1; 

	//random choice of reader or writer 
	for (int i = 0; i < 5,000; i++)
	{	int coin = rand () % 5;
	
	//tells if a reader or writer was flipped
	switch (coin) 
	{
	case 0: coin = read1;
		break;
	case 1: coin= read2; 
		break;
	case 2: coin = read3; 
		break;
	case 3: coin = write1; 
		break;
	case 4: coin = write2; 
		break;
	}
	outputFile << "coin is" << coin << endl;

	}

	switch (read1count)
	{
		//Non-Critical Section
	case 0: 
		read1count++;
		break;
		//acquires lock
	case 1: while (lock= true);
         //critical section
	case 2: if (s<= 0);
	     else read1count++;
	     break;
	case 3:s--;
	case 4: readnumber++;
		break;
	case 5: read1count++;
		break;
	case 6: if (writenumber > 0) {
				//swap out if writers are in Critical Section
			    outputFile << " PANIC";
					swap(lock, unlock);
			}
			else {
				//if there are only readers in the Critical Section
				outputFile << "Only readers are in the Critical Section" << endl;
			}
		break;
	case 7: if (k >= 300)
				k++;
			else
				outputFile << "Value of k is" << k << endl;
		break;
	case 8: outputFile << "There are" << readnumber << "readers and" << writenumber << "writers in the Critical Section" << endl;
		break;
	case 9: readnumber--;
			read1count--;
		break;
		//Unlock Critical Section
	case 10: while (unlock)
	s++;
	break;
	}
switch (read2count)
	{
		//Non-Critical Section
	case 0: 
		read2count++;
		break;
		//acquires lock
	case 1: while (lock= true);
         //critical section
	case 2: if (s<= 0);
	     else read1count++;
	     break;
	case 3:s--;
	case 4: readnumber++;
		break;
	case 5: read2count++;
		break;
	case 6: if (writenumber > 0) {
				//if there are writers in the Critical Section
				outputFile << "NO WRITERS ALLOWED IN THE CRITICAL SECTION" << endl;
			//swap out if writers are in Critical Section
				swap(lock, unlock);}
			else { 
				//if there are only readers in the Critical Section
					outputFile << "Only readers are in the Critical Section" << endl; }
		break;
	case 7: if (k >= 300)
				k++;
			else
				outputFile << "Value of k is" << k << endl;
		break;
	case 8: outputFile << "There are" << readnumber << "readers and" << writenumber << "writers in the Critical Section" << endl;
		break;
	case 9: readnumber--;
			read2count--;
		break;
		//Unlock Critical Section
	case 10: while (unlock)
	s++;
	break;
}
switch (read3count)
	{
		//Non-Critical Section
	case 0: 
		read3count++;
		break;
		//acquires lock
	case 1: while (lock= true);
         //critical section
	case 2: if (s<= 0);
	     else read3count++;
	     break;
	case 3:s--;
	case 4: readnumber++;
		break;
	case 5: read3count++;
		break;
	case 6: if (writenumber > 0) {
				//if there are writers in the Critical Section
				outputFile << "NO WRITERS ALLOWED IN THE CRITICAL SECTION" << endl;
			//swap out if writers are in Critical Section
				swap(lock, unlock);  }
			else {
				//if there are only readers in the Critical Section
				outputFile << "Only readers are in the Critical Section" << endl;}
		break;
	case 7: if (k >= 300)
				k++;
			else
				outputFile << "Value of k is" << k << endl;
		break;
	case 8: outputFile << "There are" << readnumber << "readers and" << writenumber << "writers in the Critical Section" << endl;
		break;
	case 9: readnumber--;
			read3count--;
		break;
		//Unlock Critical Section
	case 10: while (unlock)
	s++;
	break;
}
switch (write1count)
	{
		//Non-Critical Section
	case 0: 
		write1count++;
		break;
		//acquires lock
	case 1: while (lock= true);
         //critical section
	case 2: if (s<= 0);
	     else write1count++;
	     break;
	case 3:s--;
	case 4: writenumber++;
		break;
	case 5: write1count++;
		break;
	case 6: if (writenumber > 1) {
				//if there are writers in the Critical Section
				outputFile << "ONLY 1 WRITER ALLOWED IN THE CRITICAL SECTION" << endl;
			//swap out if writers are in Critical Section
				swap(lock, unlock);}
			else {
				//if there are only readers in the Critical Section
				outputFile << "Only 1 writer is in the Critical Section" << endl; }
		break;
	case 7: if (k >= 300)
				k++;
			else
				outputFile << "Value of k is" << k << endl;
		break;
	case 8: outputFile << "There are" << readnumber << "readers and" << writenumber << "writers in the Critical Section" << endl;
		break;
	case 9: writenumber--;
			write1count--;
		break;
		//Unlock Critical Section
	case 10: while (unlock =true)
	s++;
	break;
}
switch (read3count)
	{
		//Non-Critical Section
	case 0: 
		read3count++;
		break;
		//acquires lock
	case 1: while (lock= true);
         //critical section
	case 2: if (s<= 0);
	     else read3count++;
	     break;
	case 3:s--;
	case 4: readnumber++;
		break;
	case 5: read3count++;
		break;
	case 6: if (writenumber > 1) {
				//if there are writers in the Critical Section
				outputFile << "ONLY 1 WRITER ALLOWED IN THE CRITICAL SECTION" << endl;
				//swap out if writers are in Critical Section 
				swap(lock, unlock); }
					else 
				 if (readnumber > 0) {
				//if there are writers in the Critical Section
				outputFile << "NO READER ALLOWED IN THE CRITICAL SECTION" << endl;
				//swap out if writers are in Critical Section
				 swap(lock, unlock); 
					}
					else {
				//if there are only 1 writer in the Critical Section
				outputFile << "Only 1 writer is in the Critical Section" << endl; }
		break;
	case 7: if (k >= 300)
				k++;
			else
				outputFile << "Value of k is" << k << endl;
		break;
	case 8: outputFile << "There are" << readnumber << "readers and" << writenumber << "writers in the Critical Section" << endl;
		break;
	case 9: readnumber--;
			read3count--;
		break;
		//Unlock Critical Section
	case 10: while (unlock =true)
	s++;
	break;
}
switch (write2count)
	{
		//Non-Critical Section
	case 0: 
		write2count++;
		break;
		//acquires lock
	case 1: while (lock= true);
         //critical section
	case 2: if (s<= 0);
	     else write2count++;
	     break;
	case 3:s--;
	case 4: writenumber++;
		break;
	case 5: write2count++;
		break;
	case 6: if (writenumber > 1) {
				//if there are writers in the Critical Section
				outputFile << "ONLY 1 WRITER ALLOWED IN THE CRITICAL SECTION" << endl;
				//swap out if writers are in Critical Section 
				swap(lock, unlock); }
					else 
				 if (readnumber > 0) {
				//if there are writers in the Critical Section
				outputFile << "NO READER ALLOWED IN THE CRITICAL SECTION" << endl;
				//swap out if writers are in Critical Section
				 swap(lock, unlock); 
					}
					else {
				//if there are only 1 writer in the Critical Section
				outputFile << "Only 1 writer is in the Critical Section" << endl; }
					break;
	case 7: if (k >= 300)
				k++;
			else
				outputFile << "Value of k is" << k << endl;
		break;
	case 8: outputFile << "There are" << readnumber << "readers and" << writenumber << "writers in the Critical Section" << endl;
		break;
	case 9: writenumber--;
			write2count--;
		break;
		//Unlock Critical Section
	case 10: while (unlock =true)
	s++;
	break;
}
	outputFile.close();
	cout << "DONE\n";
	return 0;
}


As of yesterday a file was created and that's about it.
I didn't really look over the whole program, but I did notice this in several places:


while (lock= true);

This is a deadlock. Did you mean ==?

Also if you are using critical sections, then why are you spinning in a loop in the first place? When you lock a critical section it will block until the section becomes available... so there's no need for you to manually block (and in fact, you shouldn't).
Hello, u forgot to open the file =P,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>

//declare  global variables
int read1count, read2count, read3count, write1count, write2count = 0;
int readnumber, writenumber = 0;

using namespace std;

int main () 
{
   ofstream outputFile;
   outputFile.open("Solution.txt");
   /*i have had some problems with that as well, and i had to add the fstream::out. 
So that will be outputFile.open("Solution.txt", ios::out);*/
Last edited on
I realized that I didn't open it so I changed line 12-13 to

ofstream outputFile ("Solution.txt");

This is for a class and he wants us to block check what's in the CS and do different outputs to a file

-There can be multiple readers in the CS but writers can only be in the CS alone so I have to swap out of the CS if that happens.
-Also print panic messages if the rules aren't followed.

while (lock == true);

Thanks in advance. I'm actually studying deadlocks in my class now. How funny ... the one think I was avoiding.
Last edited on
Okay, since this is obviously homework, let me just ask some questions.

You specify that write2 = 1, but what about the other variables in that line?

When are the read1count, write1count, etc., variables ever set to anything? Never. So what are the switch statements for? (Well, write2count is but still...) Even had you managed to get them all set to 0, since all the switch statements are outside of any loop (main is only executed once...) all you would manage to do is increment all these values once, then exit the program.

What exactly are you trying to accomplish with your for loop? Currently all it does is set coin to a number between 0 and 4, then sets coin to either 1 or garbage, repeats 5000 times, then throws coin away.

While and if are not interchangeable. While should be used like this:
1
2
3
while(i >= 20){
   i++;
}


or like this:
1
2
3
4
5
6
do{
   if(i <20)
      i++;
   else
      break;
}while(true);


So, as programmed, it wouldn't ever output anything.

Oh, and take that comma out of the 5000. It compiles, but doesn't do what you expect.
Last edited on
I am a little confused by this project and he suggested we use the switch statement. I rarely use the switch statement at all.

Pretty much I've been trying to make critical section that holds multiple readers or one writer at a time. In the critical section if there it panics and swaps out of the critical section.

The coin what is used to select a random reader or writer. to implement.

I don't understand why he wants us to have the panic and swap because in this program only one item can be in the critical section anyway.
So I've been working on fixing some of the stuff. I got it output once I took out that loop my professor wanted. I'm think I'm going to go with what I know and do a different type of loop because this switch statement is kinda messy and confusing me. Hopefully I'll have it working by tomorrow because it's overdue. But better off with points off rather than no points at all.
Topic archived. No new replies allowed.