I need help in Round Robin Scheduling

please help me i dont know what's wrong with my code.

Page 1 of 1

7 Replies - 7674 Views - Last Post: 23 August 2010 - 07:15 AM Rate Topic: -----

#1 brenz13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-June 09

I need help in Round Robin Scheduling

Posted 22 August 2010 - 03:09 AM

I need a round robin scheduling that will ask for number of process, process name, arrival time, burst time, and quantum time. It will also compute the waiting time of each process and the average waiting time of all process, turn-around time of each process and the average turn around time of all process. It will also compute the CPU Utilization and the System throughput. Im stuck with my code don't know what's wrong with my code. can anyone help me please? thank you so much.

import java.io.*;
import java.util.*;
class process
{
String name;
int burst;
int arrival;
boolean done;
int wt;
process(String x, int y )
{
name=x;
arrival=y;
done=false;
}
public String toString()
{
return (name + " || ");
}
}
class use
{
public static void main(String args[])throws Exception
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter no. of processes: ");
int n=Integer.parseInt(br.readLine());
process a[]=new process[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter name: ");
String name=br.readLine();
System.out.println("Enter burst time: ");
int time=Integer.parseInt(br.readLine());
System.out.println("Enter arrival time: ");
int time2=Integer.parseInt(br.readLine());
a[i]=new process(name,time2);
}
System.out.println("Enter time slice: ");
int ts=Integer.parseInt(br.readLine());
int donecount=0;
int j=0;
Queue q=new LinkedList();
int ctime=0;
do
{
if(a[j].arrival<=ts && a[j].done==false)
{
for(int k=0;k<a.length;k++)
{
if(a[k].done==false && k!=j)
a[k].wt+=a[j].arrival;
}
q.add(a[j]);
ctime+=a[j].arrival;
a[j].arrival=0;
a[j].done=true;
donecount++;
}
else if(a[j].burst>ts && a[j].done==false)
{
for(int k=0;k<a.length;k++)
{
if(a[k].done==false && k!=j)
a[k].wt+=ts;
}
q.add(a[j]);
ctime+=ts;
a[j].burst-=ts;
}
if(j==a.length-1)
j=0;
else
j++;
if(donecount==a.length)
break;
}while(true);
process arr[]=new process[100];
int index=0;
int twt=0;
System.out.println("Gantt chart");
while(!q.isEmpty())
{
arr[index++]=(process)q.remove();
System.out.print(arr[index-1]);
}
System.out.println();
System.out.println("Waiting time: " );
for(int m=0;m<a.length;m++)
{
System.out.println("Wt of process "+a[m].name+" is "+a[m].wt);
twt+=a[m].wt;
}
System.out.println("Total waiting time: "+ twt);
System.out.println("Avg. waiting time: "+ (float)twt/a.length);
System.out.println("Total turnaround time: "+ (twt+ctime));
System.out.println("Avg. turnaround time: " + ((float)(twt+ctime)/a.length));
}
}


This post has been edited by brenz13: 22 August 2010 - 03:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I need help in Round Robin Scheduling

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: I need help in Round Robin Scheduling

Posted 22 August 2010 - 11:21 AM

You HAVE to use correct formatting...it's impossible to read without it. I've corrected formatted it for you here:



import java.io.*;
import java.util.*;
class process
{
	String name;
	int burst;
	int arrival;
	boolean done;
	int wt;
	process(String x, int y )
	{
		name=x;
		arrival=y;
		done=false;
	}
	public String toString()
	{
		return (name + " || ");
	}
}
class use
{
	public static void main(String args[])throws Exception
	{
		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter no. of processes: ");
		int n=Integer.parseInt(br.readLine());
		process a[]=new process[n];
		for(int i=0;i<n;i++)
		{
			System.out.println("Enter name: ");
			String name=br.readLine();
			System.out.println("Enter burst time: ");
			int time=Integer.parseInt(br.readLine());
			System.out.println("Enter arrival time: ");
			int time2=Integer.parseInt(br.readLine());
			a[i]=new process(name,time2);
		}
		System.out.println("Enter time slice: ");
		int ts=Integer.parseInt(br.readLine());
		int donecount=0;
		int j=0;
		Queue q=new LinkedList();
		int ctime=0;
		do
		{
			if(a[j].arrival<=ts && a[j].done==false)
			{
				for(int k=0;k<a.length;k++)
				{
					if(a[k].done==false && k!=j)
						a[k].wt+=a[j].arrival;
				}
				q.add(a[j]);
				ctime+=a[j].arrival;
				a[j].arrival=0;
				a[j].done=true;
				donecount++;
			}
			else if(a[j].burst>ts && a[j].done==false)
			{
				for(int k=0;k<a.length;k++)
				{
					if(a[k].done==false && k!=j)
						a[k].wt+=ts;
				}
				q.add(a[j]);
				ctime+=ts;
				a[j].burst-=ts;
			}
			if(j==a.length-1)
				j=0;
			else
				j++;
			if(donecount==a.length)
				break;
		}while(true);
		process arr[]=new process[100];
		int index=0;
		int twt=0;
		System.out.println("Gantt chart");
		while(!q.isEmpty())
		{
			arr[index++]=(process)q.remove();
			System.out.print(arr[index-1]);
		}
		System.out.println();
		System.out.println("Waiting time: " );
		for(int m=0;m<a.length;m++)
		{
			System.out.println("Wt of process "+a[m].name+" is "+a[m].wt);
			twt+=a[m].wt;
		}
		System.out.println("Total waiting time: "+ twt);
		System.out.println("Avg. waiting time: "+ (float)twt/a.length);
		System.out.println("Total turnaround time: "+ (twt+ctime));
		System.out.println("Avg. turnaround time: " + ((float)(twt+ctime)/a.length));
	}
}




Cheers.

This post has been edited by Dogstopper: 22 August 2010 - 11:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,072
  • Joined: 27-December 08

Re: I need help in Round Robin Scheduling

Posted 22 August 2010 - 11:51 AM

@Dogstopper: Corrected the code or just formatted it?

@brenz13: We can't help you if you don't tell us what is wrong with your code as well. :)
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: I need help in Round Robin Scheduling

Posted 22 August 2010 - 11:54 AM

I formatted it technically...but it aided in the corrections.
Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,072
  • Joined: 27-December 08

Re: I need help in Round Robin Scheduling

Posted 22 August 2010 - 11:55 AM

+1 for formatting. :)
Was This Post Helpful? 0
  • +
  • -

#6 brova  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 37
  • Joined: 20-October 09

Re: I need help in Round Robin Scheduling

Posted 22 August 2010 - 01:09 PM

+2 for formatting lol
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: I need help in Round Robin Scheduling

Posted 22 August 2010 - 01:27 PM

:D
Was This Post Helpful? 0
  • +
  • -

#8 brenz13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-June 09

Re: I need help in Round Robin Scheduling

Posted 23 August 2010 - 07:15 AM

Thanks for formatting my code... the problem in my codes is it does not follow the arrival time, instead whatever i input first on the queue,it will be the first to process... and it does not loop, once the processes are executed once it does do the concept of round robin scheduling,,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1