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

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.