Shortest Job First
Shortest Job First
Shortest Job first is a scheduling policy that selects for execution the waiting process with the smallest
execution time. SJN is a non-preemptive algorithm. Shortest remaining time is a preemptive variant of
SJF
Shortest job next is advantageous because of its simplicity and because it minimizes the average amount
of time each process has to wait until its execution is complete. However, it has the potential for process
starvation for processes which will require a long time to complete if short processes are continually
added.
Another disadvantage of using shortest job next is that the total execution time of a job must be known
before execution. While it is impossible to predict execution time perfectly, several methods can be
used to estimate it, such as a weighted average of previous execution times.
Shortest job next is used in specialized environments where accurate estimates of running time are
available.
CODE
#include<stdio.h>
int main()
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("p%d:",i+1);
scanf("%d",&bt[i]);
for(i=0;i<n;i++)
pos=i;
for(j=i+1;j<n;j++)
if(bt[j]<bt[pos])
pos=j;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
total=0;
for(i=0;i<n;i++)
total+=tat[i];
}
FIRST COME FIRST SERVED
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling
algorithm. FIFO simply queues processes in the order that they arrive in the ready queue. This is
commonly used for a task queue.
1. Since context switches only occur upon process termination, and no reorganization of the
process queue is required, scheduling overhead is minimal.
2. Throughput can be low, because long processes can be holding CPU, waiting the short processes
for a long time (known as convoy effect).
3. No starvation, because each process gets chance to be executed after a definite time.
4. Turnaround time waiting time and response time depends on the order of their arrival and can
be high for the same reasons above.
5. No prioritization occurs, thus this system has trouble meeting process deadlines.
6. The lack of prioritization means that as long as every process eventually completes, there is no
starvation. In an environment where some processes might not complete, there can be
starvation.
CODE
#include<bits/stdc++.h>
int service_time[n];
service_time[0] = 0;
wt[0] = 0;
if (wt[i] < 0)
wt[i] = 0;
cout << "Processes " << " Burst Time " << " Arrival Time "
<< " Waiting Time " << " Turn-Around Time "
cout << " " << i+1 << "\t\t" << bt[i] << "\t\t"
int main()
return 0;}
ROUND ROBIN
Round-robin (RR) is one of the algorithms employed by process and network schedulers in computing.
As the term is generally used, time slices (also known as time quanta) are assigned to each process in
equal portions and in circular order, handling all processes without priority (also known as cyclic
executive). Round-robin scheduling is simple, easy to implement, and starvation-free. Round-robin
scheduling can also be applied to other scheduling problems, such as data packet scheduling in
computer networks.
To schedule processes fairly, a round-robin scheduler generally employs time-sharing, giving each job a
time slot or quantum (its allowance of CPU time), and interrupting the job if it is not completed by then.
The job is resumed next time a time slot is assigned to that process. If the process terminates or changes
its state to waiting during its attributed time quantum, the scheduler selects the first process in the
ready queue to execute. In the absence of time-sharing, or if the quanta were large relative to the sizes
of the jobs, a process that produced large jobs would be favoured over other processes.
Round-robin algorithm is a pre-emptive algorithm as the scheduler forces the process out of the CPU
once the time quota expires.
CODE
#include<stdio.h>
int main()
{ int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{ printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
scanf("%d",&time_quantum);
for(time=0,count=0;remain!=0;)
time+=rt[count];
rt[count]=0;
flag=1;
else if(rt[count]>0)
rt[count]-=time_quantum;
time+=time_quantum;
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
return 0;
}
SHORTEST JOB REMAINING
In this scheduling algorithm, the process with the smallest amount of time remaining until completion is
selected to execute. Since the currently executing process is the one with the shortest amount of time
remaining by definition, and since that time should only reduce as execution progresses, processes will
always run until they complete or a new process is added that requires a smaller amount of time.
Shortest remaining time is advantageous because short processes are handled very quickly. The system
also requires very little overhead since it only makes a decision when a process completes or a new
process is added, and when a new process is added the algorithm only needs to compare the currently
executing process with the new process, ignoring all other processes currently waiting to execute.
Like shortest job first, it has the potential for process starvation; long processes may be held off
indefinitely if short processes are continually added.
CODE
#include <stdio.h>
int main()
int a[10],b[10],x[10],i,j,smallest,count=0,time,n;
double avg=0,tt=0,end;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
scanf("%d",&b[i]);
for(i=0;i<n;i++)
x[i]=b[i];
b[9]=9999;
for(time=0;count!=n;time++)
smallest=9;
for(i=0;i<n;i++)
smallest=i;
b[smallest]--;
if(b[smallest]==0)
count++;
end=time+1;
avg=avg+end-a[smallest]-x[smallest];
tt= tt+end-a[smallest];
return 0;