CPU scheduling treats with the issues of deciding which of the processes in the ready queue needs to be allocated to the CPU. There are several different CPU scheduling algorithms used nowadays within an operating system.
In this post, we will discuss the Round Robin Process Scheduling algorithm and also write a program for the Round Robin Process Scheduling algorithm. 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 favored over other processes. The Round Robin algorithm is a pre-emptive algorithm as the scheduler forces the process out of the CPU once the time quota expires.
We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of the Round Robin algorithm in C++, although, it’s very similar to C.
INPUT:
The first line is the number of processes(nop).
The second line contains the time quantum(qt).
The next n lines contain three variables: process name(pname), arrival time(atime) and burst time(btime).
OUTPUT:
Print the matrix for process name, arrival time, burst time, completion time, turn around time, waiting time and response time.
Also, print the Gantt chart.
The following is the SJF Process Scheduling program in C++.
#include<iostream> #include<algorithm> using namespace std; struct node{ char pname; int btime; int atime; int restime=0; int ctime=0; int wtime=-1; }a[100],b[100],c[100]; void insert(int n){ int i; for(i=0;i<n;i++){ cin>>a[i].pname; cin>>a[i].atime; cin>>a[i].btime; a[i].wtime=-a[i].atime; } } bool btimeSort(node a,node b){ return a.btime < b.btime; } bool atimeSort(node a,node b){ return a.atime < b.atime; } int k=0,f=0,r=0; void disp(int nop,int qt){ int n=nop,q; sort(a,a+n,atimeSort); int ttime=0,i; int j,tArray[n]; int alltime=0; bool moveLast=false; for(i=0;i<n;i++){ alltime+=a[i].btime; } alltime+=a[0].atime; for(i=0;ttime<=alltime;){ j=i; while(a[j].atime<=ttime&&j!=n){ b[r]=a[j]; j++; r++; } if(r==f){ c[k].pname='i'; c[k].btime=a[j].atime-ttime; c[k].atime=ttime; ttime+=c[k].btime; k++; continue; } i=j; if(moveLast==true){ b[r]=b[f]; f++; r++; } j=f; if(b[j].btime>qt){ c[k]=b[j]; c[k].btime=qt; k++; b[j].btime=b[j].btime-qt; ttime+=qt; moveLast=true; for(q=0;q<n;q++){ if(b[j].pname!=a[q].pname){ a[q].wtime+=qt; } } } else{ c[k]=b[j]; k++; f++; ttime+=b[j].btime; moveLast=false; for(q=0;q<n;q++){ if(b[j].pname!=a[q].pname){ a[q].wtime+=b[j].btime; } } } if(f==r&&i>=n) break; } tArray[i]=ttime; ttime+=a[i].btime; int rtime=0; for(j=0;j<n&&j<6;j++){ rtime=0; for(i=0;i<k;i++){ if(c[i].pname==a[j].pname){ a[j].restime=rtime; break; } rtime+=c[i].btime; } } float averageWaitingTime=0; float averageResponseTime=0; float averageTAT=0; cout<<"\nGantt Chart\n"; rtime=0; for (i=0; i<k+1&&i<20; i++){ if(i!=k) cout<<"| "<< c[i].pname << " "; rtime+=c[i].btime; for(j=0;j<6;j++){ if(a[j].pname==c[i].pname) a[j].ctime=rtime; } } cout<<"\n"; rtime=0; for (i=0; i<k+1&&i<20; i++){ cout << rtime << "\t"; tArray[i]=rtime; rtime+=c[i].btime; } cout<<"\n"; cout<<"\n"; cout<<"P.Name AT\tBT\tCT\tTAT\tWT\tRT\n"; for (i=0; i<6&&i<nop&&a[i].pname!='i'; i++){ if(a[i].pname=='\0') break; cout << a[i].pname << "\t"; cout << a[i].atime << "\t"; cout << a[i].btime << "\t"; cout << a[i].ctime << "\t"; cout << a[i].wtime+a[i].ctime-rtime+a[i].btime << "\t"; averageTAT+=a[i].wtime+a[i].ctime-rtime+a[i].btime; cout << a[i].wtime+a[i].ctime-rtime << "\t"; averageWaitingTime+=a[i].wtime+a[i].ctime-rtime; cout << a[i].restime-a[i].atime << "\t"; averageResponseTime+=a[i].restime-a[i].atime; cout <<"\n"; } cout<<"Average Response time: "<<(float)averageResponseTime/(float)n<<endl; cout<<"Average Waiting time: "<<(float)averageWaitingTime/(float)n<<endl; cout<<"Average TA time: "<<(float)averageTAT/(float)n<<endl; } int main(){ int nop,choice,i,qt; cout<<"Enter number of processes\n"; cin>>nop; cout<<"Enter time quantum\n"; cin>>qt; insert(nop); disp(nop,qt); return 0; }
OUTPUT:
Enter number of processes 5 Enter time quantum 2 1 0 5 2 1 2 3 2 4 4 3 1 5 4 7 Gantt Chart | 1 | 2 | 3 | 1 | 4 | 5 | 3 | 1 | 5 | 5 | 5 0 2 4 6 8 9 11 13 14 16 18 19 P.Name AT BT CT TAT WT RT 1 0 5 14 14 9 0 2 1 2 4 3 1 1 3 2 4 13 11 7 2 4 3 1 9 6 5 5 5 4 7 19 15 8 5 Average Response time: 2.6 Average Waiting time: 6 Average TA time: 9.8
Other process scheduling algorithms
- SJF (Non-preemptive) Process Scheduling
- HRRN (Preemptive) Process Scheduling
- Round Robin Process Scheduling
- Priority Scheduling (preemptive)
- LRTF Process Scheduling
- SJF (preemptive) Process Scheduling
Let us know in the comments if you are having any questions regarding this Round Robin Processing Scheduling algorithm.
And if you found this post helpful, then please help us by sharing this post with your friends. Thank You