Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class PROIRTYWITHPREEMPTION{
- public static void main(String args[]) {
- int sumw = 0, sumt = 0, sumBurst = 0, n, i, j, k, temp, preJobNo, doJob=0;
- double avgWT, turnAT;
- Scanner sc = new Scanner(System.in);
- System.out.println("ENTER NUMBER OF JOBS");
- n = sc.nextInt();
- int burstTime[] = new int[n];
- int waitTime[] = new int[n];
- int priority[] = new int[n];
- int arrival[] = new int[n];
- int jobNo[] = new int[n];
- int finished[] = new int[n];
- System.out.println("Enter burst time, Priority and Arrival time for each job :");
- for (i = 0; i < n; i++) {
- System.out.println("Enter burst time of job "+i+" : ");
- burstTime[i] = sc.nextInt();
- System.out.println("Enter priority of job "+i+" : ");
- priority[i] = sc.nextInt();
- System.out.println("Enter arrival time of job "+i+" : ");
- arrival[i] = sc.nextInt();
- jobNo[i] = i+1;
- sumBurst = sumBurst + burstTime[i];
- }
- int ganntChart[] = new int[sumBurst];
- /* Sorting all the job as per priority time */
- for (i = 0; i < n - 1; i++) {
- for (j = 0; j < n - 1; j++) {
- if (priority[j] > priority[j + 1]) {
- temp = priority[j];
- priority[j] = priority[j + 1];
- priority[j + 1] = temp;
- temp = arrival[j];
- arrival[j] = arrival[j + 1];
- arrival[j + 1] = temp;
- temp = burstTime[j];
- burstTime[j] = burstTime[j + 1];
- burstTime[j + 1] = temp;
- temp = jobNo[j];
- jobNo[j] = jobNo[j + 1];
- jobNo[j + 1] = temp;
- }
- }
- }
- /*printing the input table just for referance */
- System.out.println("JobNo\tBT\tpriority\tarrival");
- for(i=0;i<n;i++){
- System.out.println(jobNo[i]+"\t"+burstTime[i]+"\t"+priority[i]+"\t\t"+arrival[i]);
- }
- /* printing out the gannt chart int form of table */
- doJob = 0;
- System.out.println("Time Unit" + "\t" + "Job Number");
- for (i = 0; i < sumBurst; i++) {
- //get doable jobs as per arrival time
- for(j=0;j<n;j++)
- {
- if(arrival[j]<=i&&burstTime[j]!=0)
- {
- doJob = jobNo[j];
- burstTime[j]--;
- if(burstTime[j]==0)
- {
- finished[j] = i+1;
- }
- break;
- }
- }
- System.out.print(i+"-"+ (i+1) + "\t\t" + doJob);
- ganntChart[i] = doJob;
- System.out.println();
- }
- /*Calculation of Average Wait time */
- sumw = 0;
- for(i=0;i<n;i++){
- for(j=0;j<sumBurst;j++){
- if(ganntChart[j]!=jobNo[i]&&j<finished[i]){
- sumw++;
- }
- }
- sumw = sumw - arrival[i];
- }
- avgWT = sumw * (1.0) / n;
- /*Calculation of Average Turn Around time */
- j=0;
- while(j<n){
- sumt = sumt + finished[j]-arrival[j];
- j++ ;
- }
- turnAT = sumt * (1.0) / n;
- /*printing out the results*/
- System.out.println();
- System.out.println("AVERAGE WAITING TIME =" + avgWT + "\t" + "AVERAGE TURN AROUND TIME=" + turnAT);
- }
- }
- OUTPUT:
- ENTER NUMBER OF JOBS 5
- Enter burst time, Priority and Arrival time for each job :
- Enter burst time of job 0 : 10
- Enter priority of job 0 : 3
- Enter arrival time of job 0 :0
- Enter burst time of job 1 : 1
- Enter priority of job 1 : 1
- Enter arrival time of job 1 :1
- Enter burst time of job 2 : 2
- Enter priority of job 2 : 3
- Enter arrival time of job 2 :2
- Enter burst time of job 3 : 1
- Enter priority of job 3 : 4
- Enter arrival time of job 3 :3
- Enter burst time of job 4 : 5
- Enter priority of job 4 : 2
- Enter arrival time of job 4 :4
- JobNo BT priority arrival
- 2 1 1 1
- 5 5 2 4
- 1 10 3 0
- 3 2 3 2
- 4 1 4 3
- Time Unit Job Number
- 0-1 1
- 1-2 2
- 2-3 1
- 3-4 1
- 4-5 5
- 5-6 5
- 6-7 5
- 7-8 5
- 8-9 5
- 9-10 1
- 10-11 1
- 11-12 1
- 12-13 1
- 13-14 1
- 14-15 1
- 15-16 1
- 16-17 3
- 17-18 3
- 18-19 4
- AVERAGE WAITING TIME =7 AVERAGE TURN AROUND TIME=10.8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement