Results 1 to 13 of 13
  1. #1
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default FCFS Scedule in Java

    Hi. Hpoing someone might be able to help me with a CPU Scheduling algorithm I am trying to develop. I have a csv file with 4 columns for Process ID, Arrival Time, CPU Burst and Priority. I can successfully import the data into an array and I have a fairly good idea of what I need to do but I am getting confused about how to deal with the elements of the array and assign them to variables.
    The code I have so far is as follows:
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.StringTokenizer;
    
    public class FCFS1
    {
    	public static void main(String[] args) throws IOException
    	{
    //Create Array containing jobs to be processed
    	int [][] procList = new int [100][4];
    	File file = new File("processlist.csv");
    	int row = 0;
    	int col = 0;
    	BufferedReader bufRdr = new BufferedReader(new FileReader(file));
    	String line = null;
    
    //read each line of text file
    	while((line = bufRdr.readLine()) != null && row < 11)
    		{
    		StringTokenizer ProcessQueue = new StringTokenizer(line,",");
    		while (ProcessQueue.hasMoreTokens())
    			{
    //get next token and store it in the array
    			procList[row][col] = Integer.parseInt(ProcessQueue.nextToken());
    			col++;
    			}
    		col = 0;
    		row++;
    		}
    	}
    
    //Create class for Process information
    	public class ProcessInfo
    	{
    	int PID;		//PID
    	int CPU_burst_to_go;	// CPU Burst to finish
    	int position;		// curent position in the queue (not needed for SJF or Priority, needed 				// for FCFS, RR with FCFS
    	int priority;		// priority  
    	int arrival;		// arrival time
    	int ResponseTime;	// Response Time - first time in the CPU
    	int WaitingTime;	// Waiting Time	- total time spent in the queue
    	boolean inCPU;		// True if the process is in the CPU
    	}
    // This will store the process queue
    
    	ProcessInfo ProcessQueue[100];
    
    	int CPU_time;		//variable for the time in CPU
    	int ContextSwitchNum;	// A variable for counting the Context Switches
    
    // Output variables
    
    	double AvgWaitingTime;
    	double AvgResponseTime;
    	int [] CPU_Usage = new int[100];	//Each element contains the PID of the process in the CPU at that time
    
    	CPU_time=0;
    
    	while (procList.length>0)
    	{
    	for (int i=0; i<=procList.length; i++)
    		{
    		for (int j=0; j<=procList.length; j++)
    			{
    			for (int k=0; k<=procList.length; k++)
    				{
    				for (int l=0; l<=procList.length; l++)
    				}
    			}
    		}
    	}
    	if j<=cpu_time
    	{
    	PID=i;
    	ProcessQueue.add(i);
    	CPU_burst_to_go=k;
    	position=(ProcessQueue.length)+1;
    	priority=k;
    	arrival=j;
    	waitingTime=0;
    	inCPU = False
    	
    		if (cpu_usage==null)
    		{
    		cpu_usage.add[i];
    		inCPU = True;
    		}
    		else
    		{
    		cpu_usage
    	}
    
    	cpu_time++;	//next millisecond
    I am not sure that the for loops are the best way to do what I need. I am trying to assign i to the PID, j to the arrival time, k to the CPU burst and l to the priority.

    Am I on the right tracks? Is there a better way to itterate through the ProcessList array and add elements to the processqueue?

    Any help would be greatly appreciated.

    Thanks.
    zimbobigdog

  2. #2
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    I have developed my code further and am now working through it bit by bit. I am trying to add the Process ID from the Process list to the ProcQueue but am getting the following error:
    FCFS1.java:60: <identifier> expected
    final procQueue.add(procList[3][1];);
    ^
    FCFS1.java:60: not a statement
    final procQueue.add(procList[3][1];);
    ^
    FCFS1.java:60: illegal start of expression
    final procQueue.add(procList[3][1];);
    ^
    3 errors

    My code is now as follows:
    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.StringTokenizer;
    
    public class FCFS1
    {
    	public static void main(String[] args) throws IOException
    	{
    
    //Create Array containing jobs to be processed
    	int [][] procList = new int [100][4];
    	File file = new File("processlist.csv");
    	int row = 0;
    	int col = 0;
    	BufferedReader bufRdr = new BufferedReader(new FileReader(file));
    	String line = null;
    
    //read each line of text file
    	while((line = bufRdr.readLine()) != null && row < 11)
    		{
    		StringTokenizer ProcessQueue = new StringTokenizer(line,",");
    		while (ProcessQueue.hasMoreTokens())
    			{
    //get next token and store it in the array
    			procList[row][col] = Integer.parseInt(ProcessQueue.nextToken());
    			col++;
    			}
    		col = 0;
    		row++;
    		}
    
    //Create Process information
    	class procInfo
    		{
    		int PID;				// Process ID
    		int CPU_burst_to_go;			// CPU Burst to finish
    		int position;				// curent position in the queue
    		int priority;				// priority  
    		int arrival;				// arrival time
    		int ResponseTime;			// Response Time - first time in the CPU
    		int WaitingTime;			// Waiting Time	- total time spent in the queue
    		boolean inCPU;				// True if the process is in the CPU
    
    		int [] procQueue = new int[100];	// This will store the process queue
    		int CPU_time;				// variable for the time in CPU
    		int ContextSwitchNum;			// A variable for counting the Context Switches
    
    // Output variables
    		double AvgWaitingTime;
    		double AvgResponseTime;
    		int [] CPU_Usage = new int[100];	//Each element contains the PID of the process in the CPU at that time
    	
    //initialise variables
    			{
    			CPU_time = 0;
    			final procQueue.add(procList[3][1]);
    			}
    System.out.println(procList[6][1]);
    	}
    }
    All I want to do for now is add the 2nd element of the 3rd array from the procList array to the procQueue array. Any ideas why I am getting the error?

    Thanks.
    Last edited by zimbobigdog; 05-12-2011 at 06:04 PM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    The "final" keyword does not belong there.
    Also (and it could be the indentation that's throwing me) but is that procInfo class deinfed in the correct place? Looks to me like it's defined inside your main() method.

  4. #4
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    Thanks Tolls. I have changed the code so that the procInfo class is being declared outside the Main () method. The code compiles if if save the code as follows:
    Java Code:
    //initialise variables
    	{
    	CPU_time = 0;
    //	procQueue.add(procList[3][1]);
    	}
    If I include
    Java Code:
    procQueue.add(procList[3][1]);
    I get the following error:
    FCFS1.java:61: cannot find symbol
    symbol : variable procList
    location: class procInfo
    procQueue.add(procList[3][1]);
    ^
    1 error

    Is it possible to add an element from one array to another array?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    procList is declared in main.
    That code (assuming it's still in the same place) appears to be inside the procInfo class declaration, so cannot see procList.

    Since I have no idea what your intended design is I cannot say what it should look like.

  6. #6
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    That is exactly what confuses me. I declared procList in the Main method so that I can import the data from a csv file into the procList array. Then I created the local class procInfo with various variables including the array procQueue. I now need to be able to add elements from procList to procQueue but like you say, because procList was declared in Main, I cannot access it. How can I structure the code so that I can access procList and add it's elements to procQueue?

    The first part of my code imports from a csv file into the procInfo array
    Java Code:
    public class FCFS1
    {
    public static void main(String[] args) throws IOException
    	{
    //Create Array containing jobs to be processed
    	int [][] procList = new int [100][4];
    	File file = new File("processlist.csv");
    	int row = 0;
    	int col = 0;
    	BufferedReader bufRdr = new BufferedReader(new FileReader(file));
    	String line = null;
    
    //read each line of text file
    	while((line = bufRdr.readLine()) != null && row < 11)
    		{
    		StringTokenizer ProcessQueue = new StringTokenizer(line,",");
    		while (ProcessQueue.hasMoreTokens())
    			{
    //get next token and store it in the array
    			procList[row][col] = Integer.parseInt(ProcessQueue.nextToken());
    			col++;
    			}
    		col = 0;
    		row++;
    		}
    	}
    }
    this seems to work ok. I then create the procInfo class with it's elements:
    Java Code:
    //Create Process information
    class procInfo
    {
    	int PID;				// Process ID
    	int CPU_burst_to_go;			// CPU Burst to finish
    	int position;				// curent position in the queue
    	int priority;				// priority  
    	int arrival;				// arrival time
    	int ResponseTime;			// Response Time - first time in the CPU
    	int WaitingTime;			// Waiting Time	- total time spent in the queue
    	boolean inCPU;				// True if the process is in the CPU
    	
    	int [] procQueue = new int[100];	// This will store the process queue
    	int CPU_time;				// variable for the time in CPU
    	int ContextSwitchNum;			// A variable for counting the Context Switches
    
    // Output variables
    	double AvgWaitingTime;
    	double AvgResponseTime;
    	int [] CPU_Usage = new int[100];	//Each element contains the PID of the process in the CPU at that time
    }
    Now I need to be able to loop through the procList array and add elements to procQueue. The columns of procList represent PID, arrival, CPU_burst_to_go and priority. I want to be able to add the PID element to procQueue if arrival=CPU_time. I have an idea of the code I need to do all this but want to get to a state where I can at least add elements from procList to procQueue.

    The code I am thinking of adding once I am able to add to procQueue is this (not final):
    Java Code:
    //	for (int i=0; i<procList.length; i++)
    //		{
    //		for (int j=0; j<procList[i].length; j++)
    //			{
    //			if (procList[i][2])=cpu_time
    //				{						//Add the processes to
    //				procQueue.add(procList[3][1]);		//the process queue and
    //				CPU_burst_to_go=[i][3];				//set the relevant
    //				position=(processQueue.length)+1;		//variables including
    //				priority=[i][4];				//waiting time
    //				arrival=[i][2];
    //				ResponseTime=0;				
    //				WaitingTime=0;
    //				}
    //			if (CPU_burst_to_go=0)
    //				{
    //				inCPU = False;
    //				position=processQueue.length;
    //				CPU_Usage.add[i++];
    //				}
    //				else inCPU = True;
    //				cpu_time++;	//next millisecond
    //				CPU_burst_to_go--;
    //				WaitingTime++;
    //		}
    My intended goal is to simulate a First Come First Served (first in first out) CPU scheduling algorithm using the list of processes in the csv file.
    Last edited by zimbobigdog; 05-12-2011 at 08:16 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    You'll need to create an instance of ProcInfo (by the way, class names should start with a capital. It's variable names that start with a lower case):
    Java Code:
    ProcInfo procInfo = new ProcInfo();
    Then you can access the procQueue:
    Java Code:
    procInfo.procQueue ...
    Similarly for the other attributes of procInfo.

  8. #8
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    Thanks Tolls I've now removed the procInfo class and am only working with one class. I can declare and initialise all variables fine but am still having a problem adding elements to the procQueue array. I have tried adding the elements using various methods but keep getting the "cannot find symbol" error. The methods I have tried are:
    Java Code:
    //Create Process information
    	for(int i=0; i<procList.length; i++)
    		{
    		int PID = procList [i][0];				// Process ID
    		int CPU_burst_to_go = procList [i][2];			// CPU Burst to finish
    		int position = i;					// curent position in the queue
    		int priority = procList[i][3];						// priority  
    		int arrival = procList [i][1];				// arrival time
    		int ResponseTime = 0;					// Response Time - first time in the CPU
    		int WaitingTime = 0;					// Waiting Time	- total time spent in the queue
    		boolean inCPU;						// True if the process is in the CPU
    
    		int [] procQueue = new int[100];			// This will store the process queue
    		int CPU_time = 0;				// variable for the time in CPU
    		int queueElement;
    
    //Create Output variables
    		double AvgWaitingTime;
    		double AvgResponseTime;
    		int [] CPU_Usage = new int[100];			//Each element contains the PID of the process in the CPU at that time
    
    		if (arrival==CPU_time)
    			{
    			procQueue[i] = Integer.parseInt (procList[i][0]);
    			}
    and
    Java Code:
    //Create Process information
    	for(int i=0; i<procList.length; i++)
    		{
    		int PID = procList [i][0];				// Process ID
    		int CPU_burst_to_go = procList [i][2];			// CPU Burst to finish
    		int position = i;					// curent position in the queue
    		int priority = procList[i][3];						// priority  
    		int arrival = procList [i][1];				// arrival time
    		int ResponseTime = 0;					// Response Time - first time in the CPU
    		int WaitingTime = 0;					// Waiting Time	- total time spent in the queue
    		boolean inCPU;						// True if the process is in the CPU
    
    		int [] procQueue = new int[100];			// This will store the process queue
    		int CPU_time = 0;				// variable for the time in CPU
    		int queueElement;
    
    //Create Output variables
    		double AvgWaitingTime;
    		double AvgResponseTime;
    		int [] CPU_Usage = new int[100];			//Each element contains the PID of the process in the CPU at that time
    
    		if (arrival==CPU_time)
    			{
    			procQueue[i] = Integer.parseInt (PID);
    			}
    and
    Java Code:
    //Create Process information
    	for(int i=0; i<procList.length; i++)
    		{
    		int PID = procList [i][0];				// Process ID
    		int CPU_burst_to_go = procList [i][2];			// CPU Burst to finish
    		int position = i;					// curent position in the queue
    		int priority = procList[i][3];						// priority  
    		int arrival = procList [i][1];				// arrival time
    		int ResponseTime = 0;					// Response Time - first time in the CPU
    		int WaitingTime = 0;					// Waiting Time	- total time spent in the queue
    		boolean inCPU;						// True if the process is in the CPU
    
    		int [] procQueue = new int[100];			// This will store the process queue
    		int CPU_time = 0;				// variable for the time in CPU
    		int queueElement;
    
    //Create Output variables
    		double AvgWaitingTime;
    		double AvgResponseTime;
    		int [] CPU_Usage = new int[100];			//Each element contains the PID of the process in the CPU at that time
    
    		if (arrival==CPU_time)
    			{
    			procQueue.add (procList[i][0]);
    			}
    This code is now part of the Main method so has access to procList. (I have checked this by doing
    Java Code:
    System.out.prinLn (procList[0][0]);
    What do I need to do to add the elements to procQueue?

    Thanks.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    You'll have to show the compilation error and the line that's causing it.

  10. #10
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    Here are the errors:
    *******************
    FCFS.java:59: cannot find symbol
    symbol : method add(int)
    location: class int[]
    procQueue.add (procList[i][0]);
    ^
    1 error
    *******************
    FCFS.java:59: cannot find symbol
    symbol : method parseInt(int)
    location: class java.lang.Integer
    procQueue[i] = Integer.parseInt (procList[i][0]);
    ^
    1 error
    *******************
    FCFS.java:59: cannot find symbol
    symbol : method parseInt(int)
    location: class java.lang.Integer
    procQueue = Integer.parseInt (PID);

    1 error
    ********************

    This is Line 59:
    Java Code:
    			procQueue.add (procList[i][0]);
    Thanks for your assistance.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    symbol : method add(int)
    In this case procQueue is an int[]. An array does not have an add() method.

    symbol : method parseInt(int)
    In this case the parseInt method on Integer is for turning a String to an int. So it expects a String parameter. There is no version of the method (understandably) that takes an int parameter, which is why that symbol is not found.

    You have an array.
    Simply assign your int to the next slot in the queue.
    You might need to store the next available slot somewhere, though.

  12. #12
    zimbobigdog is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Rep Power
    0

    Default

    I'm sure this is a stupid question but how do I store the int as the next element in the queue? Is it a matter of looping through the queue and the assigning the int to the first null element?

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,178
    Rep Power
    20

    Default

    I would use a nextIndex variable, which holds the index of the next valid slot in the procQueue. So it would start at 0, and you'd increment it everytime you added an int to the queue.

    Unless I've missed something, of course...it is Monday after all.

Similar Threads

  1. Scheduling problem with FCFS/FIFO
    By ShutUpAndExplode in forum New To Java
    Replies: 7
    Last Post: 04-26-2010, 06:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •