8 Replies - 242 Views - Last Post: 08 December 2017 - 10:23 PM Rate Topic: -----

#1 Preacher2012  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Errors in my UniprocessorSchedular program

Posted 04 December 2017 - 08:52 PM

Hello, my assignment is to simulate some uniprocessor scheduling algorithms in Java. The ones that I'm supposed to simulate are: First Come First Serve(FCFS), Round-Robin(RR), Highest Response Ratio Next(HRRN), and Shortest Remaining Time(SRT). I have a sample .csv file, which will be parsed, with the following Process ID's, Execution Time Required, and Arrival Time:

U,5,4
A,0,3
B,2,6
C,4,4
D,6,5
E,8,2

Here is my code so far:
//import necessary java libraries
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.LinkedList;

//define class
public class UniprocessingSchedular
{
	public static void main(String args[])
	{
		try
		{
			//scanner to scan the .csv file
			Scanner scan = new Scanner(new File("processe.csv"));
			String s = scan.hasNext()?scan.nextLine():null;
			int processors; //# of processes
			int rrQ = 0; //Round-Robin Q
			List<Process> pList = new LinkedList<>();//process list
			if(s != null)
			{
				String[] arg = s.split(",");//separated by comma
				processors = Integer.parseInt(arg[1]);//# processes
				rrQ = Integer.parseInt(arg[2]);//Round-Robin Q
			}
			while (scan.hasNext())
			{
				String ln = scan.nextLine();
				String proc[] = ln.split(",");
				//add each of the processes to the list
				pList.add(new Process(proc[0], Integer.parseInt(proc[1]), Integer.parseInt(proc[2])));
			}
		}
		catch (FileNotFoundException ignore)
		{
			//show this message if the file with that name doesn't exist
			System.out.println("\nThat file does not exist.\n");
		}
	}
}



When I compile, I keep getting this error:

java:31: error: Process is abstract; cannot be instantiated
pList.add(new Process(proc[0], Integer.parseInt(proc[1]), Integer.parseInt(proc[2])));
^
1 error

Tool completed with exit code 1

Can someone tell me how to fix this error? I've looked up online and I've seen people use add a process like this, I don't understand why I'm getting this error. Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Errors in my UniprocessorSchedular program

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Errors in my UniprocessorSchedular program

Posted 04 December 2017 - 09:31 PM

You can't instantiate abstract classes or interfaces. You have to instantiate a subclass of Process that is not abstract. If you are using the java.lang.Process class, the documentation discusses how to obtain Process objects.
Was This Post Helpful? 1
  • +
  • -

#3 Preacher2012  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Errors in my UniprocessorSchedular program

Posted 04 December 2017 - 10:00 PM

I'm sorry, I'm trying to understand this. How can I go about doing this if I can't use the following bit of code:

pList.add(new Process(proc[0], Integer.parseInt(proc[1]), Integer.parseInt(proc[2])));



I saw the document and its talking about starting a process with processbuilder.start(). But what If I just want to simulate processes by parsing the file in my original post?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Errors in my UniprocessorSchedular program

Posted 04 December 2017 - 10:13 PM

Process is an abstract class, so only concrete subclasses can be instantiated. Beyond that, note that (per the docs) Process does not have the constructor you are trying to invoke. For both of these reasons, you cannot use the line of code you specified.

You must use the methods specified in the documentation. I am assuming you are using the built-in Process class, rather than your own Process class.
Was This Post Helpful? 1
  • +
  • -

#5 Preacher2012  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Errors in my UniprocessorSchedular program

Posted 04 December 2017 - 10:21 PM

I found a solution online. I added the following at the bottom of my code and it seems to be working so far:
private static class Process
	{
		String name;
		int arrivalTime;
		int serveTime;
		int remainingTime;

		Process(String name, int arrivalTime, int ServeTime)
		{
			this.name = name;
			this.arrivalTime = arrivalTime;
			this.serveTime = serveTime;
			remainingTime = serveTime;
		}
	}



I'm not done with the assignment however, so I'll be posting here if I encounter any other errors. Thanks for the document.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12275
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Errors in my UniprocessorSchedular program

Posted 04 December 2017 - 10:24 PM

The reason this solution works is because your Process class overshadows the built-in Process class. Be careful about naming your classes the same as built-in Java library classes. :)
Was This Post Helpful? 1
  • +
  • -

#7 Preacher2012  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Errors in my UniprocessorSchedular program

Posted 04 December 2017 - 11:46 PM

Ugh, where to start. We haven't done any programming in this class prior to this, especially not with scheduling algorithms. This is a very tedious project. I'm supposed to parse this .csv file:

U,5,4
A,0,3
B,2,6
C,4,4
D,6,5
E,8,2

And perform different scheduling algorithms with it: First Come First Serve(FCFS), Round-Robin(RR), Highest Response Ratio Next(HRRN), and Shortest Remaining Time(SRT).

My updated code looks like this:
//import necessary java libraries
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.LinkedList;

//define class
public class UniprocessingSchedular
{
	public static void main(String args[])
	{
		try
		{
			//scanner to scan the .csv file
			Scanner scan = new Scanner(new File("processes.csv"));
			String s = scan.hasNext()?scan.nextLine():null;
			int processors; //# of processes
			int rrQ = 0; //Round-Robin Q
			List<Process> pList = new LinkedList<>();//process list
			if(s != null)
			{
				String[] arg = s.split(",");//separated by comma
				processors = Integer.parseInt(arg[1]);//# processes
				rrQ = Integer.parseInt(arg[2]);//Round-Robin Q
			}
			while (scan.hasNext())
			{
				String ln = scan.nextLine();
				String proc[] = ln.split(",");
				//add each of the processes to the list
				pList.add(new Process(proc[0], Integer.parseInt(proc[1]), Integer.parseInt(proc[2])));
			}

			System.out.println("\n\n" + "FCFS:\n");
			firstComeFirstServe(pList);
			System.out.println("\n\n" + "RR:\n");
			roundRobin(pList, rrQ);
		}
		catch (FileNotFoundException ignore)
		{
			//show this message if the file with that name doesn't exist
			System.out.println("\nThat file does not exist.\n");
		}
	}

	//round-robin function
	private static void roundRobin(List<Process> pList, int rrQ)
	{
		Queue<Process> que = new ArrayDeque<>(); //create process queue
	    Iterator<Process> it = pList.iterator(); //instantiate iterator for the list
	    Process e2 = null;
	    int time = 0; //set time to 0
	    boolean flag = false; //set boolean to false
	    while (flag || !que.isEmpty() || it.hasNext())//if queue is not empty and has more
	    {
	        Process e = !que.isEmpty() ? que.remove() : it.next();//get next process
	            if (e.remainingTime != 0)//if remainig time is not 0
	            {
	            int remTime = e.remainingTime - rrQ;//remaining time is remaining time - quantum
	            if (remTime > 0) //if the remaining time is > 0
	            {
	                System.out.println(e.name + ":" + time + "->" + (time + rrQ)); //print out process
	                time += rrQ; //add quantum to time
	                e.remainingTime = e.remainingTime - rrQ; //subtract quantum from remaining time
	                flag = true; //set boolean to true
	                if(e2!=null && e2.arrivalTime < time)
	                {
	                    que.add(e2); //add e2 to the queue
	                    e2 = null;
	                }
	                while (it.hasNext())//while there is still more
	                {

	                    Process e1 = it.next(); //get the next process
	                    if(e1.arrivalTime < time) //if the arrival time is less than time
	                    {
	                        que.add(e1); //add it to the queue
	                    }
	                    else
	                    {
	                        e2 = e1; //set e2 to e1
	                        break;
	                    }
	                }
	                que.add(e); //add to the queue
	            }
	            else
	            {
	                System.out.println(e.name + ":" + time + "->" + (time + e.remainingTime)); //print out the process
	                time += e.remainingTime; //time is sum of time and remaining time
	                if(e2!=null && e2.arrivalTime < time) //if e2 isn't null and arrival time is < time
	                {
	                    que.add(e2); //add to the queue
	                    e2 =null;
	                }
	                while (it.hasNext()) //while there is more
	                {

	                    Process e1 = it.next(); //get the next process
	                    if(e1.arrivalTime < time) //if the arrival time is less than time
	                    {
	                        que.add(e1); //add to the queue
	                    }
	                    else
	                    {
	                        e2 =e1; //set e2 to e1
	                        break;
	                    }
	                }
	                flag =false; //set boolean to false
	                e.remainingTime = 0; //set remaining time to 0
	            }
	        }
	    }
    }

	public static void firstComeFirstServe(List<Process> pList)
	{
		Iterator<Process> it = pList.iterator(); //instantiate iterator for the list

		int time = 0; //set time to 0

		while(it.hasNext()) //while there is more
		{
			Process e = it.next(); //get the next process
			System.out.println(e.name + ":" + time + "->" + (time + e.serveTime)); //print it out
			time += e.serveTime; //increment time by the serveTime
		}
	}

	//override Process class
	private static class Process
	{
		//declare process variables
		String name;
		int arrivalTime;
		int serveTime;
		int remainingTime;

		//Constructor
		Process(String name, int arrivalTime, int ServeTime)
		{
			//set default values
			this.name = name;
			this.arrivalTime = arrivalTime;
			this.serveTime = serveTime;
			remainingTime = serveTime;
		}
	}
}



The code compiles without showing any errors, but I realized after running it that I have some logic errors. I'm not getting the right output for the two scheduling methods (FCFS and RR) that I have so far. My output is supposed to look something like this:

FCFS:
A:0->3
B:3->9
C:9->13
D:13->18
E:18->20

RR:
A:0->3
B:3->7
C:7->11
D:11->15
B:15->17
E:17->19
D:19->20

But instead, I'm getting this output:

FCFS:

A:0->0
B:0->0
C:0->0
D:0->0
E:0->0


RR:

I looked over my code a few times, but everytime I do, I become more confused. Can someone please help? All help is greatly appreciated.
Was This Post Helpful? 0
  • +
  • -

#8 Preacher2012  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Errors in my UniprocessorSchedular program

Posted 05 December 2017 - 11:20 PM

Never mind, I fixed the issue with the zeros and missing RR output. I'll post here later if I need help with the other two functions that I am working on (Highest Response Ratio First (HRRN) and Shortest Time Remaining (SRT)).
Was This Post Helpful? 0
  • +
  • -

#9 Preacher2012  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 158
  • Joined: 02-November 12

Re: Errors in my UniprocessorSchedular program

Posted 08 December 2017 - 10:23 PM

With my code so far, which is:

//import necessary libraries
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.LinkedList;

public class UniprocessingSchedular //start UniprocessingSchedular Class
{
	public static void main(String args[]) //start main
	{
		try //start try block
		{
			Scanner scan = new Scanner(new File("processes.csv")); //file with sample variables
			String s = scan.hasNext() ? scan.nextLine() : null; //check if there is content
			int processors; //# of processors
			int rrQ = 0; //round robin quantum
			List<Process> pList = new LinkedList<>(); //list to hold the processes
			if(s != null) //if there is content
			{
				String[] arg = s.split(",");//comma separated conent
				processors = Integer.parseInt(arg[1]);//get # of processes
				rrQ = Integer.parseInt(arg[2]); //get the round robin quantum
			}
			while(scan.hasNext()) //while there is more
			{
				String ln = scan.nextLine(); //get the next line
				String proc[] = ln.split(","); //comma separated content
				//get the Process ID, Required Execution Time, and Arrival Time
				pList.add(new Process(proc[0], Integer.parseInt(proc[1]), Integer.parseInt(proc[2])));
			}
			System.out.println("\n\n" + "FCFS:\n");//FCFS label
			FirstComeFirstServe(pList);//perform FCFS algorithm
			System.out.println("\n\n" + "RR:\n");//RR label
			RoundRobin(pList, rrQ);//perform RR algorithm
			System.out.println("\n\n" + "HRRN:\n");//RR label
			HighestResponseRatioNext(pList);//perform HRRN algorithm
			System.out.println("\n\n" + "SRT:\n");//RR label
			ShortestRemainingTime(pList);//perform SRT algorithm
		} //end try block
		catch(FileNotFoundException msg)//if file doesn't exist, throw an error message
		{
			System.out.println("\nA file with that name does not exist, please try again.\n");
		} //end catch
	}

	//define the RR function
	private static void RoundRobin(List<Process> pList, int rrQ)
	{
		Queue<Process> que = new ArrayDeque<>(); //queue processes
		Iterator<Process> it = pList.iterator(); //an iterator for the list
		Process e2 = null; //set process e2 to null
		int time = 0; //set time to 0
		boolean flag = false; //set flag to default value of false
		while(flag || !que.isEmpty() || it.hasNext()) //while the que isn't empty
		{
			Process e = !que.isEmpty() ? que.remove() : it.next(); //get the next process
			if(e.remainingTime != 0) //if there's time remaining
			{
				int remTime = e.remainingTime - rrQ; //rr quantum is subtracted from remaining time
				if(remTime > 0) //if there is time remaining
				{
					System.out.println(e.name + ":" + time + "->" + (time+rrQ)); //print process and times
					time += rrQ; //add the rr quantum to time
					e.remainingTime = e.remainingTime - rrQ;//subtract rr quantum from remaining time
					flag = true; //set flag to true
					if(e2!=null && e2.arrivalTime < time) //if e2 isn't null and arrival time is less than time
					{
						que.add(e2); //add e2 to the queue
						e2 = null; //set e2 to null
					}
					while(it.hasNext()) //while there's more
					{
						Process e1 = it.next(); //get the next process
						if(e1.arrivalTime < time) //if the arrival time is less than time
						{
							que.add(e1);//add e1 to the queue
						}
						else
						{
							e2 = e1; //else set e2 as e1
							break;
						}
					}
					que.add(e);//add e to the queue
				}
				else //if remaining time is 0
				{
					System.out.println(e.name + ":" + time + "->" + (time + e.remainingTime));//print process and times
					time += e.remainingTime; //increment time by remaining time
					if(e2!=null && e2.arrivalTime < time) //if e2 isn't null and arrival time is less than time
					{
						que.add(e2); //add e2 to the queue
						e2 = null; //set e2 to null
					}
					while(it.hasNext()) //while there's more
					{
						Process e1 = it.next(); //get the next process
						if(e1.arrivalTime < time)//if arrival time is less than time
						{
							que.add(e1);//add e1 to the queue
						}
						else
						{
							e2 = e1;//else set e2 as e1
							break;
						}
					}
					flag = false; //set flag to false
					e.remainingTime = 0; //set remaining time to 0
				}
			}
		}
	}

	//define the FCFS function
	private static void FirstComeFirstServe(List<Process> pList)
	{
		Iterator<Process> it = pList.iterator(); //an iterator for the list
		int time = 0; //set time to 0
		while(it.hasNext())//while there's more
		{
			Process e = it.next();//get the next process
			System.out.println(e.name + ":" + time + "->" + (time+e.serveTime));//print process and times
			time += e.serveTime;//increment time by serveTime
		}
	}

	//define the HRRN function
	private static void HighestResponseRatioNext(List<Process> pList)
	{
		Iterator<Process> it = pList.iterator();//an iterator for the list
		int time = 0; //set time to 0
		while(it.hasNext())//while there's more
		{
			Process e = it.next(); //get the next process
		}

	}

	//define the SRT function
	private static void ShortestRemainingTime(List<Process> pList)
	{
		Iterator<Process> it = pList.iterator();//an iterator for the list
		int time = 0; //set time to 0
	}

	//define process class to override Proces
	private static class Process
	{
		//define process variables
		String name;
		int arrivalTime;
		int serveTime;
		int remainingTime;

		//define the constructor
		Process(String name, int arrivalTime, int serveTime)
		{
			//set default values
			this.name = name;
			this.arrivalTime = arrivalTime;
			this.serveTime = serveTime;
			remainingTime = serveTime;
		}
	}
}


I'm able to produce the following output:

FCFS:

A:0->3
B:3->9
C:9->13
D:13->18
E:18->20

RR:

A:0->3
B:3->7
C:7->11
D:11->15
B:15->17
E:17->19
D:19->20

HRRN:

SRT:

According to my professor, the output for HRRN and SRT should look like this:

HRRN:

A:0->3
B:3->9
C:9->13
E:13->15
D:15->20

SRT:

A:0->3
B:3->4
C:4->8
E:8->10
B:10->15
D:15->20

I'm looking at the functions I've written for FCFS and RR so far, and I'm trying to write functions for HRRN and SRT but I'm having trouble wrapping my head around it. For HRRN, I have started a loop to get processes, but I don't know where to go from there. The equation for the ratio, as shown in the textbook is, Response ratio = (time waiting + execution time required) / execution time required. How can one implement that in a function and then print out the processes in the format shown above? All help is greatly appreciated, thank you in advance guys.

This post has been edited by Preacher2012: 08 December 2017 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1