-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.cpp
184 lines (156 loc) · 3.07 KB
/
user.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
#include <iostream.h>
#include <stdlib.h>
#include <assert.h>
#include "bounded.h"
#include "intLock.h"
#include "keyevent.h"
#include "user.h"
#include "event.h"
#include "semaphor.h"
Time TIME_SLICE = 2;
int N = 3;
volatile int theEnd = 0;
class Producer: public Thread {
public:
Producer(BoundedBuffer* bb, char y, Time time_slice);
virtual ~Producer() {
waitToComplete();
}
Thread* clone() const {
return new Producer(myBuffer, x, time_slice_clone);
}
protected:
virtual void run();
char produce() {
return x;
}
private:
Time time_slice_clone;
BoundedBuffer* myBuffer;
char x;
Semaphore sleep;
};
class Consumer: public Thread {
public:
Consumer(BoundedBuffer* bb) :
Thread(defaultStackSize, 0), myBuffer(bb), sleep(0) {
}
virtual ~Consumer() {
waitToComplete();
}
Thread* clone() const {
return new Consumer(myBuffer);
}
protected:
virtual void run();
void consume(char p);
private:
BoundedBuffer* myBuffer;
Semaphore sleep;
};
void tick() {
}
Producer::Producer(BoundedBuffer* bb, char y, Time time_slice) :
Thread(defaultStackSize, time_slice), myBuffer(bb), x(y), sleep(0), time_slice_clone(
time_slice) {
}
void Producer::run() {
while (!theEnd) {
char d = produce();
myBuffer->append(d);
assert(1 != sleep.wait(10) && "wait(10) returned 1!");
}
}
void Consumer::consume(char p) {
intLock
cout << p << " ";
intUnlock
}
void Consumer::run() {
int i = 0;
while (!theEnd) {
char d = myBuffer->take();
consume(d);
if (i++ == 40) {
assert(1 != sleep.wait(5) && "wait(10) returned 1!");
i = 0;
} else
for (int j = 0; j < 200; j++)
;
}
intLock
cout << endl << "ESC pressed - empty the buffer!" << endl;
intUnlock
while (myBuffer->fullCount()) {
char d = myBuffer->take();
consume(d);
dispatch();
}
intLock
cout << endl << "Happy End" << endl;
intUnlock
}
#ifdef SIGNAL
Semaphore signalTest(0);
void signal1() {
signalTest.signal();
}
#endif
int userMain(int argc, char* argv[]) {
BoundedBuffer *buff;
Consumer *con;
intLock
if (argc < 2) {
cout << "Invalid input!" << endl;
intUnlock
return -1;
}
int buffSize = atoi(argv[1]);
N = atoi(argv[2]);
N = N > 19 ? 19 : N;
TIME_SLICE = atoi(argv[3]);
if (buffSize < N) {
cout << "Number of Produsers is larger then Buffer size!" << endl;
intUnlock
return 1;
}
#ifdef SIGNAL
Thread *current = Thread::getThreadById(Thread::getRunningId());
current->registerHandler(1, signal1);
current->maskSignal(1);
#endif
buff = new BoundedBuffer(buffSize);
Producer **pro = new Producer*[N];
KeyboardEvent* kev;
int i;
con = new Consumer(buff);
con->start();
for (i = 0; i < N; i++) {
pro[i] = new Producer(buff, '0' + i, TIME_SLICE);
pro[i]->start();
}
kev = new KeyboardEvent(buff);
intUnlock
kev->start();
#ifdef SIGNAL
current->unmaskSignal(1);
Semaphore timer(0);
timer.wait(10);
if (N > 1) {
pro[0]->signal(0);
Thread::pause();
signalTest.wait(0);
}
#endif
for (i = 0; i < N; i++) {
delete pro[i];
}
delete[] pro;
delete kev;
delete con;
delete buff;
intLock
cout << "userMain finished!" << endl;
intUnlock
return 0;
}