-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.java
86 lines (56 loc) · 1.61 KB
/
CPU.java
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
import java.util.Queue;
public class CPU
{ public static boolean available;
public static int countNormally;
public static int countAbNormally;
public static OS os;
public static void main(String[] args)
{ available = true;
os = new OS();
Queue<PCB> ready = os.CPUScheduler();
// CPU Scheduler
while(ready.size() > 0)
{ PCB p = null;
available = true;
if(available)
{ available = false;
System.out.println("\t-- Dispatching Process --\n");
p = ready.poll();
p.run();
while(trackProcess(p))
if(!trackProcess(p))
{ os.ramSize += p.jobSize;
break; } }
if(os.ramSize > os.min && os.jobs.size() > 0)
{ System.out.println("\t-- Avilable memory in RAM: CPUScheduler is called -- \n");
os.CPUScheduler(); } }
// Simulation finished
// For output file
os.generateOutput(countNormally, countAbNormally); }
// This method will return false if process is terminated
public static boolean trackProcess(PCB p)
{ p.cut++;
int num = (int)(Math.random()*100);
if(p.cut > p.ecu)
{ termination(p, false);
return false; }
else
if(num <= 10)
{ termination(p, true);
return false; }
else
if( num <= 15)
{ termination(p, false);
return false; }
return true; }
public static void termination(PCB p, boolean normal)
{ p.terminate();
available = true;
os.ramSize += p.jobSize;
System.out.printf("Process Termination \nPID: %d CUT: %d Size: %d KB ", p.pid, p.cut, p.jobSize);
if(normal)
{ countNormally++;
System.out.println("Normal termination. \n"); }
else
{ countAbNormally++;
System.out.println("Abnormal termination. \n"); } } }