/
BasicProcess.cs
428 lines (392 loc) · 10.8 KB
/
BasicProcess.cs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
namespace Frapes
{
/// <summary>
/// This class is used to represent a process in the system.
/// </summary>
public class BasicProcess
{
private int _state = Defines.NotReady;
private int _processid = 0;
private int _readytime = 0;
private int _deadlinetime = 0;
private int _executiontime = 0;
private int _priority = 0;
private int _sort = 0;
private int _blockingtime = 0;
private int _blockinglength = 0;
/// <value>
/// Indicates the process current state. It should be set
/// to a constant declare on the Defines class.
/// </value>
public int State
{
get { return _state; }
set { _state = value; }
}
/// <value>
/// The Process Identifier, unique to each process on the system.
/// </value>
public int ProcessId
{
get { return _processid; }
set { _processid = value; }
}
/// <value>
/// The amount of system time units required for the process to become ready
/// for execution.
/// </value>
public int ReadyTime
{
get { return _readytime; }
set { _readytime = value; }
}
/// <value>
/// The maximum amount of system time units for the process to complete execution.
/// </value>
public int DeadlineTime
{
get { return _deadlinetime; }
set { _deadlinetime = value; }
}
/// <value>
/// The total time units passed since the process started execution.
/// </value>
public int ExecutionTime
{
get { return _executiontime; }
set { _executiontime = value; }
}
//// <value>
/// Represents the process priority
/// </value>
public int Priority
{
get { return _priority; }
set { _priority = value; }
}
//// <value>
/// Order of the process
/// </value>
public int Sort
{
get { return _sort; }
set { _sort = value; }
}
//// <value>
/// Time units when the process will be blocked.
/// </value>
public int BlockingTime
{
get { return _blockingtime; }
set { _blockingtime = value; }
}
//// <value>
/// How long the blocking will take.
/// </value>
public int BlockingLength
{
get { return _blockinglength; }
set { _blockinglength = value; }
}
/// <summary>
/// Array of strings with the Process properties labels.
/// </summary>
public static string[] ProcessLabels = {"Process Id", "Ready Time", "Exec Time", "Deadline Time", "Priority", "Blocking Time", "Blocking Length"};
/// <summary>
/// Default constructor method for the BasicProcess class.
/// </summary>
public BasicProcess ()
{
}
/// <summary>
/// Constructor that sets default values.
/// </summary>
/// <param name="procId">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="readyTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="execTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="deadlineTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="priority">
/// A <see cref="System.Int32"/>
/// </param>
public BasicProcess (int procId, int readyTime, int execTime, int deadlineTime, int priority)
{
this.ProcessId = procId;
this.ReadyTime = readyTime;
this.DeadlineTime = deadlineTime;
this.ExecutionTime = execTime;
this.Priority = priority;
// process may be already ready
if (this.ReadyTime <= 0)
this.State = Defines.Ready;
// process may have finished
if (this.ExecutionTime <= 0)
this.State = Defines.Finished;
}
/// <summary>
/// Constructor that sets default values for processes with a Blocking time and length.
/// </summary>
/// <param name="procId">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="readyTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="execTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="deadlineTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="priority">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="blockingTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="blockingLength">
/// A <see cref="System.Int32"/>
/// </param>
public BasicProcess (int procId, int readyTime, int execTime, int deadlineTime, int priority, int blockingTime, int blockingLength)
{
this.ProcessId = procId;
this.ReadyTime = readyTime;
this.DeadlineTime = deadlineTime;
this.ExecutionTime = execTime;
this.Priority = priority;
this.BlockingTime = blockingTime;
this.BlockingLength = blockingLength;
// Process may be running
if (this.ReadyTime <= 0)
{
this.State = Defines.Ready;
// Process may be blocked already (very unlikely, but...)
if ((this.BlockingTime == 0) && (this.BlockingLength > 0))
{
this.State = Defines.Blocked;
}
}
// Process may have finished
if (this.ExecutionTime <= 0)
{
this.State = Defines.Finished;
}
}
/// <summary>
/// Constructor that sets default values for processes with a Blocking time and length, and a sort value.
/// </summary>
/// <param name="procId">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="readyTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="execTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="deadlineTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="priority">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="blockingTime">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="blockingLength">
/// A <see cref="System.Int32"/>
/// </param>
/// <param name="sort">
/// A <see cref="System.Int32"/>
/// </param>
public BasicProcess (int procId, int readyTime, int execTime, int deadlineTime, int priority, int blockingTime, int blockingLength, int sort)
{
this.ProcessId = procId;
this.ReadyTime = readyTime;
this.DeadlineTime = deadlineTime;
this.ExecutionTime = execTime;
this.Priority = priority;
this.BlockingTime = blockingTime;
this.BlockingLength = blockingLength;
this.Sort = sort;
// Process may be running
if (this.ReadyTime <= 0)
{
this.State = Defines.Ready;
// Process may be already blocked...unusual, but can happen
if ((this.BlockingTime == 0) && (this.BlockingLength > 0))
{
this.State = Defines.Blocked;
}
}
// process may be finished
if (this.ExecutionTime <= 0)
{
this.State = Defines.Finished;
}
}
/// <summary>
/// Constructor used to create a copy of an existing process
/// </summary>
/// <param name="process">
/// A <see cref="BasicProcess"/>
/// </param>
public BasicProcess (BasicProcess process)
{
this.ProcessId = process.ProcessId;
this.ReadyTime = process.ReadyTime;
this.DeadlineTime = process.DeadlineTime;
this.ExecutionTime = process.ExecutionTime;
this.State = process.State;
this.Priority = process.Priority;
this.BlockingTime = process.BlockingTime;
this.BlockingLength = process.BlockingLength;
}
/// <summary>
/// Makes the process ready.
/// </summary>
public void SetReady ()
{
this.State = Defines.Ready;
}
/// <summary>
/// It is called if the process is the actual running one.
/// Only effective if State is Ready
/// </summary>
/// <returns>
/// A <see cref="System.Boolean"/>
/// </returns>
public bool Run ()
{
bool result = true;
if ((this.State == Defines.Ready) || (this.State == Defines.Running))
{
this.ExecutionTime--;
this.ReadyTime--;
this.DeadlineTime--;
this.BlockingTime--;
this.State = Defines.Running;
if ((this.BlockingTime <= 0) && (this.BlockingLength > 0))
{
this.State = Defines.Blocked;
this.BlockingLength++;
}
if (this.ExecutionTime <= 0)
{
this.State = Defines.Finished;
result = false;
}
else
{
result = true;
}
}
return result;
}
/// <summary>
/// It is called if BasicProcess is called after every time unit.
/// If State is Running, it sets State to Ready. Returns false if Deadline is missed and does nothing except that.
/// </summary>
/// <returns>
/// A <see cref="System.Boolean"/>
/// </returns>
public bool UpdateStatistics ()
{
bool result = true;
if (this.State == Defines.Finished)
{
// finished, do nothing
}
else if (this.State == Defines.NotReady)
{
this.DeadlineTime--;
this.ReadyTime--;
if (this.ReadyTime <= 0)
{
this.State = Defines.Ready;
}
}
else if (this.State == Defines.Ready)
{
this.ReadyTime--;
this.DeadlineTime--;
if (this.DeadlineTime < 0)
{
result = false;
}
}
else if (this.State == Defines.Running)
{
// Pocess running, do nothing
}
else if (this.State == Defines.Blocked)
{
this.DeadlineTime--;
this.ReadyTime--;
this.BlockingLength--;
if (this.BlockingLength <= 0)
{
this.State = Defines.Ready;
if (this.ExecutionTime == 0)
{
this.State = Defines.Finished;
}
}
}
return result;
}
/// <summary>
/// Dump the necessary information according to the process State
/// </summary>
/// <returns>
/// A <see cref="System.String"/>
/// </returns>
public string DumpStatistics ()
{
string result = "Process: " + Convert.ToString (this.ProcessId) + " ";
if (this.State == Defines.NotReady)
{
result = result + "Gets ready in " + Convert.ToString (this.ReadyTime) + " timeunits. " + "Deadline Time: " + Convert.ToString (this.DeadlineTime);
}
else if (this.State == Defines.Ready)
{
result = result + "Ready: Exec. Time: " + Convert.ToString (this.ExecutionTime) + " Deadline Time: " + Convert.ToString (this.DeadlineTime);
}
else if (this.State == Defines.Finished)
{
result = result + "Finished: Answer Time = " + Convert.ToString (-1 * this.ReadyTime);
}
else if (this.State == Defines.Blocked)
{
result = result + "Blocked, " + Convert.ToString (this.BlockingLength) + " timeunits to go. " + " Deadline Time: " + Convert.ToString (this.DeadlineTime);
}
else
{
result = result + "Running: Exec. Time: " + Convert.ToString (this.ExecutionTime) + " Deadline Time: " + Convert.ToString (this.DeadlineTime);
}
if ((this.DeadlineTime <= 0) && (this.ExecutionTime != 0))
{
result = result + " DEADLINE ACHIEVED";
}
return result;
}
/// <summary>
/// Returns a string with the BasicProcess information
/// </summary>
/// <returns>
/// A <see cref="System.String"/>
/// </returns>
public override string ToString ()
{
return string.Format ("[BasicProcess: State={0}, ProcessId={1}, ReadyTime={2}, DeadlineTime={3}, ExecutionTime={4}, Priority={5}, Sort={6}, BlockingTime={7}, BlockingLength={8}]", State, ProcessId, ReadyTime, DeadlineTime, ExecutionTime, Priority, Sort, BlockingTime, BlockingLength);
}
}
}