Here you will get java program for shortest job first (sjf) scheduling algorithm, both preemptive and non-preemptive.

Shortest job first scheduling algorithm can also be known as shortest job next scheduling. It is very easy to implement and efficient in reducing average response time. Now we will see how it will work with the example and its implementation.

In shortest job first, we should know the execution time of each process before running. This we can estimate in many ways. This is the prerequisite for SJF.

Suppose we have set of processes are in ready queue. The SJF scheduling algorithm will choose the job which has shortest remaining time to complete. We have 2 variations of this SJF algorithm that are preemptive and non-preemptive. Preemptive version of SJF also known as SRTF.

Non-Preemptive

Example:

Process id

Arrival time

Burst time

P1

0

3

P2

0

1

P3

0

2

We have 3 processes in our ready queue. As we discussed SJF will schedule the job which is having least execution time or burst time.

Gantt Chart

Now we will calculate the completion time, waiting time, turnaround time of each process.

Process id

Completion time

Waiting time

Turnaround time

P1

6

3

6

P2

1

0

1

P3

3

1

3

Average waiting time = (3+0+1)/3 = 1.33

Turnaround time = (6+1+3)/3 = 3.33

Java Program for Shortest Job First (SJF) Scheduling (Non-Preemptive)

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

importjava.util.*;

publicclassSJF{

publicstaticvoidmain(Stringargs[])

{

Scanner sc=newScanner(System.in);

System.out.println("enter no of process:");

intn=sc.nextInt();

intpid[]=newint[n];

intat[]=newint[n];// at means arrival time

intbt[]=newint[n];// bt means burst time

intct[]=newint[n];// ct means complete time

intta[]=newint[n];// ta means turn around time

intwt[]=newint[n];//wt means waiting time

intf[]=newint[n];// f means it is flag it checks process is completed or not

intst=0,tot=0;

floatavgwt=0,avgta=0;

for(inti=0;i<n;i++)

{

System.out.println("enter process "+(i+1)+" arrival time:");

at[i]=sc.nextInt();

System.out.println("enter process "+(i+1)+" brust time:");

bt[i]=sc.nextInt();

pid[i]=i+1;

f[i]=0;

}

booleana=true;

while(true)

{

intc=n,min=999;

if(tot==n)// total no of process = completed process loop will be terminated

break;

for(inti=0;i<n;i++)

{

/*

* If i'th process arrival time <= system time and its flag=0 and burst<min

* That process will be executed first

*/

if((at[i]<=st)&&(f[i]==0)&&(bt[i]<min))

{

min=bt[i];

c=i;

}

}

/* If c==n means c value can not updated because no process arrival time< system time so we increase the system time */

Execution time should be estimated prior to the scheduling. It will incur some cost.

Now we will see preemptive version of SJF that is SRTF (shortest remaining tie first)

Preemptive

In SRTF the selection of job is same like in SJF. But the difference is In SJF process will run till completion. In SRTF process will run till completion or a new process added into queue which is having smaller execution time than the current process remaining execution time.

Process id

Arrival time

Burst time

P1

2

3

P2

1

2

P3

3

4

P4

5

6

When process is added to queue or process is completed then only CPU may switch the process.

Gantt Chart

Process id

Completion time

Waiting time

Turnaround time

P1

6

1

4

P2

3

0

2

P3

10

3

7

P4

16

5

11

Average turnaround time = (4+2+7+11)/4 = 6.0

Average waiting time = (1+0+3+5)/4 = 2.25

Java Program for Shortest Job First (SRTF) Scheduling (Preemptive)

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

importjava.util.*;

publicclassSRTF{

publicstaticvoidmain(Stringargs[])

{

Scanner sc=newScanner(System.in);

System.out.println("enter no of process:");

intn=sc.nextInt();

intpid[]=newint[n];// it takes pid of process

intat[]=newint[n];// at means arrival time

intbt[]=newint[n];// bt means burst time

intct[]=newint[n];// ct means complete time

intta[]=newint[n];// ta means turn around time

intwt[]=newint[n];// wt means waiting time

intf[]=newint[n];// f means it is flag it checks process is completed or not