## Shortest Job First.

Q62. Define Shortest Job First.

Ans. The ready queue is sorted on the basis of the process size entering the queue and the shortest process is assigned the processor first.

## Drawbacks of SJF

Q9. What are the drawbacks of SJF?

Ans. We allocate CPU to shortest job so the job with more burst keeps on waiting. It is known as starvation. A particular process keeps on waiting, but it is not allocated the CPU. To prevent this, use aging.

According to waiting time, we calculate three age after a particular check time and after that the aged processes is given the system (10-15 ms).

## Numerical Solved by Shortest Job First Scheduling Algorithm

Q8. Consider the following:

Arrive Time             Process                Burst Time

0                               P1                        10

1                               P2                        4

2                               P3                        3

3                               P4                        1

Draw Gantt Chart and calculate average turnaround and waiting time using Shortest Job First Scheduling Algorithm.

Grant Chart:

 P1 P2 P4 P2 P3 P1

0           1                     3           4                         6                               9                              18

Turnaround Time

P1=18-0=18

P2=6-1=5

P3=9-2=7

P4=4-3=1

Average Turnaround Time

=(18+5+7+1)/4

=31/4

=7.77 ms

Waiting Time

P1’s waiting time=9-1=8

P2’s waiting time=4-3=1

P3’s waiting time=6-2=4

P4’s waiting time=3-3=0

Average Waiting Time

=(8+1+4+0)/4

=13/4

=3.25 ms

## Preemptive Shortest Job First

Q6. Explain Preemptive Shortest Job First.

Ans. If a new process arrives with CPU burst length less than remaining time of current executing process, a preemptive SJF algorithm will preempt the currently executing process. This scheme is known as the Shortest Remaining Time First (SRTF).

## Numerical Solved by SJF

Q5. Consider the set of process P1,P2,P3,P4 and P5 having burst time as 10,1,2,1 and 5 ms and priority 5,1,3,4 and 2. The processes are assumed to have arrived at time 0, then draw Gantt Chart and calculate average turnaround and waiting time using Shortest Job First Scheduling Algorithm.

Ans. Solving by SJF

Process                Burst Time           Priority Time

P1                        10                        5

P2                        1                           1

P3                        2                           3

P4                        1                           4

P5                        5                           2

Gantt Chart

 P2 P4 P3 P5 P1

0               1               2                      4                            9                                                    19

Turnaround Time

P1=19

P2=1

P3=4

P4=2

P5=9

Average Turnaround Time

=(19+1+4+2+9)/5

=35/5

=7 milliseconds

Waiting Time

P1’s waiting time=9

P2’ waiting time=0

P3’s waiting time=2

P4’swaiting time=1

P5’s waiting time=4

Average Waiting Time

=(9+0+2+1+4)/5

=16/5

=3.2 ms

## Non-preemptive Shortest Job First Scheduling Algorithm

Q3. Explain Non-preemptive Shortest Job First Scheduling with example.

Ans. Once CPU is given to the process it cannot be preempted until completes its CPU burst.

Let’s take four processes that arrive at the same time in this order:

Process               CPU Time Needed (ms)

P1                         6

P2                         8

P3                         7

P4                         3

Gantt Chart:

 P4 P1 P3 P2

0                    3                       9                                        16                                                 24

P1’ waiting time=3

P2’s waiting time=16

P3’s waiting time=9

P4’s waiting time=0

Average Waiting Time

=(3+16+9+0)/4

=28/4

=7 milliseconds

Turnaround Time

Turnaround Time = Burst Time + Waiting Time

Process             Turnaround Time

P1                       6+3=9

P2                       8+16=24

P3                       7+9=16

P4                       3+0=3

Average Turnaround Time

=(9+24+16+3)/4

=13 milliseconds

## Explain Shortest Job First (SJF).

Q1. Explain Shortest Job First (SJF).

Ans. Clearly, the average waiting time under a purely first-in first-out system is going to often be poor if one task is significantly longer than the others.

So, the first idea that comes about after seeing this is the idea of having the shortest task go first, or shortest job first scheduling. This, obviously, would be similar to the idea above, except that as each job comes in, it is sorted into the queue based on size.

In shortest job first (SJF), waiting job (or process) with the smallest estimated run time to completion is run next. In other words, when CPU is available, it is assigned to the process that has smallest next CPU burst.

The SJF scheduling is especially appropriate for batch jobs for which the run times are known in advance.