-
Notifications
You must be signed in to change notification settings - Fork 2
/
readerwriter.cpp
141 lines (114 loc) · 3.14 KB
/
readerwriter.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
// readerwriter.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <process.h>
#include <windows.h>
#include <iostream>
#include <time.h>
#define MAX_RAND_INT 100
#define MIN_RAND_INT 0
#define WRITERS_NUMBER 2
#define READERS_NUMBER 2
using namespace std;
unsigned long __stdcall Reader(void*);
unsigned long __stdcall Writer(void*);
int getRandom();
void sleep(int);
CRITICAL_SECTION writersBlocker;
CRITICAL_SECTION readersBlocker;
CRITICAL_SECTION allReadersBlocker;
CRITICAL_SECTION readersCounterBlocker;
CRITICAL_SECTION writersCounterBlocker;
int readersAmount;
int writersAmount;
HANDLE writers[WRITERS_NUMBER];
HANDLE readers[READERS_NUMBER];
DWORD threadID;
int _tmain(int argc, _TCHAR* argv[])
{
readersAmount = 0;
writersAmount = 0;
InitializeCriticalSection(&writersBlocker);
InitializeCriticalSection(&readersBlocker);
InitializeCriticalSection(&allReadersBlocker);
InitializeCriticalSection(&readersCounterBlocker);
InitializeCriticalSection(&writersCounterBlocker);
for(int i = 0; i < WRITERS_NUMBER; ++i)
{
writers[i] = CreateThread(NULL, 0, Writer, (void*)i, 0, &threadID);
}
for(int i = 0; i < READERS_NUMBER; ++i)
{
readers[i] = CreateThread(NULL, 0, Reader, (void*)i, 0, &threadID);
}
system("pause");
return 0;
}
unsigned long __stdcall Reader(void* num)
{
int number = (int)num;
printf("Started Reader %d\n", number);
while(true)
{
EnterCriticalSection(&allReadersBlocker);
EnterCriticalSection(&readersBlocker);
EnterCriticalSection(&readersCounterBlocker);
readersAmount++;
if(readersAmount == 1)
{
EnterCriticalSection(&writersBlocker);
}
LeaveCriticalSection(&readersCounterBlocker);
LeaveCriticalSection(&readersBlocker);
LeaveCriticalSection(&allReadersBlocker);
printf("Reader %d started to read\n", number);
sleep(3);
printf("Reader %d read\n", number);
EnterCriticalSection(&readersCounterBlocker);
readersAmount--;
if(readersAmount == 0)
{
LeaveCriticalSection(&writersBlocker);
}
LeaveCriticalSection(&readersCounterBlocker);
sleep(3);
}
return 0;
}
unsigned long __stdcall Writer(void* num)
{
int number = (int)num;
printf(" Started Writer %d\n", number);
while(true)
{
EnterCriticalSection(&writersCounterBlocker);
writersAmount++;
if(writersAmount == 1)
{
EnterCriticalSection(&readersBlocker);
}
LeaveCriticalSection(&writersCounterBlocker);
EnterCriticalSection(&writersBlocker);
printf(" Writer %d started to write\n", number);
sleep(5);
printf(" Writer %d wrote\n", number);
LeaveCriticalSection(&writersBlocker);
EnterCriticalSection(&writersCounterBlocker);
writersAmount--;
if(writersAmount == 0)
{
LeaveCriticalSection(&readersBlocker);
}
LeaveCriticalSection(&writersCounterBlocker);
sleep(10);
}
return 0;
}
int getRandom()
{
return rand() % (MAX_RAND_INT - MIN_RAND_INT) + MIN_RAND_INT;
}
void sleep(int maxSleepSeconds)
{
Sleep((getRandom() % maxSleepSeconds) * 1000);
}