-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test13.cpp
158 lines (125 loc) · 2.67 KB
/
Test13.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
/*#include <iostream.h>
#include <stdlib.h>
#include "Semaphor.h"
#include "Thread.h"
#include "PCB.h"
#include "Event.h"
#include "KrnlEv.h"
#include "IVTEntry.h"
#include "Kernel.h"
// Test: dogadjaji
#include <iostream.h>
#include <stdlib.h>
PREPAREENTRY(9,1);
Semaphore* mutex = 0;
Semaphore* sleepSem = 0;
void tick() {}
class Znak : public Thread
{
public:
Znak(char znak, int n) : Thread(), znak(znak), n(n) {}
virtual ~Znak() { waitToComplete(); }
void run()
{
// for (long i = 0; i < 100000; i++)
for (int i = 0; i < n; i++)
{
if (mutex->wait(1)) {
lock
//cout << "mutex val: " << mutex->val() << endl;
cout << znak;
unlock
//cout << "X";
mutex->signal();
lock
//cout << "mutex val: " << mutex->val() << endl;
unlock
}
// for (int j = 0; j < 10000; j++)
// for (int k = 0; k < 10000; k++);
Time sleepTime = 2 + rand() % 8;
lock
//cout << "sleepTime: " << sleepTime << endl;
//cout << "sleepSem val: " << sleepSem->val() << endl;
unlock
sleepSem->wait(sleepTime);
lock
//cout << "sleepSem val: " << sleepSem->val() << endl;
unlock
// dispatch();
}
lock
//cout << "mutex val: " << mutex->val() << endl;
unlock
if (mutex->wait(1)) {
lock
cout << endl << znak << " finished" << endl;
unlock
mutex->signal();
lock
//cout << "mutex val: " << mutex->val() << endl;
unlock
}
}
private:
char znak;
int n;
};
class Key : public Thread {
public:
Key(int n) : Thread(), n(n) { }
virtual ~Key() { waitToComplete(); }
void run() {
Event e(9);
for (int i = 0; i < n; i++) {
if (mutex->wait(1)) {
lock
cout << endl << "key waiting " << (i + 1) << endl;
unlock
mutex->signal();
}
e.wait();
if (mutex->wait(1)) {
lock
cout << endl << "key continue " << (i + 1) << endl;
unlock
mutex->signal();
}
sleepSem->wait(5);
}
if (mutex->wait(1)) {
lock
cout << endl << "key finished" << endl;
unlock
mutex->signal();
}
}
private:
int n;
};
int userMain(int argc, char* argv[]) {
mutex = new Semaphore(1);
sleepSem = new Semaphore(0);
Znak* a = new Znak('a', 10);
Znak* b = new Znak('b', 15);
Znak* c = new Znak('c', 20);
Key* k = new Key(150);
a->start();
b->start();
c->start();
k->start();
delete a;
//cout << "Deleted a" << endl;
delete b;
delete c;
delete k;
if (mutex->wait(1)) {
lock
cout << endl << "userMain finished" << endl;
unlock
mutex->signal();
}
delete sleepSem;
delete mutex;
return 0;
}*/