Skip to main content

Shortest Remaining Time First (SRTF) Program in C++ || dot clu

Shortest Remaining Time First (SRTF) Program in C++ || dot clu

Shortest Remaining Time First (SRTF)

Shortest Remaining Time First (SRTF) Algorithm is preemptive version of Shortest Job First Algorithm. In this current process is executed until it is completed or a new process is added having lower burst time compare to the the remaining time for current process.
SRTF algorithm may lead to starvation, if processes with lower burst time continues to add to cpu scheduler then the current process will never get a chance to get executed.

For example consider the following table
ProcessArrival TimeBurst Time
P1010
P216
P329
P434
At time t=0, Process P1 will start get executing as it is only the process present at that time. Then at t=1, Process P2 added to the CPU scheduler, at this time remaining time(Burst time) for Process P1 gets 9, as Burst time of P2 is less than the remaining time of other processes (for now there is only process P1) therefore process P1 is preempted and P2 is alloted the CPU.
This process is repeated until all process get executed. This is known as Shortest Remaining Time First Algorithm. Now lets see how to implement it in a computer program.
SOURCE CODE:
  1. # include<stdio.h>
  2. # include<conio.h>
  3. #include<iostream>

  4. # define max1 10      //MAX PROCESSES
  5. # define r 6          //MAX RESOURCES

  6. using namespace std;

  7. void sort(int st,int a[],int b[],int pr[],int np)
  8. {       int i,j,t,p,z;
  9. for(i=0;i<np-1;i++)
  10. {
  11. for(j=st;j<np-i-1;j++)
  12. {
  13. if(a[j]>a[j+1])
  14. {
  15.     t=a[j];      z=b[j];        p=pr[j];
  16.     a[j]=a[j+1]; b[j]=b[j+1]; pr[j]=pr[j+1];
  17.     a[j+1]=t; b[j+1]=z; pr[j+1]=p;
  18. }
  19. }
  20. }
  21. }
  22. /* SHEDULING ALGO-SHORTEST REMAINING TIME FIRST-PREEMTIVE */

  23. void srtf()
  24. {
  25. int p1[max1],t1[max1],i,ch,np;
  26. int arr[max1];
  27. int tat[max1];
  28. int wt[max1];
  29. int resp[max1];
  30. float rdelay[max1];
  31. float atat;
  32. float awt;
  33. float aresp;
  34. float ardelay;
  35. int pgant[max1];
  36. int tgant[max1];
  37. int tex=0;
  38. int j,k,count,c,a;
  39. int trem[max1];
  40. cout<<"\nEnter no. of processes  : ";
  41. cin>>np;
  42. for(i=0;i<np;i++)
  43. {
  44. cout<<"\nEnter the Process  :- ",i;
  45. p1[i]=i;
  46. cout<<"\nExecution time : ";
  47. cin>>t1[i];
  48. cout<<"\nArrival time   : ";
  49. cin>>arr[i];
  50. }
  51. cout<<"\nPROCESSES\t\tEXECUTION TIME\t\tARRIVAL TIME";
  52. for(i=0;i<np;i++)
  53. {
  54. cout<<"\n p "<<p1[i]<<"  \t\t\t"<<t1[i]<<"  \t\t\t"<<arr[i];
  55. }
  56. for(i=0;i<np;i++)
  57. { tex=tex+t1[i];
  58. trem[i]=t1[i];
  59. }
  60. sort(0,arr,p1,t1,np);
  61. tgant[0]=0;
  62. j=0,k=0,count=0,c=0;
  63. while(count!=tex)
  64. {
  65. if(j!=np)
  66. {       j=k;
  67. for(i=k;i<np;i++)
  68. {
  69. if(arr[i]<=count)
  70. j++;
  71. else
  72. break;
  73. }
  74. if((j-k)>1)
  75. sort(k,trem,p1,arr,j);
  76. }
  77. while(trem[k]==0)
  78. k++;
  79. c++;
  80. pgant[c-1]=p1[k];
  81. if(j==np)
  82. a=trem[k];
  83. else
  84. { if(trem[k] < (arr[j]-tgant[c-1]) )
  85. a=trem[k];
  86. else
  87. a=arr[j]-tgant[c-1];
  88. }
  89. tgant[c]=tgant[c-1]+a;
  90. count=tgant[c];
  91. trem[k]=trem[k]-(tgant[c]-tgant[c-1]);
  92. if(c>1)
  93. {
  94. if(pgant[c-1]==pgant[c-2])
  95. { tgant[c-1]=tgant[c];
  96. c--;
  97. }
  98. }
  99. }
  100. sort(0,arr,p1,trem,np);
  101. for(i=0;i<np;i++)
  102. {
  103. wt[i]=0;
  104. tat[i]=0;
  105. resp[i]=-1;
  106. }
  107. for(i=0;i<c;i++)
  108. {
  109. for(j=0;j<np;j++)
  110. {
  111. if(pgant[i]==p1[j])
  112. {
  113. wt[j]=wt[j]+tgant[i]-tat[j];
  114. if(resp[j]==-1)
  115. resp[j]=tgant[i];
  116. tat[j]=tgant[i+1] ;
  117. break;
  118. }
  119. }
  120. }
  121. for(i=0;i<np;i++)
  122. {
  123. wt[i]=wt[i]-arr[i];
  124. tat[i]=tat[i]-arr[i];
  125. resp[i]=resp[i]-arr[i];
  126. rdelay[i]=(float)tat[i]/t1[i];
  127. }
  128. atat=0; awt=0; aresp=0; ardelay=0;
  129. for(i=0;i<np;i++)
  130. {
  131. atat=atat+tat[i];
  132. awt=awt+wt[i];
  133. aresp=aresp+resp[i];
  134. ardelay=ardelay+rdelay[i];
  135. }
  136. atat=atat/np;
  137. awt=awt/np;
  138. aresp=aresp/np;
  139. ardelay=ardelay/np;

  140. cout<<"\nGANTT CHART:- ";
  141. for(i=0;i<c;i++)
  142. cout<<"   P"<<pgant[i]<<"  ";

  143. for(i=0;i<=c;i++)
  144. cout<<"    "<<tgant[i];

  145. cout<<"\n\nAverage waiting time      : "<<awt;
  146. cout<<"\nAverage turn arround time : "<<atat;
  147. cout<<"\nAverage response time     : "<<aresp;
  148. cout<<"\nAverage relative delay    : "<<ardelay;
  149. getch();
  150. }

  151. int main()
  152. {
  153.       srtf();
  154.      getch();
  155. }

Comments

  1. Could you explain the code with comments?

    ReplyDelete
  2. hey...Please explain code with comments.
    Its difficult to understand variables used in this code.

    ReplyDelete

Post a Comment

Popular posts from this blog

First Fit Program and Algorithm in C++ || dot clu

First Fit Program and Algorithm in C++ || dot clu First Fit Algorithm in C and C++ Here you will learn about first fit algorithm in C and C++ with program examples. There are various memory management schemes in operating system like first fit, best fit and worst fit. In this section we will talk about first fit scheme. Suggestion: 1. First Fit 2.  Best Fit 3.  Worst Fit What is First Fit Memory Management Scheme? In this scheme we check the blocks in a sequential manner which means we pick the first process then compare it’s size with first block size if it is less than size of block it is allocated otherwise we move to second block and so on. First Fit Algorithm Get no. of Processes and no. of blocks. After that get the size of each block and process requests. Now allocate processes if(block size >= process size) //allocate the process else //move on to next block Display the processes with the blocks that are allocated to a respective...

C++ Program For PRIORITY WITH NON - PREEMPTIVE Scheduling Algorithm || dot clu

C++ Program For PRIORITY WITH  NON - PREEMPTIVE Scheduling Algorithm It is important to distinguish  preemptive  from  non - preemptive scheduling algorithms.  Preemption  means the operating system moves a process from running to ready without the process requesting it. Without  preemption , the system implements ``run to completion (or yield or block)''. Non-Preemptive Scheduling Non-Preemptive Scheduling means once a process starts its execution or the CPU is processing a specific process it cannot be halted or in other words we cannot preempt (take control) the CPU to some other process. A computer system implementing this cannot support the execution of process in a multi task fashion. It executes all the processes in a sequential manner. It is not practical as all processes are not of same priority and are not always known to the system in advance. Source Code: #include <iostream> using namespace std; int main(...