-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test12.cpp
80 lines (66 loc) · 1.08 KB
/
Test12.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
/*#include "kernel.h"
#include <iostream.h>
#include <stdlib.h>
#include "Semaphor.h"
#include "Thread.h"
#include "PCB.h"
// Test: Semafori sa spavanjem 4
int t=-1;
const int n=15;
Semaphore s(1);
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);
if(getId()%2)
s.signal();
lock
cout << "Thread " << getId() << " finished: r = " << r << endl;
unlock
}
void tick()
{
t++;
if(t){
lock
cout << t << endl;
unlock
}
}
int userMain(int argc, char** argv)
{
lock
cout << "Test starts.\n";
unlock
TestThread* t[n];
int i;
for(i=0;i<n;i++)
{
t[i] = new TestThread(5*(i+1));
t[i]->start();
}
for(i=0;i<n;i++)
{
t[i]->waitToComplete();
}
delete t;
lock
cout << "Test ends.\n";
unlock
return 0;
}*/