Write a C program to implement the various process scheduling mechanisms such as Round Robin Scheduling.

Description of Round Robin Scheduling:

Scheduling is a fundamental operating system function. CPU scheduling is the basis of the multi-programming operating system. CPU scheduling algorithm determines how the CPU will be allocated to the process. These are of two types.

1. Primitive scheduling algorithms: In this, the CPU can release the process even in the middle of execution. For example, the cpu executes the process p1, in the middle of execution the cpu received a request signal from process p2, then the OS compares the priorities of p1&p2. If the priority p1 is higher than the p2 then the CPU continues the execution of process p1.Otherwise, the cpu preempt the process p1 and assigned to process p2.

2) Non-Primitive Scheduling algorithm: In this, once the cpu assigned to a process the processor does not release until the completion of that process. The cpu will assign some other job only after the previous job has finished.

Round Robin Scheduling Scheduling methodology: 

Though put: It means how many jobs are completed by the CPU within a time period.
Turn around time: The time interval between the submission of the process and the time of the completion is the turn around time.
Turn around time=Finished time – arrival time
Waiting time: it is the sum of the periods spent waiting by a process in the ready queue
Waiting time=Starting time- arrival time
Response time: it is the time duration between the submission and first response
Response time=First response-arrival time

CPU Utilization: This is the percentage of time that the processor is busy. CPU utilization may range from 0 to 100%.


Round Robin Scheduling: It is a primitive scheduling algorithm it is designed especially for time-sharing systems. In this, the CPU switches between the processes. When the time quantum expired, the CPU switches to another job. A small unit of time called a quantum or time slice. A time quantum is generally is a circular queue new processes are added to the tail of the ready queue.

If the process may have a CPU burst of less than one-time slice then the process release the CPU voluntarily. The scheduler will then process to next process ready queue otherwise; the process will be put at the tail of the ready queue.



Algorithm for Round Robin:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process(n) = burst time process(n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of process(n-1 ) + the time difference in getting the CPU from process(n-1)
(b) Turn around time for process(n) = waiting time of process(n) + burst time of process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process

/* Program to Simulate Round Robin CPU Scheduling Algorithm */

Program Code:
#include<stdio.h>
#include<conio.h>
struct process
{
char pn[10];
int bt,ct,time;
}p[10];
void main()
{
int i,full,n,tq,wt[10],tat[10], time1=0;
float avgwt=0.0;
clrscr();
printf("Enter number of processes:");
scanf("%d",&n);
printf("Enter process name and burst time of %d process\n", n);
for(i=0;i<n;i++)
{
scanf("%s%d",&p[i].pn,&p[i].bt);
p[i].time=p[i].bt;
}
printf("Enter quantum:");
scanf("%d",&tq);
full=n;
while(full)
{
for(i=0;i<n;i++)
{
if(p[i].bt>=tq)
{
p[i].bt-=tq;
time1=time1+tq;
}
else if(p[i].bt!=0)
{
time1+=p[i].bt;
p[i].bt=0;
}
else
continue;
if(p[i].bt==0)
{
full=full-1;
tat[i]=time1;
}
}
}
for(i=0;i<n;i++)
{
p[i].ct=tat[i];
wt[i]=tat[i]-p[i].time;
}
printf("----------------------------------\n");
printf("PN\tBt\tCt\tTat\tWt\n");
printf("----------------------------------\n");
for(i=0;i<n;i++)
{
printf("%2s\t%2d\t%2d\t%2d\t%2d\n",p[i].pn,p[i].time,p[i].ct,tat[i],wt[i]);
avgwt+=wt[i];
}
printf("----------------------------------\n");
avgwt=avgwt/n;
printf(" Average waiting time = %.2f\n",avgwt);
printf("-----------------------------------\n");
getch();
}

Program Output:
Enter number of processes: 5
Enter process name and burst time of 5 process
1 10
2 5
3 15
4 3
5 20
Enter quantum:5
--------------------------------------
PN Bt Ct Tat Wt
--------------------------------------
1 10 28 28 18
2 5 10 10 5
3 15 43 43 28
4 3 18 18 15
5 20 53 53 33
--------------------------------------
Average waiting time = 19.79
--------------------------------------
Next
Newer Post
Previous
This is the last post.
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: