0% found this document useful (0 votes)
49 views7 pages

"Compute" "Pre-Emptive Priority Scheduling Algorithm "

The document describes a Java program that implements preemptive priority scheduling algorithm. It includes classes and methods to: 1) Get input data like process name, burst time, arrival time, priority for each process. 2) Sort processes by priority and implement the scheduling algorithm to assign processes to time slots. 3) Calculate turnaround time and waiting time for each process. 4) Display the scheduling details like Gantt chart and output tables.

Uploaded by

Ankit Goyal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

"Compute" "Pre-Emptive Priority Scheduling Algorithm "

The document describes a Java program that implements preemptive priority scheduling algorithm. It includes classes and methods to: 1) Get input data like process name, burst time, arrival time, priority for each process. 2) Sort processes by priority and implement the scheduling algorithm to assign processes to time slots. 3) Calculate turnaround time and waiting time for each process. 4) Display the scheduling details like Gantt chart and output tables.

Uploaded by

Ankit Goyal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PPScheduling.java 13/Dec/2010 1 import javax.swing.*; 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.table.*; 5 import java.util.

*; 6 7 public class PPScheduling extends JFrame { 8 9 static int length = 0; 10 static int[]tempbursttime = new int[100]; 11 static int[]processName = new int[100]; 12 static int[]arrivalTime = new int[100]; 13 static int[]burstTime = new int[100]; 14 static int[]priority = new int[100]; 15 static boolean[]finished = new boolean[100]; 16 17 static int[]ttime = new int[100]; 18 static int[]task = new int[1000000]; 19 20 static int finish = 0; 21 static int time = 0; 22 static int totalTime = 0; 23 24 JToolBar toolbar = new JToolBar(); 25 JButton computeButton = new JButton("Compute"); 26 JLabel titleLabel = new JLabel("Pre-emptive Priority Scheduling Algorithm "); 27 JPanel toolPanel = new JPanel(); 28 JTabbedPane bodyTabbedpane = new JTabbedPane(); 29 30 static JTextArea taskArea = new JTextArea(); 31 static JTextArea ganttArea = new JTextArea(); 32 33 static DefaultTableModel modelData = new DefaultTableModel(); 34 static DefaultTableModel modelCalc = new DefaultTableModel(); 35 static DefaultTableModel modelCalcResult = new DefaultTableModel(); 36 37 JTable dataTable = new JTable(modelData); 38 JTable calcTable = new JTable(modelCalc); 39 JTable calcResultTable = new JTable(modelCalcResult); 40 41 JScrollPane dataScroll = new JScrollPane(dataTable); 42 JScrollPane calcScroll = new JScrollPane(calcTable); 43 JScrollPane calcResultScroll = new JScrollPane(calcResultTable); 44 JScrollPane taskResultScroll = new JScrollPane(taskArea); 45 JScrollPane ganttAreaScroll = new JScrollPane(ganttArea); 46 47 JSplitPane calcSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, 48 calcResultScroll, taskResultScroll); 49 JSplitPane tbtSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 50 calcScroll, calcSplitPane); 51 52 53 public PPScheduling() { 54 toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS)); 55 toolbar.add(computeButton); 56 toolbar.add(Box.createHorizontalGlue()); 57 toolbar.add(titleLabel); 58 59 modelData.addColumn("Process"); 60 modelData.addColumn("Burst Time"); 61 modelData.addColumn("Arrival Time"); 62 modelData.addColumn("Priority"); 63 64 modelCalc.addColumn("Process"); 65 modelCalc.addColumn("Turnaround Time"); 66 modelCalc.addColumn("Waiting Time"); 67 68 modelCalcResult.addColumn("Time"); 69 modelCalcResult.addColumn("Task"); 70 modelCalcResult.addColumn("Priority"); 71 72 calcSplitPane.setResizeWeight(0.5); 73 tbtSplitPane.setResizeWeight(0.5); 74 75 bodyTabbedpane.addTab("Entered Data/Process", dataScroll); 76 bodyTabbedpane.addTab("Temp/Burst/Time Calculation", tbtSplitPane); 77 bodyTabbedpane.addTab("Gannt Chart", ganttAreaScroll); 1 of 7

PPScheduling.java 13/Dec/2010 78 79 this.add(toolbar, BorderLayout.NORTH); 80 this.add(bodyTabbedpane, BorderLayout.CENTER); 81 82 computeButton.addActionListener(new ActionListener() { 83 public void actionPerformed(ActionEvent ae) { 84 try { 85 86 87 88 while (modelData.getRowCount()>0){ 89 modelData.removeRow(0); 90 } 91 92 while (modelCalc.getRowCount()>0){ 93 modelCalc.removeRow(0); 94 } 95 96 while (modelCalcResult.getRowCount()>0){ 97 modelCalcResult.removeRow(0); 98 } 99 100 taskArea.setText(""); 101 ganttArea.setText(""); 102 103 getInputData(); 104 outputInputData(); 105 106 bubble(); 107 copyBurstTime(); 108 processInputData(); 109 outputprocess(); 110 computeAverage(); 111 generateTaskProcess(); 112 displayTable(); 113 114 } catch(Exception e) { 115 return; 116 } 117 } 118 }); 119 120 } 121 122 123 124 public static void main(String args[]) { 125 try { 126 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 127 } catch (Exception e) { } 128 129 PPScheduling pps = new PPScheduling(); 130 131 int width = 500; 132 int height = 450; 133 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 134 int x = (screen.width-width)/2; 135 int y = (screen.height-height)/2; 136 137 pps.setBounds(x, y, width, height); 138 pps.setTitle("Pre-emptive Priority Scheduling Algorithm"); 139 pps.setDefaultCloseOperation( EXIT_ON_CLOSE ); 140 pps.setVisible(true); 141 142 143 } 144 145 public static void getInputData() { 146 147 String parseValue = ""; 148 parseValue = JOptionPane.showInputDialog(null, "Enter Number of Process(es)?" 149 150 if(parseValue.equalsIgnoreCase("")) { 151 JOptionPane.showMessageDialog(null, "Please enter a valid input!"); 152 } else { 153 length = Integer.parseInt(parseValue); 154 } 2 of 7

PPScheduling.java 13/Dec/2010 155 156 157 158 for (int x=0; x<length; x++) { 159 processName[x] = x; 160 161 String parseProcess, parseArialTime, parsePriority; 162 163 parseProcess = JOptionPane.showInputDialog(null, "Burst Time for Process + (x + 1)); 164 165 if(parseProcess.equalsIgnoreCase("")) { 166 JOptionPane.showMessageDialog(null, "Please enter a valid input!"); 167 return; 168 } else { 169 burstTime[x] = Integer.parseInt(parseProcess); 170 } 171 172 parseArialTime = JOptionPane.showInputDialog(null, "Arrival Time for Proc " + (x + 1)); 173 174 if(parseArialTime.equalsIgnoreCase("")) { 175 JOptionPane.showMessageDialog(null, "Please enter a valid input!"); 176 return; 177 } else { 178 arrivalTime[x] = Integer.parseInt(parseArialTime); 179 } 180 181 parsePriority = JOptionPane.showInputDialog(null, "Priority for Process " + (x + 1)); 182 183 if(parsePriority.equalsIgnoreCase("")) { 184 JOptionPane.showMessageDialog(null, "Please enter a valid input!"); 185 return; 186 } else { 187 priority[x] = Integer.parseInt(parsePriority); 188 } 189 190 finished[x] = false; 191 } 192 193 194 195 } 196 197 public static void generateTaskProcess() { 198 int i; 199 int currentPriority; 200 int currentProcess; 201 boolean selected=false; 202 203 totalTime=0; 204 time=1; 205 206 207 for (i=0; i<=length-1; i++) { 208 totalTime=totalTime+burstTime[i]; 209 } 210 211 totalTime++; 212 213 214 currentProcess=0; 215 currentPriority=priority[0]; 216 217 int cnt = 0; 218 while (time<totalTime){ 219 selected=false; 220 221 222 for (i=0; i<=length-1; i++){ 223 224 if ((time==arrivalTime[i]) && (finished[i]==false)) { 225 if (currentPriority>priority[i]) { 226 currentPriority=priority[i]; 227 currentProcess=i; 228 selected=true; 3 of 7

PPScheduling.java 229 } else { 230 231 if ((currentPriority==priority[i])&&(burstTime[i]<burstTime[currentProcess])) { 232 currentProcess=i; 233 currentPriority=priority[i]; 234 selected=true; 235 } 236 } 237 } else { 238 239 if ((time>arrivalTime[i])&&(currentPriority>priority[i])&&(finished[i]==false)) { 240 currentPriority=priority[i]; 241 currentProcess=i; 242 selected=true; 243 } 244 } 245 246 if (selected==true) { 247 break; 248 } 249 } 250 251 task[time]=processName[currentProcess]; 252 burstTime[currentProcess]=burstTime[currentProcess]-1; 253 254 255 256 Object[] rowData3 = {time,task[time],currentPriority}; 257 modelCalcResult.insertRow(cnt, rowData3); 258 cnt++; 259 time++; 260 261 262 if (burstTime[currentProcess]==0){ 263 finished[currentProcess]=true; 264 for (i=0; i<=length-1; i++) { 265 if (finished[i]==false) 266 { 267 currentProcess=i; 268 currentPriority=priority[currentProcess]; 269 break; 270 } 271 } 272 } 273 274 } 275 } 276 277 public static void displayTable(){ 278 int lineCount=0; 279 int i=1,tempi=1; 280 281 282 while (i<totalTime){ 283 284 ganttArea.append("Time :"); 285 for (lineCount=0; lineCount<=10; lineCount++) 286 { 287 if (tempi<10) 288 ganttArea.append("|-0"+tempi+"-|"); 289 else 290 ganttArea.append("|-"+tempi+"-|"); 291 tempi++; 292 } 293 294 ganttArea.append("\nProcess :"); 295 for (lineCount=0; lineCount<=10; lineCount++) 296 { 297 if (task[i]<10) 298 ganttArea.append("|P0"+task[i]+"-|"); 299 else 300 ganttArea.append("|P"+task[i]+"-|"); 301 302 i++; 303 4 of 7

13/Dec/2010

PPScheduling.java 13/Dec/2010 304 if (i==totalTime) 305 break; 306 } 307 ganttArea.append("\n\n"); 308 } 309 } 310 311 public static void outputInputData() { 312 for (int x=0; x<length;x++) { 313 314 Object[] rowData = {processName[x] + 1, burstTime[x], arrivalTime[x], priority[x]}; 315 modelData.insertRow(x, rowData); 316 317 318 } 319 } 320 321 public static void copyBurstTime() { 322 for (int x=0; x<length; x++) { 323 tempbursttime[x]=burstTime[x]; 324 } 325 } 326 327 static void processInputData() { 328 int s=0; 329 330 while(finish!=length){ 331 do { 332 333 for (int x=0;x<length;x++) { 334 335 s=0; 336 if ((arrivalTime[x]<=time)&&(tempbursttime[x]!=0)){ 337 time=time+1; 338 tempbursttime[x]=tempbursttime[x]-1; 339 ttime[x]=time; 340 341 if (tempbursttime[x]==0) { 342 finish=finish+1; 343 } 344 345 s = 1; 346 347 } 348 349 if (s==1) { 350 while (x!=length){ 351 x++; 352 } 353 } 354 } 355 356 } while(s==1); 357 time=time+1; 358 } 359 } 360 361 public static void tcoutinput() { 362 System.out.print("--PROCESS|--BURSTTIME|--ARRIVAL TIME|--PRIORITY|\n"); 363 for (int x=0;x<length;x++) { 364 System.out.print(" "+processName[x]+"|"+" "+burstTime[x]+"|") 365 System.out.print(" "+arrivalTime[x]+"|"); 366 System.out.print(" "+priority[x]+"|"); 367 System.out.print("\n"); 368 369 } 370 371 } 372 373 public static void outputprocess(){ 374 for (int x=0;x<length;x++) { 375 Object[] rowData2 = {processName[x] + 1,(ttime[x]-arrivalTime[x]),(ttime[x]-arrivalTime[x]-burstTime[x])}; 376 modelCalc.insertRow(x, rowData2); 377 378 } 5 of 7

PPScheduling.java 13/Dec/2010 379 } 380 381 public static void bubble(){ 382 int i,j; 383 for (i=0;i<length;i++){ 384 for (j=0;j<(length-1-i);j++) 385 { 386 if (priority[j+1]<priority[j]){ 387 swap(j+1,j); 388 } 389 } 390 } 391 } 392 393 public static void swap(int x,int y) { 394 int temp,temp2; 395 int[] temp3 = new int[100]; 396 int temp4; 397 398 temp=burstTime[x]; 399 temp2=priority[x]; 400 temp3[x]=processName[x]; 401 temp4=arrivalTime[x]; 402 403 //transfer 404 burstTime[x]=burstTime[y]; 405 priority[x]=priority[y]; 406 processName[x]=processName[y]; 407 arrivalTime[x]=arrivalTime[y]; 408 409 //get from temp 410 burstTime[y]=temp; 411 priority[y]=temp2; 412 processName[y]=temp3[x]; 413 arrivalTime[y]=temp4; 414 } 415 416 public static void computeAverage(){ 417 int totaltt=0,totalwt=0; 418 int x,time=0; 419 420 for (x=0;x<length;x++){ 421 totaltt=totaltt+ttime[x]-arrivalTime[x]; 422 totalwt=totalwt+ttime[x]-arrivalTime[x]-burstTime[x]; 423 } 424 425 taskArea.append("Average Turn Around Time - [ "); 426 for (x=0;x<length;x++){ 427 if (x!=length-1){ 428 taskArea.append("" + (ttime[x]-arrivalTime[x])+ "+"); 429 430 } else { 431 taskArea.append("" + (ttime[x]-arrivalTime[x])); 432 433 } 434 } 435 436 taskArea.append(" ]/"+length); 437 taskArea.append(" = "+totaltt/(float)length+" "); 438 taskArea.append("\n"); 439 taskArea.append("Average Waiting Time - "); 440 taskArea.append("[ "); 441 442 443 444 for (x=0;x<length;x++) { 445 446 if (x!=length-1) 447 taskArea.append("" + (ttime[x]-arrivalTime[x]-burstTime[x] + "+")); 448 else 449 taskArea.append("" + (ttime[x]-arrivalTime[x]-burstTime[x])); 450 } 451 452 453 taskArea.append(" ]/"+length); 454 taskArea.append(" = "+(totalwt/(float)length)+"\n"); 455 } 6 of 7

PPScheduling.java 456 457 }

13/Dec/2010

7 of 7

You might also like