Round Robin Scheduling Algorithm > Java Program
Operating Systems
Round Robin Scheduling Algorithm > Java Program
Round robin Scheduling algorithm (RR) is designed especially for time sharing system.It is similar to FCFS scheduling,but preempted is added to switch between processes.A small unit of time called a time quantum is defined.a time quantum is generally from 10 to 100 milliseconds.The CPU scheduler goes around the ready queue ,allocating the CPU to each process for a time interval of up to 1 time quantum.
Input of Round robin Scheduling algorithm (RR) is:
n <-----Enter number of process:
bt[]<-----Enter brust Time:
q <-----Enter Time quantum:
Output of Round robin Scheduling algorithm (RR) is:
average wating time
Average turn around time Program:
import java.io.*;
class round
{
public static void main(String args[])throws IOException
{
DataInputStream in=new DataInputStream(System.in);
int i,j,k,q,sum=0;
System.out.println("Enter number of process:");
int n=Integer.parseInt(in.readLine());
int bt[]=new int[n];
int wt[]=new int[n];
int tat[]=new int[n];
int a[]=new int[n];
System.out.println("Enter brust Time:");
for(i=0;i<n;i++)
{
System.out.println("Enter brust Time for "+(i+1));
bt[i]=Integer.parseInt(in.readLine());
}
System.out.println("Enter Time quantum:");
q=Integer.parseInt(in.readLine());
for(i=0;i<n;i++)
a[i]=bt[i];
for(i=0;i<n;i++)
wt[i]=0;
do
{
for(i=0;i<n;i++)
{
if(bt[i]>q)
{
bt[i]-=q;
for(j=0;j<n;j++)
{
if((j!=i)&&(bt[j]!=0))
wt[j]+=q;
}
}
else
{
for(j=0;j<n;j++)
{
if((j!=i)&&(bt[j]!=0))
wt[j]+=bt[i];
}
bt[i]=0;
}
}
sum=0;
for(k=0;k<n;k++)
sum=sum+bt[k];
}
while(sum!=0);
for(i=0;i<n;i++)
tat[i]=wt[i]+a[i];
System.out.println("process\t\tBT\tWT\tTAT");
for(i=0;i<n;i++)
{
System.out.println("process"+(i+1)+"\t"+a[i]+"\t"+wt[i]+"\t"+tat[i]);
}
float avg_wt=0;
float avg_tat=0;
for(j=0;j<n;j++)
{
avg_wt+=wt[j];
}
for(j=0;j<n;j++)
{
avg_tat+=tat[j];
}
System.out.println("average wating time "+(avg_wt/n)+"\nAverage turn around time"+(avg_tat/n));
}
}
/*Round robin Scheduling algorithm output:
Enter number of process:
4
Enter brust Time:
Enter brust Time for 1
4
Enter brust Time for 2
5
Enter brust Time for 3
6
Enter brust Time for 4
7
Enter Time quantum: 4
process BT WT TAT
process1 4 0 4
process2 5 12 17
process3 6 13 19
process4 7 15 22
average wating time 10.0
Average turn around time15.5 */
Comments
Post a Comment