Showing posts with label Round Robin(RR) algorithm. Show all posts
Showing posts with label Round Robin(RR) algorithm. Show all posts

Saturday, 6 February 2016

CPU Scheduling Algorithm - Round Robin(Java)


This is a Java program which implements one of the CPU Scheduling algorithm called Round Robin(RR).

Round robin algorithm is mainly used in time sharing systems , it is also similar to First Come First Served(FCFS) algorithm but FCFS does not have that time slicing switch.

All the jobs gets executed in this scheduling algorithm , so the advantage here is , its Starvation free.




Download Code

PROGRAM :

package mycodingcorner;

import java.util.Scanner;

public class RoundRobin {
 public static void main(String[] args) {
  int i, j, n, r, q, e = 0;
  int bt_c[] = new int[10];
  int bt[] = new int[10];
  int m[] = new int[50];
  float f, avg = 0;
  Scanner scan = new Scanner(System.in);
  System.out.print("\nEnter how many jobs ?\t");
  n = scan.nextInt();
  for (i = 1; i <= n; i++) {
   System.out.print("Enter burst time for job " + i + " :\t");
   bt[i] = scan.nextInt();
   bt_c[i] = bt[i]; // stores job has how much burst time in array i
  }
  System.out.print("\nEnter Quantum (time slice value) :\t");
  q = scan.nextInt();
  int max = 0;
  max = bt[1];
  for (j = 1; j <= n; j++)
   if (max <= bt[j])
    max = bt[j];

  if ((max % q) == 0)
   r = (max / q);
  else
   r = (max / q) + 1;
  for (i = 1; i <= r; i++) {
   System.out.print("\n\nRound" + i);
   for (j = 1; j <= n; j++) {
    if (bt[j] > 0) {
     bt[j] = bt[j] - q;

     if (bt[j] <= 0) {
      bt[j] = 0;
      System.out.print("\njob " + j + " is completed");
     } else
      System.out.print("\njob" + j + " remaining time is " + bt[j]);
    }
   }

  }
  for (i = 1; i <= n; i++) {
   e = 0;
   for (j = 1; j <= r; j++) {
    if (bt_c[i] != 0) {
     if (bt_c[i] >= q) {
      m[i + e] = q;
      bt_c[i] -= q;
     } else {
      m[i + e] = bt_c[i];
      bt_c[i] = 0;
     }
    } else
     m[i + e] = 0;
    e = e + n;
   }
  }
  for (i = 2; i <= n; i++)
   for (j = 1; j <= i - 1; j++)
    avg = avg + m[j];
  for (i = n + 1; i <= r * n; i++) {
   if (m[i] != 0) {
    for (j = i - (n - 1); j <= i - 1; j++)
     avg = m[j] + avg;
   }
  }
  f = avg / n;
  System.out.print("\n\n\nTOTAL WATING TIME: \t" + avg);
  System.out.print("\n\nAVERAGE WAITING TIME: \t\n" + f);
  scan.close();
 }
}
Download Code

OUTPUT :

Java - Cpu Scheduling Algorithm - Round Robin(RR)


Friday, 5 February 2016

CPU Scheduling Algorithm - Round Robin(C)


This is a C program which implements one of the CPU Scheduling algorithm called Round Robin(RR).

Round robin algorithm is mainly used in time sharing systems , it is also similar to First Come First Served(FCFS) algorithm but FCFS does not have that time slicing switch.

All the jobs gets executed in this scheduling algorithm , so the advantage here is , its Starvation free.




Download Code

PROGRAM :

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int bt_c[10],bt[10],i,j,n,m[50],r,q,e=0;
    float f,avg=0;
    printf("\nEnter how many jobs ?\t");
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        printf("Enter burst time for job %d :\t",i);
        scanf("%d",&bt[i]);
        bt_c[i]=bt[i]; //stores job has how much burst  time in array  i
    }
    printf("\nEnter Quantum (time slice value) :\t");
    scanf("%d",&q);
    int max=0;
    max=bt[1];
    for(j=1; j<=n; j++)
        if(max<=bt[j])
            max=bt[j];

    if((max%q)==0)
        r=(max/q);
    else
        r=(max/q)+1;
    for(i=1; i<=r; i++)
    {
        printf("\n\nRound %d",i);
        for(j=1; j<=n; j++)
        {
            if(bt[j]>0)
            {
                bt[j]=bt[j]-q;

                if(bt[j]<=0)
                {
                    bt[j]=0;
                    printf("\njob %d is completed",j);
                }
                else
                    printf("\njob %d remaining time is %d",j,bt[j]);
            }
        }

    }
    for(i=1; i<=n; i++)
    {
        e=0;
        for(j=1; j<=r; j++)
        {
            if(bt_c[i]!=0)
            {
                if(bt_c[i]>=q)
                {
                    m[i+e]=q;
                    bt_c[i]-=q;
                }
                else
                {
                    m[i+e]=bt_c[i];
                    bt_c[i]=0;
                }
            }
            else
                m[i+e]=0;
            e=e+n;
        }
    }
    for(i=2; i<=n; i++)
        for(j=1; j<=i-1; j++)
            avg=avg+m[j];
    for(i=n+1; i<=r*n; i++)
    {
        if(m[i]!=0)
        {
            for(j=i-(n-1); j<=i-1; j++)
                avg=m[j]+avg;
        }
    }
    f=avg/n;
    printf("\n\n\nTOTAL WATING TIME:%f\t",avg);
    printf("\n\nAVERAGE WAITING TIME:%f\t\n",f);
    return 0;
}
Download Code

OUTPUT : 

C Program - CPU Scheduling Algorithm : Round Robin