1- import java .util .*;
2-
3- class SchedulingAlgorithms
4- {
5-
6- static class Process
7- {
8- int pid ;
9- int priority ;
10- int arrivalTime ;
11- int burstTime ;
12- int remainingTime ;
13- int completionTime ;
14- int waitingTime ;
15- int turnaroundTime ;
16- int responseTime ;
17- boolean completed ;
18-
19- Process (int pid , int priority , int arrivalTime , int burstTime )
20- {
1+ import java .util .Scanner ;
2+ import java .util .Arrays ;
3+ import java .util .Comparator ;
4+
5+ class SchedulingAlgorithms {
6+ static class Process {
7+ private int pid ;
8+ private int priority ;
9+ private int arrivalTime ;
10+ private int burstTime ;
11+ private int remainingTime ;
12+ private int completionTime ;
13+ private int waitingTime ;
14+ private int turnaroundTime ;
15+ private int responseTime ;
16+
17+ Process (int pid , int priority , int arrivalTime , int burstTime ) {
2118 this .pid = pid ;
2219 this .priority = priority ;
2320 this .arrivalTime = arrivalTime ;
2421 this .burstTime = burstTime ;
2522 this .remainingTime = burstTime ;
26- this .completed = false ;
23+ this .responseTime = - 1 ;
2724 }
25+ public int getPid () { return pid ; }
26+ public int getPriority () { return priority ; }
27+ public int getArrivalTime () { return arrivalTime ; }
28+ public int getBurstTime () { return burstTime ; }
29+ public int getRemainingTime () { return remainingTime ; }
30+ public int getCompletionTime () { return completionTime ; }
31+ public int getWaitingTime () { return waitingTime ; }
32+ public int getTurnaroundTime () { return turnaroundTime ; }
33+ public int getResponseTime () { return responseTime ; }
34+
35+ public void setRemainingTime (int remainingTime ) { this .remainingTime = remainingTime ; }
36+ public void setCompletionTime (int completionTime ) { this .completionTime = completionTime ; }
37+ public void setWaitingTime (int waitingTime ) { this .waitingTime = waitingTime ; }
38+ public void setTurnaroundTime (int turnaroundTime ) { this .turnaroundTime = turnaroundTime ; }
39+ public void setResponseTime (int responseTime ) { this .responseTime = responseTime ; }
2840 }
2941
30- public static void main (String [] args )
31- {
32- Scanner sc = new Scanner (System .in );
33-
34- System .out .print ("Enter number of processes: " );
35- int n = sc .nextInt ();
36-
37- Process [] processes = new Process [n ];
38- for (int i = 0 ; i < n ; i ++)
39- {
40- System .out .println ("Process " + (i + 1 ));
41- System .out .print ("Enter arrival time: " );
42- int at = sc .nextInt ();
43- System .out .print ("Enter burst time: " );
44- int bt = sc .nextInt ();
45- System .out .print ("Enter priority (integer): " );
46- int priority = sc .nextInt ();
47- processes [i ] = new Process (i + 1 , priority , at , bt );
48- }
42+ public static void main (String [] args ) {
43+ try (Scanner sc = new Scanner (System .in )) {
44+ System .out .print ("Enter number of processes: " );
45+ int n = sc .nextInt ();
46+
47+ Process [] processes = new Process [n ];
48+ for (int i = 0 ; i < n ; i ++) {
49+ System .out .println ("Process " + (i + 1 ));
50+ System .out .print ("Enter arrival time: " );
51+ int at = sc .nextInt ();
52+ System .out .print ("Enter burst time: " );
53+ int bt = sc .nextInt ();
54+ System .out .print ("Enter priority (integer): " );
55+ int priority = sc .nextInt ();
56+ processes [i ] = new Process (i + 1 , priority , at , bt );
57+ }
4958
50- System .out .println ("Choose the scheduling algorithm:" );
51- System .out .println ("1. Non-Preemptive" );
52- System .out .println ("2. Preemptive" );
53- System .out .print ("Enter your choice (1 or 2): " );
54- int choice = sc .nextInt ();
55-
56- switch (choice )
57- {
58- case 1 :
59- nonPreemptive ( processes ) ;
60- break ;
61- case 2 :
62- preemptive ( processes ) ;
63- break ;
64- default :
65- System . out . println ( "Invalid choice." ) ;
66- return ;
59+ System .out .println ("\n Choose the scheduling algorithm:" );
60+ System .out .println ("1. Non-Preemptive (Shortest Job First with Priority) " );
61+ System .out .println ("2. Preemptive (Shortest Remaining Time First) " );
62+ System .out .print ("Enter your choice (1 or 2): " );
63+ int choice = sc .nextInt ();
64+
65+ switch (choice ) {
66+ case 1 :
67+ nonPreemptive ( processes );
68+ break ;
69+ case 2 :
70+ preemptive ( processes );
71+ break ;
72+ default :
73+ System . out . println ( "Invalid choice. Exiting." );
74+ return ;
75+ }
6776 }
6877 }
69-
70- private static void nonPreemptive (Process [] processes )
71- {
78+
79+ private static void nonPreemptive (Process [] processes ) {
7280 int n = processes .length ;
73- int completed = 0 ;
81+ int completedProcesses = 0 ;
7482 int currentTime = 0 ;
7583 boolean [] isCompleted = new boolean [n ];
7684
77- while (completed != n )
78- {
79- int idx = -1 ;
80- int minBurst = Integer .MAX_VALUE ;
81- for (int i = 0 ; i < n ; i ++)
82- {
83- if ((processes [i ].arrivalTime <= currentTime ) && !isCompleted [i ])
84- {
85- if (processes [i ].burstTime < minBurst )
86- {
87- minBurst = processes [i ].burstTime ;
88- idx = i ;
89- }
90- if (processes [i ].burstTime == minBurst )
91- {
92- if (processes [i ].priority < processes [idx ].priority )
93- {
94- idx = i ;
85+ while (completedProcesses < n ) {
86+ int selectedProcessIndex = -1 ;
87+ int minBurstTime = Integer .MAX_VALUE ;
88+ int highestPriority = Integer .MAX_VALUE ;
89+
90+ for (int i = 0 ; i < n ; i ++) {
91+ if (processes [i ].getArrivalTime () <= currentTime && !isCompleted [i ]) {
92+ if (processes [i ].getBurstTime () < minBurstTime ) {
93+ minBurstTime = processes [i ].getBurstTime ();
94+ highestPriority = processes [i ].getPriority ();
95+ selectedProcessIndex = i ;
96+ }
97+ else if (processes [i ].getBurstTime () == minBurstTime ) {
98+ if (processes [i ].getPriority () < highestPriority ) {
99+ highestPriority = processes [i ].getPriority ();
100+ selectedProcessIndex = i ;
95101 }
96102 }
97103 }
98104 }
99105
100- if (idx != -1 )
101- {
102- processes [idx ].waitingTime = currentTime - processes [idx ].arrivalTime ;
103- currentTime += processes [idx ].burstTime ;
104- processes [idx ].completionTime = currentTime ;
105- processes [idx ].turnaroundTime = processes [idx ].completionTime - processes [idx ].arrivalTime ;
106- processes [idx ].responseTime = processes [idx ].waitingTime ;
107- isCompleted [idx ] = true ;
108- completed ++;
109- }
110- else
111- {
106+ if (selectedProcessIndex != -1 ) {
107+ Process currentProcess = processes [selectedProcessIndex ];
108+
109+ if (currentProcess .getResponseTime () == -1 ) {
110+ currentProcess .setResponseTime (currentTime - currentProcess .getArrivalTime ());
111+ }
112+
113+ currentProcess .setWaitingTime (currentTime - currentProcess .getArrivalTime ());
114+ currentTime += currentProcess .getBurstTime ();
115+ currentProcess .setCompletionTime (currentTime );
116+ currentProcess .setTurnaroundTime (currentProcess .getCompletionTime () - currentProcess .getArrivalTime ());
117+
118+ isCompleted [selectedProcessIndex ] = true ;
119+ completedProcesses ++;
120+ } else {
112121 currentTime ++;
113122 }
114123 }
115124
116- printResults (processes , "Non-Preemptive SJF (Shortest Job First ) Scheduling" );
125+ printResults (processes , "Non-Preemptive SJF (with Priority ) Scheduling" );
117126 }
118127
119- private static void preemptive (Process [] processes )
120- {
128+ private static void preemptive (Process [] processes ) {
121129 int n = processes .length ;
122- int completed = 0 ;
130+ int completedProcesses = 0 ;
123131 int currentTime = 0 ;
124- int minRemainingTime ;
125- int shortest = -1 ;
126- boolean check = false ;
127-
128- int [] waitingTime = new int [n ];
129- int [] turnaroundTime = new int [n ];
130- int [] completionTime = new int [n ];
131- int [] responseTime = new int [n ];
132-
133- int [] remainingTime = new int [n ];
134- for (int i = 0 ; i < n ; i ++)
135- {
136- remainingTime [i ] = processes [i ].burstTime ;
137- }
138132
139- while (completed != n )
140- {
141- minRemainingTime = Integer .MAX_VALUE ;
142- shortest = -1 ;
143- check = false ;
144-
145- for (int i = 0 ; i < n ; i ++)
146- {
147- if ((processes [i ].arrivalTime <= currentTime ) &&
148- (remainingTime [i ] < minRemainingTime ) && ( remainingTime [i ] > 0 ))
149- {
150- minRemainingTime = remainingTime [i ];
151- shortest = i ;
152- check = true ;
133+ while (completedProcesses < n ) {
134+ int shortestJobIndex = -1 ;
135+ int minRemainingTime = Integer .MAX_VALUE ;
136+
137+ for (int i = 0 ; i < n ; i ++) {
138+ if (processes [i ].getArrivalTime () <= currentTime && processes [i ].getRemainingTime () > 0 ) {
139+ if (processes [i ].getRemainingTime () < minRemainingTime ) {
140+ minRemainingTime = processes [i ].getRemainingTime ();
141+ shortestJobIndex = i ;
142+ }
153143 }
154144 }
155145
156- if (!check )
157- {
146+ if (shortestJobIndex == -1 ) {
158147 currentTime ++;
159148 continue ;
160149 }
161150
162- if (responseTime [shortest ] == 0 )
163- {
164- responseTime [shortest ] = currentTime - processes [shortest ].arrivalTime ;
151+ Process currentProcess = processes [shortestJobIndex ];
152+
153+ if (currentProcess .getResponseTime () == -1 ) {
154+ currentProcess .setResponseTime (currentTime - currentProcess .getArrivalTime ());
165155 }
166156
167- remainingTime [ shortest ]-- ;
157+ currentProcess . setRemainingTime ( currentProcess . getRemainingTime () - 1 ) ;
168158 currentTime ++;
169159
170- if (remainingTime [shortest ] == 0 )
171- {
172- completed ++;
173- completionTime [shortest ] = currentTime ;
174- turnaroundTime [shortest ] = completionTime [shortest ] - processes [shortest ].arrivalTime ;
175- waitingTime [shortest ] = turnaroundTime [shortest ] - processes [shortest ].burstTime ;
176- if (waitingTime [shortest ] < 0 )
177- {
178- waitingTime [shortest ] = 0 ;
160+ if (currentProcess .getRemainingTime () == 0 ) {
161+ completedProcesses ++;
162+ currentProcess .setCompletionTime (currentTime );
163+ currentProcess .setTurnaroundTime (currentProcess .getCompletionTime () - currentProcess .getArrivalTime ());
164+ currentProcess .setWaitingTime (currentProcess .getTurnaroundTime () - currentProcess .getBurstTime ());
165+
166+ if (currentProcess .getWaitingTime () < 0 ) {
167+ currentProcess .setWaitingTime (0 );
179168 }
180169 }
181170 }
182171
183- for (int i = 0 ; i < n ; i ++)
184- {
185- processes [i ].completionTime = completionTime [i ];
186- processes [i ].turnaroundTime = turnaroundTime [i ];
187- processes [i ].waitingTime = waitingTime [i ];
188- processes [i ].responseTime = responseTime [i ];
189- }
190-
191- printResults (processes , "Preemptive SRTF (Shortest Remaining Time First) Scheduling" );
172+ printResults (processes , "Preemptive SRTF Scheduling" );
192173 }
193174
194- private static void printResults (Process [] processes , String title )
195- {
175+ private static void printResults (Process [] processes , String title ) {
196176 System .out .println ("\n " + title );
197177 System .out .printf ("%-10s%-10s%-15s%-15s%-17s%-15s%-17s%-15s\n " ,
198- "Process" , "Priority" , "Arrival Time" , "Burst Time" ,
199- "Completion Time" , "Waiting Time" , "Turnaround Time" , "Response Time\n " );
178+ "Process" , "Priority" , "Arrival Time" , "Burst Time" ,
179+ "Completion Time" , "Waiting Time" , "Turnaround Time" , "Response Time" );
200180
201181 double totalWT = 0 , totalTAT = 0 ;
202182
203- Arrays .sort (processes , (a , b ) -> a .pid - b .pid );
183+ Comparator <Process > byPid = (p1 , p2 ) -> Integer .compare (p1 .getPid (), p2 .getPid ());
184+ Arrays .sort (processes , byPid );
204185
205- for (Process p : processes )
206- {
207- totalWT += p .waitingTime ;
208- totalTAT += p .turnaroundTime ;
186+ for (Process p : processes ) {
187+ totalWT += p .getWaitingTime ();
188+ totalTAT += p .getTurnaroundTime ();
209189 System .out .printf ("%-10d%-10d%-15d%-15d%-17d%-15d%-17d%-15d\n " ,
210- p . pid , p .priority , p .arrivalTime , p .burstTime ,
211- p . completionTime , p .waitingTime , p .turnaroundTime , p .responseTime );
190+ p . getPid () , p .getPriority () , p .getArrivalTime () , p .getBurstTime () ,
191+ p . getCompletionTime () , p .getWaitingTime () , p .getTurnaroundTime () , p .getResponseTime () );
212192 }
213193
214- System .out .printf ("Average Waiting Time: %.2f\n " , totalWT / processes .length );
194+ System .out .printf ("\n Average Waiting Time: %.2f\n " , totalWT / processes .length );
215195 System .out .printf ("Average Turnaround Time: %.2f\n " , totalTAT / processes .length );
216196 }
217- }
197+ }
0 commit comments