Results 1 to 13 of 13
Thread: FCFS Scedule in Java
- 05-11-2011, 05:42 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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:
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.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
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
- 05-12-2011, 05:00 PM #2
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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:
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?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]); } }
Thanks.Last edited by zimbobigdog; 05-12-2011 at 05:04 PM.
- 05-12-2011, 05:15 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 05-12-2011, 06:06 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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:
If I includeJava Code://initialise variables { CPU_time = 0; // procQueue.add(procList[3][1]); }I get the following error:Java Code:procQueue.add(procList[3][1]);
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?
- 05-12-2011, 06:28 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 05-12-2011, 07:13 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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
this seems to work ok. I then create the procInfo class with it's elements: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++; } } }
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.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 }
The code I am thinking of adding once I am able to add to procQueue is this (not final):
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.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++; // }Last edited by zimbobigdog; 05-12-2011 at 07:16 PM.
- 05-13-2011, 09:24 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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):
Then you can access the procQueue:Java Code:ProcInfo procInfo = new ProcInfo();
Similarly for the other attributes of procInfo.Java Code:procInfo.procQueue ...
- 05-13-2011, 01:18 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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:
andJava 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]); }
andJava 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); }
This code is now part of the Main method so has access to procList. (I have checked this by doingJava 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]); }
What do I need to do to add the elements to procQueue?Java Code:System.out.prinLn (procList[0][0]);
Thanks.
- 05-13-2011, 01:29 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You'll have to show the compilation error and the line that's causing it.
- 05-13-2011, 02:26 PM #10
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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:
Thanks for your assistance.Java Code:procQueue.add (procList[i][0]);
- 05-13-2011, 02:49 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 05-14-2011, 01:20 PM #12
Member
- Join Date
- Dec 2010
- Posts
- 13
- Rep Power
- 0
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?
- 05-16-2011, 12:25 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Scheduling problem with FCFS/FIFO
By ShutUpAndExplode in forum New To JavaReplies: 7Last Post: 04-26-2010, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks