-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test11.cpp
115 lines (82 loc) · 1.56 KB
/
Test11.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
/*#include "kernel.h"
#include <iostream.h>
#include <stdlib.h>
#include "Semaphor.h"
#include "Thread.h"
#include "PCB.h"
// Test: Semafori sa spavanjem 3
int t=-1;
Semaphore s(0);
class TestThread : public Thread
{
private:
Time waitTime;
public:
TestThread(Time WT): Thread(), waitTime(WT){}
~TestThread()
{
waitToComplete();
}
protected:
void run();
};
void TestThread::run()
{
lock
cout << "Thread " << getId() << "waits for " << waitTime << " units of time.\n";
unlock
int r = s.wait(waitTime);
s.signal();
lock
cout << "Thread " << getId() << " finished: r = " << r << endl;
unlock
}
void tick()
{
//t++;
//if(t)
// syncPrintf("%d\n",t);
}
#include <iostream.h>
Semaphore* mutex = 0;
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 < 10000; i++)
{
if (mutex->wait(n)) {
cout << znak;
mutex->signal();
}
}
if (mutex->wait(n)) {
cout << endl << znak << " finished" << endl;
mutex->signal();
}
}
private:
char znak;
int n;
};
int userMain(int argc, char* argv[]) {
mutex = new Semaphore(1);
Znak* a = new Znak('a', 10);
Znak* b = new Znak('b', 15);
Znak* c = new Znak('c', 20);
a->start();
b->start();
c->start();
delete a;
delete b;
delete c;
if (mutex->wait(1)) {
cout << endl << "userMain finished" << endl;
mutex->signal();
}
delete mutex;
return 0;
}*/