HI....to all members of daniweb...I am looking for code of RR Shceduling using JAVA... I am grateful if somebody would like to help me to it...and tanks "nalang daan"!!! from PHillippines

Dani AI

Generated

originally asked for a Java Round‑Robin scheduler. posted a C example and reminded that threads should be consolidated. The snippet below is a compact, self‑contained Java implementation that follows a FIFO ready queue, accepts unsorted arrivals (it sorts them), and calculates completion, waiting and turnaround times. It is written to be easy to adapt to file or console input.

Round‑Robin essentials to watch for: keep a ready queue and add processes as their arrival times occur; when the queue is empty advance the clock to the next arrival; after running a process for the quantum requeue it if unfinished; compute waiting time as completion - arrival - burst. Common bugs are failing to add newly arrived processes after a time slice and not advancing time when the CPU is idle.

import java.util.*;

public class RoundRobin {
    static class Process {
        int id, arrival, burst, remaining, completion, waiting, turnaround;
        Process(int id, int arrival, int burst) {
            this.id = id; this.arrival = arrival; this.burst = burst; this.remaining = burst;
        }
    }

    public static void roundRobin(List<Process> procs, int quantum) {
        Collections.sort(procs, Comparator.comparingInt(p -> p.arrival));
        Queue<Process> q = new LinkedList<>();
        int time = 0, index = 0;
        List<Process> finished = new ArrayList<>();

        while (index < procs.size() || !q.isEmpty()) {
            while (index < procs.size() && procs.get(index).arrival <= time) q.add(procs.get(index++));
            if (q.isEmpty()) { time = procs.get(index).arrival; continue; }

            Process p = q.poll();
            int run = Math.min(quantum, p.remaining);
            p.remaining -= run; time += run;
            while (index < procs.size() && procs.get(index).arrival <= time) q.add(procs.get(index++));
            if (p.remaining > 0) q.add(p);
            else {
                p.completion = time;
                p.turnaround = p.completion - p.arrival;
                p.waiting = p.turnaround - p.burst;
                finished.add(p);
            }
        }

        System.out.printf("%3s %7s %6s %11s %8s %11s\n","ID","Arrival","Burst","Completion","Waiting","Turnaround");
        double totalW = 0, totalT = 0;
        for (Process p : finished) {
            System.out.printf("%3d %7d %6d %11d %8d %11d\n", p.id, p.arrival, p.burst, p.completion, p.waiting, p.turnaround);
            totalW += p.waiting; totalT += p.turnaround;
        }
        System.out.printf("Average waiting=%.2f, turnaround=%.2f\n", totalW/finished.size(), totalT/finished.size());
    }

    public static void main(String[] args) {
        List<Process> procs = Arrays.asList(
            new Process(1, 0, 10),
            new Process(2, 2, 4),
            new Process(3, 4, 6),
            new Process(4, 6, 8)
        );
        roundRobin(new ArrayList<>(procs), 3);
    }
}

Notes: the example uses integer time units and omits context‑switch overhead; to model that add a fixed overhead when switching. For fractional times or very large bursts use doubles or longs. The core logic here addresses the typical pitfalls not handled by simple examples that assume sorted arrivals.

Recommended Answers

All 2 Replies

//This code is in C but you can convert it easily

#include<stdio.h>
#include <unistd.h>

#define k 4

main()
{
  int numElements,nu=0,i=0,j;
  int cpu[k],arrival[k],start=0,finish[k],cpu1[k],turn[k],arrival1[k],t,me;
  int m = 0,wait [k]={0,0,0,0},y;

  printf("Please enter the CPU Cycles:\n");//println
  getnumbers(cpu);

  printf("Please enter the Arrival Times:\n");
  getnumbers(arrival);

  copyelt(cpu,cpu1);
  copyelt(arrival,arrival1);
  for(i=0;i<k;i++){

  }

  i = 0;

  while(m==0){
    if(cpu[i] >= 20)
      {cpu[i]=cpu[i]-20;
      wait[i]+=start-arrival[i];
      start = start+20;
      arrival[i]=start;
      }
    else
      if(cpu[i]!=0)
    { wait[i] += start-arrival[i];
    start = start + cpu[i];
    arrival[i]=start;
    cpu[i]=0;}
    j = (i+1)%k;
    if(arrival[i] < arrival1[j] && cpu[i] != 0)
      i = i%k;
    else
        i = j;

    m = search(cpu);
  }

  for(i=0;i<k;i++){

    turn[i] = arrival[i]-arrival1[i];

    nu = wait[i] + nu;
  }
  float p = (nu/k); 
  displayresult(wait,turn,cpu1,arrival1);

}

getnumbers(int num[k]){
  int n,i;
  n=k;

  for(i=0;i<k;i++){
    scanf("%d",&num[i]);
  }
}

int search(int num[]){
  int y = 0 ;
  int i,l=0;
  for(i=0;i<k;i++)
    {if(num[i] == 0)
      { l++;
      }
    }
  if(l==k)
    y = 1;
  return y;
}

displayresult(int wat[],int tur[],int cp[],int pk[]){
  int i;
  printf("\nProcesses          CPU Time     Arrival Time             Wait              Turnaround Time\n");
  printf("---------------------------------------------------------------------------------------------\n");
  for(i=0;i<k;i++){
    printf("%d                  %d                %d                    % d                   %d\n",i,cp[i],pk[i],wat[i],tur[i]);
  }
  printf("-----------------------------------------------------------------------------------------------------\n");
} 

copyelt(int me[],int my[]){
  int i;
  for(i = 0;i<k;i++)
    {my[i] = me[i];
    }
  return my;
}
//This code ask for ten CPU cycles and arrival times from the Keyboard after that it will computes the wait ,turnaround and finish times for the input.
//But the assumption is that the arrival times are in the ascending order.

Stay with the original thread http://www.daniweb.com/forums/thread92130.html

Don't start a new thread simply because you don't like the answers you've been getting. If you feel the need to "draw attention" to the thread, or "move it to the top of the list" then post a quick reply to it, or edit an entry, or something to that effect. Don't just simply start a new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.