Sunteți pe pagina 1din 22

Shortest Remaining Time First ( SRTF ) or

preemptive sjf cpu scheduling in c


code in C:
#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;
}
output:-
enter the number of Processes:
4
enter arrival time
0
1
2
3
enter burst time
8
4
9
5
Average waiting time = 6.500000
Average Turnaround time = 13.000000
--------------------------------
#include<stdio.h>
int main()
{
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
printf("Enter no of Processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter burst time for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)
{
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-
bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}
printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
return 0;
}
Code Explanation

Line no 8-15 are used for taking inputs. The array variables `at` and `bt` stores
arrival time and burst time of processes.
Array `rt` stores the remaining time of the processes which initially are equal to
the burst time of the processes. This is the time to complete execution of all the
processes.
Line no 18 starts a loop where time starts from zero, line no 20 to 27 are used
to find a process which have the least remaining time and its arrival time is less
than or equal to the current time. This condition is checked at every unit time,
that is why time is incremented by 1 (time++).
Line no 26 is used to print the arrival time and burst time for finished processes.
At this moment, variable `time` gives the time of start of the process.
Therefore Turnaround time=(time+burst time-arrival time),
Waiting time =(time arrival time)
In line no 28 remaining time for shortest process is decreased by one since time
is increased by one.
If a particular process have just finished/completed, then it satisfies the condition
in line no 29.
Since the variable time stores the current time, and the process was running at
this moment, therefore its ending time = time+1 (line no 32).
Since
burst time+waiting time = end time - Start time
therefore
waiting time=end Time-burst time-arrival time
waiting time and turnaround time of all processes are summed up and divided by
n to give their average.

Written 28 Apr View Upvotes

Related Questions
More Answers Below

Is there an OS that has successfully implemented the Shortest Job First


scheduling algorithm?

How can I implement Shortest job first algorithm?

How do you implement a C program for round-robin scheduling with arrival


time?

How does one implement a C code for priority scheduling with arrival time
and priorities?

How do I write a C program to implement a priorityscheduling algorithm,


along with displaying the Gantt chart?
Rahul Dharan, Hacker!

2.5k Views

Here's the code I wrote which illustrates SRTF algorithm and also displays the
ghantt chart.

//*** CODE BY gOku^ ***

#include<stdio.h>

int main()

int n,ari[10],bur[10],total=0,i,j,small,temp,procs[100],k,waiting[10],finish[10];

float tavg=0.0,wavg=0.0;

printf("ENETR THE NUMBER OF PROCESSES:");

scanf("%d",&n);

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

printf("ENETR THE ARIIVAL TIME OF PROCESS %d:\t",i);

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

printf("ENETR THE BURST TIME OF PROCESS %d:\t",i);

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

waiting[i]=0;

total+=bur[i];

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

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

if(ari[i]>ari[j])

temp=ari[i];

ari[i]=ari[j];

ari[j]=temp;

temp=bur[i];

bur[i]=bur[j];

bur[j]=temp;

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

small=3200;

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

if((bur[j]!=0) && (ari[j]<=i) && (bur[j]<small))

small=bur[j]; k=j;

bur[k]--;
procs[i]=k;

k=0;

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

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

if(procs[i]==j)

finish[j]=i;

waiting[j]++;

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

printf("\nPROCESS %d:-FINISH TIME==> %d TURNAROUND TIME==>%d


WAITING TIME==>%d\n",i+1,finish[i]+1,(finish[i]-ari[i])+1,(((finish[i]+1)-
waiting[i])-ari[i]));

wavg=wavg+(((finish[i]+1)-waiting[i])-ari[i]);

tavg=tavg+((finish[i]-ari[i])+1);

printf("\n WAvG==>\t%f\n TAVG==>\t%f\n",(wavg/n),(tavg/n));

return 0;

/* to display the ghantt chat print the contents of the array procs[ ] */

Let me know if you have any problems understanding the code.


How do I write a C program to implement a SRTF (Shortest Remaining Time First)
scheduling algorithm, along with displaying the Gantt chart?

100+ million people read Quora every month

Sign in to follow people with unique insights on Computer Programming

By continuing, I agree that I am at least 13 years old and have read and agree to the Terms of
Service and Privacy Policy.

5 Answers

Avnish Yadav, 19, Computer Student ... Love to code. (*.*)

843 Views

Code

#include<stdio.h>
int main()
{
int at[10],bt[10],rt[10],endTime,i,smallest;
int remain=0,n,time,sum_wait=0,sum_turnaround=0;
printf("Enter no of Processes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter arrival time for Process P%d : ",i+1);
scanf("%d",&at[i]);
printf("Enter burst time for Process P%d : ",i+1);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("\n\nProcess\t|Turnaround Time| Waiting Time\n\n");
rt[9]=9999;
for(time=0;remain!=n;time++)
{
smallest=9;
for(i=0;i<n;i++)
{
if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)
{
smallest=i;
}
}
rt[smallest]--;
if(rt[smallest]==0)
{
remain++;
endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-
bt[smallest]-at[smallest]);
sum_wait+=endTime-bt[smallest]-at[smallest];
sum_turnaround+=endTime-at[smallest];
}
}
printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);
printf("Average Turnaround time = %f",sum_turnaround*1.0/5);
return 0;
}

Code Explanation

Line no 8-15 are used for taking inputs. The array variables `at` and `bt` stores
arrival time and burst time of processes.
Array `rt` stores the remaining time of the processes which initially are equal to
the burst time of the processes. This is the time to complete execution of all the
processes.
Line no 18 starts a loop where time starts from zero, line no 20 to 27 are used
to find a process which have the least remaining time and its arrival time is less
than or equal to the current time. This condition is checked at every unit time,
that is why time is incremented by 1 (time++).
Line no 26 is used to print the arrival time and burst time for finished processes.
At this moment, variable `time` gives the time of start of the process.
Therefore Turnaround time=(time+burst time-arrival time),
Waiting time =(time arrival time)
In line no 28 remaining time for shortest process is decreased by one since time
is increased by one.
If a particular process have just finished/completed, then it satisfies the condition
in line no 29.
Since the variable time stores the current time, and the process was running at
this moment, therefore its ending time = time+1 (line no 32).
Since
burst time+waiting time = end time - Start time
therefore
waiting time=end Time-burst time-arrival time
waiting time and turnaround time of all processes are summed up and divided by
n to give their average.

Written 28 Apr View Upvotes


Related Questions
More Answers Below

Is there an OS that has successfully implemented the Shortest Job First


scheduling algorithm?

How can I implement Shortest job first algorithm?

How do you implement a C program for round-robin scheduling with arrival


time?

How does one implement a C code for priority scheduling with arrival time
and priorities?

How do I write a C program to implement a priorityscheduling algorithm,


along with displaying the Gantt chart?

Rahul Dharan, Hacker!

2.5k Views

Here's the code I wrote which illustrates SRTF algorithm and also displays the
ghantt chart.

//*** CODE BY gOku^ ***

#include<stdio.h>

int main()

int n,ari[10],bur[10],total=0,i,j,small,temp,procs[100],k,waiting[10],finish[10];

float tavg=0.0,wavg=0.0;

printf("ENETR THE NUMBER OF PROCESSES:");

scanf("%d",&n);

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

printf("ENETR THE ARIIVAL TIME OF PROCESS %d:\t",i);

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

printf("ENETR THE BURST TIME OF PROCESS %d:\t",i);

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

waiting[i]=0;

total+=bur[i];

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

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

if(ari[i]>ari[j])

temp=ari[i];

ari[i]=ari[j];

ari[j]=temp;

temp=bur[i];

bur[i]=bur[j];

bur[j]=temp;

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

{
small=3200;

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

if((bur[j]!=0) && (ari[j]<=i) && (bur[j]<small))

small=bur[j]; k=j;

bur[k]--;

procs[i]=k;

k=0;

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

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

if(procs[i]==j)

finish[j]=i;

waiting[j]++;

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

printf("\nPROCESS %d:-FINISH TIME==> %d TURNAROUND TIME==>%d


WAITING TIME==>%d\n",i+1,finish[i]+1,(finish[i]-ari[i])+1,(((finish[i]+1)-
waiting[i])-ari[i]));
wavg=wavg+(((finish[i]+1)-waiting[i])-ari[i]);

tavg=tavg+((finish[i]-ari[i])+1);

printf("\n WAvG==>\t%f\n TAVG==>\t%f\n",(wavg/n),(tavg/n));

return 0;

/* to display the ghantt chat print the contents of the array procs[ ] */

Let me know if you have any problems understanding the code.

Peace !

Written 11 Jun 2015 View Upvotes

Mnish Mehta, B.Tech 3rd year student

5.7k Views

#include<stdio.h>
#include<malloc.h>
int n,*a;
struct pro
{
int name,at,bt,tt,wt,left,status;
}*p;
void create(int num)
{
a=(int*)malloc(num*sizeof(int));
}
int smallers(int num)
{
int c=0,i;
for(i=0;i<n;i++)
{
if((p+i)->bt<num && (p+i)->status==0)
{
*(a+c)=(p+i)->bt;
c++;
}
}
return c;
}
int search(int num)
{
int i;
for(i=0;i<n;i++)
{
if((p+i)->bt==num && (p+i)->status==0)
return i;
}
}
int main()
{
int i,c_time=0,b,s_nums,j,pro_num,p_time;
float att=0,awt=0;
printf("Enter no. of processes- ");
scanf("%d",&n);
create(n);
p=(struct pro *)malloc(n*sizeof(struct pro));
for(i=0;i<n;i++)
(p+i)->status=0;
printf("---Enter Process Name, Arrival Time & CPU Burst Time---\n");
for(i=0;i<n;i++)
scanf("%d %d %d",&(p+i)->name,&(p+i)->at,&(p+i)->bt);
for(i=0;i<n;i++)
{
if((p+i)->status==0)
{
b=(p+i)->bt;
s_nums=smallers(b);
if(s_nums>0)
{
(p+i)->left=(p+i)->bt;
for(j=0;j<s_nums;j++)
{
p_time=c_time;
pro_num=search(*(a+j));
if(c_time<(p+pro_num)->at)
{
if((p+i)->left>(p+pro_num)->at)
{
c_time=(p+pro_num)->at;
(p+i)->left=(p+i)->bt-(c_time-p_time);
c_time=c_time+(p+pro_num)->bt;
(p+pro_num)->status=1;
}
else
{
c_time=c_time+(p+i)->bt;
(p+i)->tt=c_time-(p+i)->at;
(p+i)->status=1;
if(c_time<(p+pro_num)->at)
break;
}
}
else
{
c_time=c_time+(p+pro_num)->bt;
}
(p+pro_num)->tt=c_time-(p+pro_num)->at;
(p+pro_num)->status=1;
}
if((p+i)->status==0)
{
c_time=c_time+(p+i)->left;
(p+i)->tt=c_time-(p+i)->at;
(p+i)->status=1;
}
}
else
{
c_time=c_time+(p+i)->bt;
(p+i)->tt=c_time-(p+i)->at;
(p+i)->status=1;
}
}
}
printf("\nProcess\t\tArrival Time\t\tCPU Burst Time\t\tTurnAround Time\t\tWaiting
Time\n");
for(i=0;i<n;i++)
{
(p+i)->wt=(p+i)->tt-(p+i)->bt;
awt=awt+(p+i)->wt;
att=att+(p+i)->tt;
}
for(i=0;i<n;i++)
{
printf("%d\t\t%d\t\t\t%d\t\t\t%d\t\t\t%d\n",(p+i)->name,(p+i)->at,(p+i)->bt,
(p+i)->tt,(p+i)->wt);
}
printf("\nAverage Turnaround Time : %f\n",att/n);
printf("Average Waiting Time : %f\n\n",awt/n);
return 0;
}

//SAMPLE OUTPUT
/*Enter no. of processes- 4
---Enter Process Name, Arrival Time & CPU Burst Time---
1 0 8
2 1 4
3 2 9
4 3 5

Process Arrival Time CPU Burst Time TurnAround Time Waiting Time
1 0 8 17 9
2 1 4 4 0
3 2 9 24 15
4 3 5 7 2

Average Turnaround Time : 13.000000


Average Waiting Time : 6.500000
*/

Written 24 Aug 2015

Pooja Khatri, MCA

707 Views

#include<stdio.h>

int main()

int at[10],bt[10],rt[10],endTime,i,smallest;

int remain=0,n,time,sum_wait=0,sum_turnaround=0;

printf("Enter no of Processes : ");

scanf("%d",&n);

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

{
printf("Enter arrival time for Process P%d : ",i+1);

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

printf("Enter burst time for Process P%d : ",i+1);

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

rt[i]=bt[i];

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

rt[9]=9999;

for(time=0;remain!=n;time++)

smallest=9;

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

if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)

smallest=i;

rt[smallest]--;

if(rt[smallest]==0)

remain++;

endTime=time+1;
printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-bt[smallest]-
at[smallest]);

sum_wait+=endTime-bt[smallest]-at[smallest];

sum_turnaround+=endTime-at[smallest];

printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);

printf("Average Turnaround time = %f",sum_turnaround*1.0/5);

return 0;

I hope this program will help you.

Written 16 Mar

Yogesh Datir, Student of Algorithms Design

887 Views

#include<stdio.h>

int main()

int at[10],bt[10],rt[10],endTime,i,smallest;

int remain=0,n,time,sum_wait=0,sum_turnaround=0;

printf("Enter no of Processes : ");

scanf("%d",&n);

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

printf("Enter arrival time for Process P%d : ",i+1);


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

printf("Enter burst time for Process P%d : ",i+1);

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

rt[i]=bt[i];

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

rt[9]=9999;

for(time=0;remain!=n;time++)

smallest=9;

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

if(at[i]<=time && rt[i]<rt[smallest] && rt[i]>0)

smallest=i;

rt[smallest]--;

if(rt[smallest]==0)

remain++;

endTime=time+1;

printf("\nP[%d]\t|\t%d\t|\t%d",smallest+1,endTime-at[smallest],endTime-
bt[smallest]-at[smallest]);

sum_wait+=endTime-bt[smallest]-at[smallest];

sum_turnaround+=endTime-at[smallest];
}

printf("\n\nAverage waiting time = %f\n",sum_wait*1.0/n);

printf("Average Turnaround time = %f",sum_turnaround*1.0/5);

return 0;

Written 21 Feb View Upvotes

Related Questions

How do I write a C++ program to implement a SRTF (Shortest Remaining


Time First) scheduling algorithm, along with displaying the Gantt chart?

What C program can help me simulate the shortest job first scheduling
without arrival times to calculate the average waiting time?

Is the following Gantt diagram correct for a preemptive Shortest Job First
algorithm (SJF)?

Is the following Gantt diagram correct for a non-preemptive Shortest Job


First algorithm?

How do I write a C program to implement OkCupid's matching algorithm?

How do you implement a C program for preemptive priority scheduling


with quantum or time slice?

What are the best Gantt chart plugins available for project schedule
display and management in a web project?

How much time does a person take while writing code for the
implementation of algorithms for the first time?

What is the solution for the starvation problem in the shortest job first-
scheduling algorithm?

How can I implement the shortest distance algorithm in C# (.net).

What is the time complexity of this implementation of Dijkstra's shortest


path algorithm?

What is the shortest C program you can come up with for implementing
Sieve of Eratosthenes ?

Can someone give me the C++ implementation of the cycle cancelling


algorithm and successive shortest path algorithm?
What is the idiom style for functional programming languages to
implement graph-related algorithm (like bfs, dfs, shortest path etc)?

What are Gantt Charts?

S-ar putea să vă placă și