-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schedule.java
140 lines (115 loc) · 3.58 KB
/
Schedule.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
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
import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Schedule {
//private LinkedList <Interval> chart;
private int count;
private ArrayList<Process>readyQueue,processQueue;
public Schedule() {
readyQueue = new ArrayList<Process>();
processQueue = new ArrayList<Process>();
count = 0;
}
public void getinfo(int type)
{
Process temp;
System.out.println("Enter number of processes:");
Scanner sc = new Scanner(System.in);
count = sc.nextInt();
for(int i = 0;i<count;i++){
temp = new Process();
temp.input(type);
readyQueue.add(temp);
}
processQueue = readyQueue;
}
public void priorityScheduling() // input: process id, burst time, priority
{
// sort Processes based on priorities
Collections.sort(processQueue);
ArrayList<Integer> waitingTime = new ArrayList<Integer>();
// Printing sorted processes
/*
* for (Iterator iterator = processQueue.iterator(); iterator.hasNext();) {
Process process = (Process) iterator.next();
process.showInfo();
}
*/
int timer = 0;
for (Iterator iterator = processQueue.iterator(); iterator.hasNext();)
{
Process process = (Process) iterator.next();
process.startTime = timer;
process.endTime = timer + process.bt;
timer = process.endTime;
}
System.out.println("\n Gantt Chart :\n");
for (Iterator iterator = processQueue.iterator(); iterator.hasNext();) {
Process process = (Process) iterator.next();
process.showInfo();
waitingTime.add(process.getStartTime());
System.out.println("\nWaiting time:"+process.getStartTime()+"\nTurn around time:"+(process.getStartTime()+process.bt));
}
//Finding avg waiting time
int sum=0;
for (Iterator iterator = waitingTime.iterator(); iterator.hasNext();) {
sum = sum + (Integer) iterator.next();
}
System.out.println("Average Waiting time = "+(sum/count));
}
public void fcfs() // input: process id,burst time,arrival time
{
ArrayList<Integer> waitingTime = new ArrayList<Integer>();
int timer = 0;
for (Iterator iterator = processQueue.iterator(); iterator.hasNext();)
{
Process process = (Process) iterator.next();
process.startTime = timer;
process.endTime = timer + process.bt;
timer = process.endTime;
}
System.out.println("\n Gantt Chart :\n");
for (Iterator iterator = processQueue.iterator(); iterator.hasNext();) {
Process process = (Process) iterator.next();
process.showInfo();
waitingTime.add(process.getStartTime()-process.at);
System.out.println("\nWaiting time:"+(process.getStartTime()-process.at)+"\nTurn around time:"+(process.getStartTime()-process.at+process.bt));
}
//Finding avg waiting time
int sum=0;
for (Iterator iterator = waitingTime.iterator(); iterator.hasNext();) {
sum = sum + (Integer) iterator.next();
}
System.out.println("\nAverage Waiting time = "+(sum/count));
}
public void roundRobin()
{
int timeSlice = 0; // TimeSlice or TimeQuantum
Scanner sc = new Scanner(System.in);
timeSlice = sc.nextInt();
sc.close();
Collections.sort(processQueue,Process.arrivalTimeComparator);
}
public static void main(String[] args) {
/*
* For Priority Scheduling
Schedule schedule = new Schedule();
schedule.getinfo(0);
schedule.priorityScheduling();
*/
/*
// For FCFS
Schedule schedule = new Schedule();
schedule.getinfo(1);
schedule.fcfs();
*/
// For Round Robin
Schedule schedule = new Schedule();
schedule.getinfo(1);
schedule.roundRobin();
}
}