Program of SJF - shortest job first (sjf) | Program in C++ || Dot clu
Shortest job next (SJN), also known as shortest job first (SJF) or shortest process next (SPN), 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 SJN.
Shortest job first depends on the average running time of the processes. The accurate estimates of these measures help in the implementation of the shortest job first in an environment, which otherwise makes the same nearly impossible to implement. This is because often the execution burst of processes does not happen beforehand. It can be used in interactive environments where past patterns are available to determine the average time between the waiting time and the commands. Although it is disadvantageous to use the shortest-job-first concept in short-term CPU scheduling, it is considered highly advantageous in long-term CPU scheduling. Moreover, the throughput is high in the case of shortest job first.
Shortest job first also has its share of disadvantages. For one, it can cause process starvation for longer jobs if there are a large number of shorter processes. Another is the need to know the execution time for each process beforehand. Often, this is almost impossible in many environments.
Source Code of SJF
- //shortest job First >> SJF
- #include<iostream>
- using namespace std;
- int main()
- {
- int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
- cout<<"Enter Total Number of Process:";
- cin>>n;
- cout<<"\nEnter Burst Time and Priority\n";
- for(i=0;i<n;i++)
- {
- cout<<"\nP["<<i+1<<"]\n";
- cout<<"Burst Time:";
- cin>>bt[i];
- cout<<"Priority:";
- cin>>pr[i];
- p[i]=i+1; //contains process number
- }
- //sorting burst time, priority and process number in ascending order using selection sort
- for(i=0;i<n;i++)
- {
- pos=i;
- for(j=i+1;j<n;j++)
- {
- if(pr[j]<pr[pos])
- pos=j;
- }
- temp=pr[i];
- pr[i]=pr[pos];
- pr[pos]=temp;
- 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 is 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=total/n; //average waiting time
- total=0;
- cout<<"\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];
- cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
- }
- avg_tat=total/n; //average turnaround time
- cout<<"\n\nAverage Waiting Time="<<avg_wt;
- cout<<"\nAverage Turnaround Time="<<avg_tat;
- return 0;
- }
Also Watch Video for DEMO
dotclu
dot clu
=====Also Watch Our Video========
Program sum of "n" number in c | n number program in c || dot clu
Program of sum of 2 integer number | 2nd Program of C tutorial | Program of input integer | dot clu
Get started With C || First Program in C || C tutorial || Hello world program in C || dot clu
Install Virtual BOX in your PC | create Virtual Machine | downloading link in description || dot clu
How to Install Hyper V in windows 7?
What is Bitcoin? || Earn Bitcoin money with your Android phone || 100% working || dot clu || Hindi
Comments
Post a Comment