Sunday, 12 May 2013

3. ROUND ROBIN SCHEDULING IN JAVA



Code:
import java.util.*;
class RR
{
            Scanner sc=new Scanner(System.in);
            int[] bur,rem,wai,ta;
            int size,q,b=0,t=0,flag=0;
            RR(int size)
            {
                        this.size=size;
                        bur=new int[size];
                        wai=new int[size];
                        ta=new int[size];
                        rem=new int[size];
            }
            void get()
            {
                        for(int i=0;i<size;i++)
                        {
                                    System.out.print("Enter burst time of P"+(i+1)+":");
                                    bur[i]=rem[i]=sc.nextInt();
                        }
                        System.out.print("Enter quantum time:");
                        q=sc.nextInt();
            }
            void round()
            {
                        do{
                                    flag=0;
                                    for(int i=0;i<size;i++)
                                    {
                                                if(rem[i]>=q)
                                                {
                                                            System.out.print("P"+(i+1)+"\t");
                                                            for(int j=0;j<size;j++)
                                                            {
                                                                        if(j==i)
                                                                                    rem[i]=rem[i]-q;
                                                                        else if(rem[j]>0)
                                                                                    wai[j]+=q;
                                                            }
                                                }
                                                else if(rem[i]>0)
                                                {
                                                            System.out.print("P"+(i+1)+"\t");
                                                            for(int j=0;j<size;j++)
                                                            {
                                                                        if(j==i)
                                                                                    rem[i]=0;
                                                                        else if(rem[j]>0)
                                                                                    wai[j]+=rem[i];
                                                            }
                                                }
                                    }
                                    for(int i=0;i<size;i++)
                                                if(rem[i]>0)
                                                            flag=1;
                        }while(flag==1);
                        for(int i=0;i<size;i++)
                                    ta[i]=wai[i]+bur[i];
            }
            void display()
            {
                        System.out.println("\nProcess\tBurst\tWaiting\tTurnaround");
                        for(int i=0;i<size;i++)
                        {
                                    System.out.println("P"+(i+1)+"\t"+bur[i]+"\t"+wai[i]+"\t"+ta[i]);
                                    b+=wai[i];
                                    t+=ta[i];
                        }
                        System.out.println("Average waiting time:"+(b/size));
                        System.out.println("Average Turnaround time:"+(t/size));
            }
}
class KRR
{
            public static void main(String arg[])
            {
                        Scanner s=new Scanner(System.in);
                        System.out.print("Enter the no of process:");
                        int n=s.nextInt();
                        RR obj = new RR(n);
                        obj.get();
                        obj.round();
                        obj.display();
            }
}
OUTPUT
Enter the no of process:3
Enter burst time of P1:13
Enter burst time of P2:8
Enter burst time of P3:3
Enter quantum time:4
P1      P2      P3      P1      P2      P1      P1
Process Burst   Waiting Turnaround
P1      13      11      24
P2      8       11      19
P3      3       8       11
Average waiting time:10
Average Turnaround time:18                  




No comments:

Post a Comment