-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.java
More file actions
75 lines (64 loc) · 2.29 KB
/
Scheduler.java
File metadata and controls
75 lines (64 loc) · 2.29 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
import java.util.*;
/**
* Schedules jobs with FIFO or Priority scheduling policy.
*/
public class Scheduler {
public static scheduler policy;
/**
* Load 30 jobs into an array
*/
public static final ArrayList<PCB> jobs = new ArrayList<>(30);
/**
* Load instruction set into an array
*/
public static ArrayList<CPU> instructions = new ArrayList<>();
private static final PriorityQueue<PCB> priority_queue = new PriorityQueue<>();
private static final LinkedList<PCB> fifo_queue = new LinkedList<>();
//Long-term scheduler that adds jobs to a policy
//and sets its added time to real time.
//Also makes sure each job is assigned to its assigned policy
//And adds the job to the queue accordingly.
static void addJob(PCB job) {
job.setAddedTime(System.currentTimeMillis());
jobs.add(job); //takes job from job pool/thread pool.
if (policy == scheduler.PRIORITY) {
priority_queue.add(job);
} else {
fifo_queue.add(job);
}
}
//Add a CPU to the list of CPUs
static void addCpu(CPU cpu) {
instructions.add(cpu);
}
//Checks if there is remaining job in the queue
//and retrieves the job that's in ready state at the top of the list.
static synchronized boolean hasNext() {
PCB nextJob;
if (policy == scheduler.PRIORITY) {
nextJob = priority_queue.peek();
} else {
nextJob = fifo_queue.peek();
}
return nextJob == null;
}
//Short-term schedule that retrives the job from the top of the list with hasNext() and then removes it.
//Syncronized method provides protection from a race condition or a deadlock between CPUs.
static synchronized PCB nextJob() {
PCB nextJob;
if (policy == scheduler.PRIORITY) {
nextJob = priority_queue.poll();
} else {
nextJob = fifo_queue.poll();
}
if (nextJob != null) {
nextJob.setJobState(PCB.JobState.RUNNING); //sets the status from ready to running.
}
return nextJob;
}
//keeps a hold of our two policy types.
public enum scheduler {
FIFO,
PRIORITY
}
}