- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserLandProcess.java
More file actions
Latest commit
201 lines (171 loc) · 5.56 KB
/
Copy pathUserLandProcess.java
File metadata and controls
201 lines (171 loc) · 5.56 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
importjava.util.concurrent.Semaphore;
abstractclassUserLandProcessimplementsRunnable{
publicstaticfinalintPAGE_SIZE = 1024;
publicstaticfinalintPAGE_COUNT = 1024;
publicstaticfinalintMEM_SIZE = PAGE_COUNT*PAGE_SIZE; // 1024 Pages with 1024 bytes each.
publicstaticint[][] tlb = newint[][]{{-1,-1},{-1,-1}}; // [][0 = Virtual, 1 = Physical]
publicstaticbyte[] memory = newbyte[MEM_SIZE]; // Virtual memory.
privateThreadthread; // The thread for this process
privateSemaphoresem; // The semaphore for this process
privatebooleanisExpired; // Is this process timed out?
privatebooleanisDone; // Is this process done?
/**
* Constructor.
*/
UserLandProcess(){
thread = newThread(this, ("ULP: " + this.getClass()));
sem = newSemaphore(0);
isExpired = false;
isDone = false;
thread.start();
}
/**
* Sets the boolean indicating that this process' quantum has expired.
*/
publicvoidrequestStop(){
dbMes("Stop Requested");
isExpired = true;
}
/**
* Will represent the main of our "program."
*/
publicabstractvoidmain();
/**
* Indicates if the semaphore is 0.
* @return Returns true if the semaphore is 0.
*/
publicbooleanisStopped(){
return (sem.availablePermits() == 0);
}
/**
* Friendship ended with thread.isAlive(). Keeping track of it ourseves is the real homie now.
*
* @return true when the java thread is not alive.
*/
publicbooleanisDone(){
returnisDone;
}
/**
* Releases (inclements) the semaphore, allowing this thread to run.
*/
publicvoidstart(){
sem.release();
dbMes("Start, sem: " + (sem.availablePermits()));
}
/**
* Acquires (decriments) the semaphore, stoping this thread from running.
*/
publicvoidstop(){
dbMes("Stop, sem: " + (sem.availablePermits()));
sem.acquireUninterruptibly();
}
/**
* Aquires the semaphore, then calls main.
*/
publicvoidrun(){
dbMes("Run");
sem.acquireUninterruptibly();
try{
main();
isDone = true;
System.out.println(this.getClass().getSimpleName() + " died.");
}
catch(Exceptione){
// Catch exceptions so that they don't effect other processes.
System.out.println(e.getLocalizedMessage());
isDone = true;
System.out.println(this.getClass().getSimpleName() + " failed.");
}
// I am doing this because thead.isAlive() didn't give us the right answer and This wouldn't be able to cooperate the right way.
OS.switchProcess();
}
/**
* If the task is expired, then it unexpiers it and calles OS.switchProcesss()
*/
publicvoidcooperate(){
dbMes("Cooperate, isExpired: " + isExpired);
if(isExpired == true){
isExpired = false;
OS.switchProcess();
//sem.acquireUninterruptibly();
}
}
/**
* Reads data from memory.
*
* @param address The address to be read.
* @return The memory in said address, or -1 on failure.
*/
publicbyteread(intaddress){
dbMes("Reading from " + address);
intmapping = getPhysicalAddress(address);
if (mapping != -1){
returnmemory[mapping];
}
elsereturn -1;
}
/**
* Writes data to memory.
*
* @param address The address to be writen to.
* @param value The value to be writen.
*/
publicvoidwrite(intaddress, bytevalue){
intmapping = getPhysicalAddress(address);
if (mapping != -1){
dbMes("Writing " + value + " to mapping " + mapping);
memory[mapping] = value;
}
else{
dbMes("Writing failed.");
}
}
/**
* Clears the TLB entries
*/
publicstaticvoidclearTLB(){
tlb[0][0] = -1;
tlb[0][1] = -1;
tlb[1][0] = -1;
tlb[1][1] = -1;
}
/**
* Helper Method: Gets the physical address from the procvided virtual address.
*
* @param address The desired virtal address in memory.
* @return The physical address of the specified virtal memory address.
*/
privateintgetPhysicalAddress(intaddress){
intpage = address / 1024;
intoffset = address % 1024;
// Check if TLB has the virtual memory.
if (tlb[0][0] == page){
returntlb[0][1] * PAGE_SIZE + offset;
}
elseif (tlb[1][0] == page){
returntlb[1][1] * PAGE_SIZE + offset;
}
// If the TLB doesn't have the virual memory map, get the virtual memory from OS.
OS.getMapping(page);
// Check for the memory from OS.
if (tlb[0][0] == page){
returntlb[0][1] * PAGE_SIZE + offset;
}
elseif (tlb[1][0] == page){
returntlb[1][1] * PAGE_SIZE + offset;
}
else{
dbMes("getPhysicalMapping(): Didn't find page address after calling OS.getMapping(" + page + ").");
dbMes("TLB: \n" + tlb[0][0] + " " + tlb[0][1] +"\n" + tlb[1][0] + " " + tlb[1][1]);
return -1;
}
}
/**
* DEBUGGING HELPER!!!
*
* @param message The debugging message.
*/
protectedvoiddbMes(Stringmessage){
OS.dbMes("USERLAND_PROCESS (" + this.getClass() + "): " + message);
}
}