-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUSER.cpp
executable file
·146 lines (100 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
//File: user.cpp
//Author: Milos Acimovic
//===========================================================================//
// Project: Operating Systems 1 project
// File: user.cpp
// Date: May 2015
//===========================================================================//
#include <iostream.h>
#include <stdlib.h>
#include "thread.h"
#include "intLock.h"
//---------------------------------------------------------------------------//
// Ovo se menja u testu
//---------------------------------------------------------------------------//
Time time_slice = 2; // 0 ili defaultTimeSlice
int N = 3; // 1 <= N <= 19
//---------------------------------------------------------------------------//
class A : public Thread {
public:
A(Time time_slice): Thread(defaultStackSize, time_slice) {}
A():Thread(defaultStackSize, 40){}
virtual ~A() {waitToComplete(); }
protected:
virtual void run();
};
//---------------------------------------------------------------------------//
class B : public Thread {
public:
B () : Thread(defaultStackSize, 20) {}
virtual ~B() {waitToComplete(); }
protected:
virtual void run();
};
//---------------------------------------------------------------------------//
// Korisnicki program mora obavezno da definise ovu f-ju
//---------------------------------------------------------------------------//
void tick(){}
//---------------------------------------------------------------------------//
void A::run(){
for(int i = 0; i < 30; i++){
intLock
cout << "u A::run() counter number:" << i<< " id no. = "<<getId() << '\n';
intUnlock
for (int k = 0; k<10000; ++k)
for (int j = 0; j <30000; ++j);
}
intLock
cout << "A marked terminating\n";
intUnlock
}
//---------------------------------------------------------------------------//
void B::run(){
for(int i = 0; i < 30; i++){
intLock
cout << "u B::run() counter number:" << i << " id no. = "<<getId() <<'\n';
intUnlock
for (int k = 0; k<10000; ++k)
for (int j = 0; j <30000; ++j);
}
intLock
cout<<"B marked terminating\n";
intUnlock
}
int userMain (int argc, char* argv[]){
intLock
if(argc <2){
cout<<"Invalid input!"<<endl;
intUnlock
return -1;
}
N = atoi(argv[1]);
N = N>19 ? 19 : N;
time_slice = atoi(argv[2]);
A **athreads = new A*[N];
B* threadb = new B();
int i;
for (i=0; i<N; i++){
athreads[i] = new A(time_slice);
athreads[i]->start();
}
threadb->start();
intUnlock
for(i = 0; i < 30;i++){
intLock
cout << "userMain with id: " << Thread::getThreadById(2)->getId() << "counter = " << i<< '\n';
intUnlock
for(int j = 0; j < 3000; j++)
for(int z = 0; z < 3000; z++);
}
for (i=0; i<N; i++){
delete athreads[i];
}
delete [] athreads;
delete threadb;
intLock
cout<<"userMain finished! and its id is: " <<Thread::getRunningId()<<'\n';
intUnlock
return 0;
}
//---------------------------------------------------------------------------//