|
| 1 | +/* |
| 2 | + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license |
| 3 | + */ |
| 4 | + |
| 5 | +package com.mycompany.preemptivesjf; |
| 6 | +import java.util.*; |
| 7 | +/** |
| 8 | + * |
| 9 | + * @author lenovo |
| 10 | + */ |
| 11 | +public class PreemptiveSJF { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + System.out.println("Enter the number of processes"); |
| 15 | + Scanner sc= new Scanner(System.in); |
| 16 | + int pno=sc.nextInt(); |
| 17 | + |
| 18 | + int[] at=new int[pno]; |
| 19 | + int[] bt=new int[pno]; |
| 20 | + int[] rt=new int[pno]; //remaining time |
| 21 | + int[] wt=new int[pno]; |
| 22 | + int[] bt1=new int[pno]; |
| 23 | + int[] ct=new int[pno]; |
| 24 | + int[] flag=new int[pno]; |
| 25 | + int[] tat=new int[pno]; |
| 26 | + int[] pid=new int[pno]; |
| 27 | + |
| 28 | + System.out.println("Enter the arrival time"); |
| 29 | + for(int i=0;i<pno;i++) |
| 30 | + { |
| 31 | + at[i]=sc.nextInt(); |
| 32 | + } |
| 33 | + |
| 34 | + System.out.println("Enter the burst time"); |
| 35 | + for(int i=0;i<pno;i++) |
| 36 | + { |
| 37 | + bt[i]=sc.nextInt(); |
| 38 | + } |
| 39 | + System.out.println("Please wait!"); |
| 40 | + //initialising flag |
| 41 | + for(int i=0;i<pno;i++) |
| 42 | + { |
| 43 | + flag[i]=0; |
| 44 | + } |
| 45 | + |
| 46 | + //copying burst time in remaining time |
| 47 | + |
| 48 | + for(int i=0;i<pno;i++) |
| 49 | + { |
| 50 | + rt[i]=bt[i]; |
| 51 | + bt1[i]=bt[i]; //for priniting purpose only |
| 52 | + |
| 53 | + } |
| 54 | + int st=0; |
| 55 | + int count=0; |
| 56 | + int smallest=0; |
| 57 | + //max number |
| 58 | + int end;//end time |
| 59 | + for(st=0;count!=pno;st++) |
| 60 | + { |
| 61 | + int min=99999; |
| 62 | + |
| 63 | + for(int i=0; i<pno; i++) |
| 64 | + { |
| 65 | + if(at[i]<=st && bt[i]<min && bt[i]>0 ) //chosing the smallest burst time |
| 66 | + smallest=i; //choosing the smallest burst time of all |
| 67 | + min=bt[i]; |
| 68 | + } |
| 69 | + bt[smallest]--; //dec burst time by 1...we decrement step by step i.e 1 by 1 to check if there is any other process at that particular time having burst time less |
| 70 | + |
| 71 | + |
| 72 | + if(bt[smallest]==0) // iterate through loop untill bt=0 i.e process execution is completed |
| 73 | + { |
| 74 | + pid[smallest]++; |
| 75 | + count++; |
| 76 | + end=st+1; |
| 77 | + tat[smallest] = end - at[smallest]; |
| 78 | + ct[smallest] = end; |
| 79 | + wt[smallest] =tat[smallest] - bt1[smallest]; |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + System.out.println("\tpid\t\tat\t\tbt\t\ttat\t\twt\t\tct\t\t"); |
| 85 | + for(int i=0;i<pno;i++) |
| 86 | + { |
| 87 | + System.out.println("\t\t"+pid[i]+"\t\t"+at[i]+"\t\t"+bt1[i]+"\t\t"+tat[i]+"\t\t"+wt[i]+"\t\t"+ct[i]+"\t\t"); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + } |
| 94 | +} |
0 commit comments