0% found this document useful (0 votes)
195 views12 pages

Shortest Job First

Shortest job remaining is a preemptive scheduling algorithm that selects the waiting process with the smallest remaining execution time to run next. It aims to minimize process completion times by always prioritizing nearly-finished processes. While it has high throughput by handling short jobs quickly, it risks starving long jobs if new short jobs keep arriving. The algorithm requires little overhead as it only needs to compare the current job to any new arrivals.

Uploaded by

akh yad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views12 pages

Shortest Job First

Shortest job remaining is a preemptive scheduling algorithm that selects the waiting process with the smallest remaining execution time to run next. It aims to minimize process completion times by always prioritizing nearly-finished processes. While it has high throughput by handling short jobs quickly, it risks starving long jobs if new short jobs keep arriving. The algorithm requires little overhead as it only needs to compare the current job to any new arrivals.

Uploaded by

akh yad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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;

printf("Enter number of process:");

scanf("%d",&n);

printf("\nEnter Burst Time:\n");

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

printf("p%d:",i+1);

scanf("%d",&bt[i]);

p[i]=i+1; //contains process number


}

//sorting burst time in ascending order using selection sort

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;

wt[0]=0; //waiting time for first process will be zero

//calculate waiting time

for(i=1;i<n;i++)
{

wt[i]=0;

for(j=0;j<i;j++)

wt[i]+=bt[j];

total+=wt[i];

avg_wt=(float)total/n; //average waiting time

total=0;

printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");

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

tat[i]=bt[i]+wt[i]; //calculate turnaround time

total+=tat[i];

printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);

avg_tat=(float)total/n; //average turnaround time

printf("\n\nAverage Waiting Time=%f",avg_wt);

printf("\nAverage Turnaround Time=%f\n",avg_tat);

}
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>

using namespace std;

void Waiting_Time(int processes[], int n, int bt[],

int wt[], int at[])

int service_time[n];

service_time[0] = 0;

wt[0] = 0;

for (int i = 1; i < n ; i++)

service_time[i] = service_time[i-1] + bt[i-1];


wt[i] = service_time[i] - at[i];

if (wt[i] < 0)

wt[i] = 0;

void findTurnAroundTime(int processes[], int n, int bt[],

int wt[], int tat[])

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

tat[i] = bt[i] + wt[i];

void findavgTime(int processes[], int n, int bt[], int at[])

int wt[n], tat[n];

findWaitingTime(processes, n, bt, wt, at);

findTurnAroundTime(processes, n, bt, wt, tat);

cout << "Processes " << " Burst Time " << " Arrival Time "

<< " Waiting Time " << " Turn-Around Time "

<< " Completion Time \n";


int total_wt = 0, total_tat = 0;

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

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

int compl_time = tat[i] + at[i];

cout << " " << i+1 << "\t\t" << bt[i] << "\t\t"

<< at[i] << "\t\t" << wt[i] << "\t\t "

<< tat[i] << "\t\t " << compl_time << endl;

cout << "Average waiting time = "

<< (float)total_wt / (float)n;

cout << "\nAverage turn around time = "

<< (float)total_tat / (float)n;

int main()

int processes[] = {1, 2, 3};

int n = sizeof processes / sizeof processes[0];

int burst_time[] = {5, 9, 6};

int arrival_time[] = {3, 3, 6};

findavgTime(processes, n, burst_time, arrival_time)

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];

printf("Enter Total Process:\t ");

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];

printf("Enter Time Quantum:\t");

scanf("%d",&time_quantum);

printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");

for(time=0,count=0;remain!=0;)

if(rt[count]<=time_quantum && rt[count]>0)

time+=rt[count];

rt[count]=0;

flag=1;

else if(rt[count]>0)

rt[count]-=time_quantum;

time+=time_quantum;

if(rt[count]==0 && flag==1)

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;

printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);

printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

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;

printf("enter the number of Processes:\n");

scanf("%d",&n);

printf("enter arrival time\n");

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

scanf("%d",&a[i]);

printf("enter burst time\n");

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++)

if(a[i]<=time && b[i]<b[smallest] && b[i]>0 )

smallest=i;

b[smallest]--;

if(b[smallest]==0)

count++;

end=time+1;

avg=avg+end-a[smallest]-x[smallest];

tt= tt+end-a[smallest];

printf("\n\nAverage waiting time = %lf\n",avg/n);

printf("Average Turnaround time = %lf",tt/n);

return 0;

You might also like