OS34
OS34
Ayush 37 20272195
3.Round Robin(Preemptive):
Algorithm of Round Robin:
Step 1: Organize all processes according to their arrival time in the ready queue.
The queue structure of the ready queue is based on the FIFO structure to execute
all CPU processes.
Step 2: Now, we push the first process from the ready queue to execute its task
for a fixed time, allocated by each process that arrives in the queue.
Step 3: If the process cannot complete their task within defined time interval or
slots because it is stopped by another process that pushes from the ready queue
to execute their task due to arrival time of the next process is reached. Therefore,
CPU saved the previous state of the process, which helps to resume from the point
where it is interrupted. (If the burst time of the process is left, push the process
end of the ready queue).
Step 4: Similarly, the scheduler selects another process from the ready queue to
execute its tasks. When a process finishes its task within time slots, the process
will not go for further execution because the process's burst time is finished.
Step 5: Similarly, we repeat all the steps to execute the process until the work has
finished.
Source code:
#include<iostream>
#include<iostream>
using namespace std;
void findWaitingTime(int processes[], int n, int bt[], int wt[], int quantum)
{ int rem_bt[n];
for (int i = 0 ; i < n ; i++)
rem_bt[i] = bt[i];
Ayush 38 20272195
int t = 0;
while (1)
{ bool done = true;
for (int i = 0 ; i < n; i++)
{ if (rem_bt[i] > 0)
{ done = false;
if (rem_bt[i] > quantum)
{ t += quantum;
rem_bt[i] -= quantum; }
else
{ t = t + rem_bt[i];
wt[i] = t - bt[i];
rem_bt[i] = 0; }
}
}
if (done == true)
break; }
}
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 quantum)
{ int wt[n], tat[n], total_wt = 0, total_tat = 0;
findWaitingTime(processes, n, bt, wt, quantum);
findTurnAroundTime(processes, n, bt, wt, tat);
cout << "Processes "<< " Burst time "
Ayush 39 20272195
<< " Waiting time " << " Turn around time\n";
for (int i=0; i<n; i++)
{ total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i+1 << "\t\t" << bt[i] <<"\t "<< wt[i] <<"\t\t "
<<tat[i]<<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[] = {10, 5, 8};
int quantum = 2;
cout<<"Quantum="<<quantum<<"\n";
findavgTime(processes, n, burst_time, quantum);
return 0;
}
Ayush 40 20272195
Output:
Ayush 41 20272195
3. Priority:
1. First input the processes with their arrival time, burst time and priority.
2. First process will schedule, which have the lowest arrival time, if two or
more processes will have lowest arrival time, then whoever has higher priority
will schedule first.
3. Now further processes will be schedule according to the arrival time and
priority of the process. (Here we are assuming that lower the priority number
having higher priority). If two process priority are same then sort according to
process number.
4. Once all the processes have been arrived, we can schedule them based on
their priority.
Source Code:
#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;
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
Ayush 43 20272195
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
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;
total=0;
Ayush 44 20272195
total+=tat[i];
cout<<"\nP["<<p[i]<<"]\t\t"<<bt[i]<<"\t\t\t\t"<<wt[i]<<"\t\t\t\t"<<tat[i];
}
avg_tat=total/n;
cout<<"\n\nAverage Waiting Time="<<avg_wt;
cout<<"\nAverage Turnaround Time="<<avg_tat;
return 0;
}
Output:
Ayush 45 20272195