losh177 0 Junior Poster in Training

Hi,

I'm having some problems with an infinite loop. For some reason, the process 112 is not being send anywhere. What i'm suppose to to do is to mimic a CPU. Each line represents a process which the first number is the machine time that it suppose to be send to the CPU, the second number is the process ID and the third number is the time it will take to that process to be completed. All process are being loaded into the initial queue (0) and then there are 4 other priority queues. If a process needs to be send to the CPU but the CPU is not free, the process is moved to the next queue. If anyone could point why i'm stuck in the infinit loop, thanks

Data File:
<Time to send to CPU > <Process ID> <Time for Process to be completed>
2 101 3
7 102 1
9 103 7
12 104 5
14 105 1
17 106 1
18 107 50
24 108 2
34 109 12
37 110 3
44 111 10
45 112 48
50 113 1
56 114 1
71 115 3
81 116 2

public class CPU 
{
	//time that the CPU will spend in a process
	private int CPU_Time;
	
	//process received by the CPU
	private Process Work;
	
	//variable to hold busy flag
	private boolean busy_flag;
	
	
	public CPU()
	{
		busy_flag = false;
		CPU_Time = 0;
	}
	
	//returns true or false if CPU_Time is not 0 (CPU busy)
	public boolean Busy()
	{
		return busy_flag;
	}
	
	//pass process for the first time to CPU
	public void getProcess(Process Task, int t)
	{
		Work = Task;
		Work.setTimeIn(t);
		CPU_Time = (int) java.lang.Math.pow(2, (double) Work.getQueue());
		busy_flag = true;
	}
	
	//pass process from queue into CPU
	public void getProcess(Process Task)
	{
		Work = Task;
		CPU_Time = (int) java.lang.Math.pow(2, (double) Work.getQueue());
		busy_flag = true;
	}
	
	//decrease CPU timer by 1 unit and increase and clears busy flag if CPU_Time = 0
	public void Process()
	{
		CPU_Time--;
		
		//decrease time needed to complete process
		Work.setTimer();
		
		if (CPU_Time == 0 || Work.getTimer() == 0)
			busy_flag = false;
	}
	
	//returns Process to be passed to queue
	public Process freeCPU(int x)
	{
		Work.setTimeOut(x);
		return Work;
	}
}
import java.util.Scanner;
import java.io.*;

public class Driver 
{
	public static void main(String [] arg) throws IOException  
	{
		Scanner Scan = new Scanner(new File("mfq.txt"));
		LineWriter lw = new LineWriter("csci.txt");
		
		//numbers of queue in the system
		int queues = 5;
		
		//Array holding priority Queues of objects
		ObjectQueue [] objQueue = new ObjectQueue [queues];
		
		for (int c = 0; c < queues; c++)
		{
			ObjectQueue obj = new ObjectQueue();
			obj.clear();
			objQueue[c] = obj;
		}
		//Variable to hold any process/task to be pass to the CPU
		//from the text file or a queue
		Process Task;
	    
		//variable to finish all process when set to false
		boolean END;
		
		//CPU object
		CPU Core = new CPU();
		
		//variable to hold time elapsed in the machine
		int Machine_Time = 0;
		
		// q set to -1 as initial value
		int q;
		
		//move all process into objQueue[0]
		while (Scan.hasNext())
		{
			Task = new Process (Scan.nextLine(), lw, 0);
			objQueue[0].insert(Task);
			
		}
		
		lw.println("Process id" + "		" + "Last Queue" + "		" + "Time In" + "		" + "Time Out" );
		System.out.println("Process id" + "		" + "Last Queue" + "		" + "Time In" + "		" + "Time Out" );
		
		//set all process into the first priority queue
		do
		{
			//set END to false for a continuous loop until all queues are empty and q to -1 
			END = true;
			q = -1;
			
			//check if there is any process in the input Queue that needs to be send to CPU
			if (!objQueue[0].isEmpty())
			{
				if (((Process)objQueue[0].query()).getSendTime() == Machine_Time)
				{
					q = 0;
				}
			}
			
			//if a process does not have to be send to CPU, look in priority queues
			if (q != 0)
			{
				for (int i = 1; i < queues; i++)
				{
					if (!objQueue[i].isEmpty())
					{
						q = i;
						break;
					}
				}
			}
			
			if (q > -1)
			{
				Task = (Process) objQueue[q].remove();
				
				if (Core.Busy())
				{
					if (q < queues - 1)
						q++;
					
					Task.setQueue(q);
					objQueue[q].insert(Task);
				}
				else
				{
					if (q == 0)
						Core.getProcess(Task, Machine_Time);
					else
					{
						if (Task.getTimeIn() == 0)
						{
							//if (Task.getId() == 112)
							//{
							//	int t = Task.getId();;
							//}
							Core.getProcess(Task, Machine_Time);
						}
						else
						{
							//if (Task.getId() == 112)
							//{
							//	int t = Task.getId();
							//}
							Core.getProcess(Task);
						}
					}
				}
				
				Core.Process();
				
				//if  CPU is not busy
				if (!Core.Busy())
				{
					Task = Core.freeCPU(Machine_Time);
					
					if (Task.getTimer() == 0)
					{
						//if (Task.getId() == 112)
						//{
						//	int t = Task.getId();;
						//}
						Task.EndProcess();
					}
					else
					{
						if (q < queues - 1)
							q++;
						
						Task.setQueue(q);
						objQueue[q].insert(Task);
					}
				}
				
				//check if all queues are empty
				if (!Core.Busy())
					if (objQueue[0].isEmpty())
						if (objQueue[1].isEmpty())
							if (objQueue[2].isEmpty())
								if (objQueue[3].isEmpty())
									if (objQueue[4].isEmpty())
										END = false;
			}
			
			Machine_Time++;
			
		}while (END);
	}
}
import java.io.*;

/* @author Richard Stegman
 * @version 2.1, 1/5/07
 * 
 * LineWriter provides simple methods for opening and writing to text files.
 * 
 * Example:
 * 
 *      LineWriter lw = new LineWriter(foo.txt");
 *      lw.println("This is a string.");
 *      lw.println();
 *      lw.println(108);
 *      lw.close(); 
 *
 */

public class LineWriter {
    private PrintWriter outFile;
    
    /*
     * LineWriter constructor. 
     * Creates a new file or overwrites an existing file.
     * @param fileName name of the file to be created
     */
    
    public LineWriter(String fileName) {
        try {
            FileWriter fw = new FileWriter(fileName);
            BufferedWriter bw = new BufferedWriter(fw);
            outFile = new PrintWriter(bw);
        }    
        catch (IOException e) {
            System.out.println("LineWriter cannot open output file: " + fileName);
            e.printStackTrace();
        }
    }

    /*
     * LineWriter constructor. 
     * Creates a new file. If the file exists can append to it.
     * @param fileName name of the file to be created
     * @param mode if equal to "a" opens in append mode
     */
    
    public LineWriter(String fileName, String mode) {
        try {
            FileWriter fw = new FileWriter(fileName, "a".equals(mode));
            BufferedWriter bw = new BufferedWriter(fw);
            outFile = new PrintWriter(bw);
        }    
        catch (IOException e) {
            System.out.println("LineWriter cannot open output file: " + fileName);
            e.printStackTrace();
        }
    }

    /**
     * Outputs a string and newline to the file.
     * @param s string to be output
     */
     
    public void println(String s) {
        outFile.println(s);
    }

    /**
     * Outputs a string to the file.
     * @param s string to be output
     */
     
    public void print(String s) {
        outFile.print(s);
    }

    /**
     * Outputs an integer and newline to the file.
     * @param i integer to be output
     */
     
    public void println(int i) {
        outFile.println(i);
    }

    /**
     * Outputs an integer to the file.
     * @param i integer to be output
     */
     
    public void print(int i) {
        outFile.print(i);
    }

    /**
     * Outputs a long and newline to the file.
     * @param l long to be output
     */
     
    public void println(long l) {
        outFile.println(l);
    }

    /**
     * Outputs a long to the file.
     * @param l long to be output
     */
     
    public void print(long l) {
        outFile.print(l);
    }

     /**
     * Outputs a short and newline to the file.
     * @param s short to be output
     */
     
    public void println(short s) {
        outFile.println(s);
    }

    /**
     * Outputs a short to the file.
     * @param s short to be output
     */
     
    public void print(short s) {
        outFile.print(s);
    }
    
    /**
     * Outputs a byte and newline to the file.
     * @param b byte to be output
     */
     
    public void println(byte b) {
        outFile.println(b);
    }

    /**
     * Outputs a byte to the file.
     * @param b byte to be output
     */
     
    public void print(byte b) {
        outFile.print(b);
    }
    
    /**
     * Outputs a boolean and newline to the file.
     * @param b boolean to be output
     */
     
    public void println(boolean b) {
        outFile.println(b);
    }

    /**
     * Outputs a boolean to the file.
     * @param b boolean to be output
     */
     
    public void print(boolean b) {
        outFile.print(b);
    }

    /**
     * Outputs a float and newline to the file.
     * @param f float to be output
     */
     
    public void println(float f) {
        outFile.println(f);
    }

    /**
     * Outputs a float to the file.
     * @param f float to be output
     */
     
    public void print(float f) {
        outFile.print(f);
    }

    /**
     * Outputs a double and newline to the file.
     * @param d double to be output
     */
     
    public void println(double d) {
        outFile.println(d);
    }

    /**
     * Outputs a double to the file.
     * @param d double to be output
     */
     
    public void print(double d) {
        outFile.print(d);
    }

    /**
     * Outputs a character and newline to the file.
     * @param c char to be output
     */
     
    public void println(char c) {
        outFile.println(c);
    }

    /**
     * Outputs a character to the file.
     * @param c char to be output
     */
     
    public void print(char c) {
        outFile.print(c);
    }

    /**
     * Outputs an object and newline to the file.
     * @param o object to be output
     */
     
    public void println(Object o) {
        outFile.println(o);
    }

    /**
     * Outputs an object to the file.
     * @param o object to be output
     */
     
    public void print(Object o) {
        outFile.print(o);
    }
    
    /**
     * Outputs a newline character to the file.
     */
     
    public void println() {
        outFile.println();
    }

    /**
     * Closes the file.
     */
     
    public void close() {
        outFile.flush();
        outFile.close();
    }
}
public class ObjectQueue
{
    private Object[] item;
    private int front;
    private int rear;
    private int size;
    
    public ObjectQueue(){
        size = 100;
        item = new Object[size];
        front = size-1;
        rear = size-1;
    }
    
    public ObjectQueue(int max){
        size = max;
        item = new Object[size];
        front = size-1;
        rear = size-1;
    }
    
    public boolean isEmpty(){
        return front == rear;
    }
    
    public boolean isFull(){
        return rear == size-1 ? front == 0 : front == rear+1;
    }
    
    public void clear(){
        front = size-1;
        rear = size-1;
    }
    
    public void insert(Object x){
        if (isFull()){
            System.out.println("Insert Runtime Error: Queue Overflow");
            System.exit(1);
        }
        if (rear == size-1)
            rear = 0;
        else
            rear++;
        item[rear] = x;
    }
    
    public Object remove(){
        if (isEmpty()){
            System.out.println("Remove Runtime Error: Queue Underflow");
            System.exit(1);
        }
        if (front == size-1)
            front = 0;
        else
            front++;
        return item[front];
    }
    
    public Object query(){
        if (isEmpty()){
            System.out.println("Query Runtime Error: Queue Underflow");
            System.exit(1);
        }
        if (front == size-1)
            return item[0];
        else
            return item[front+1];
    }

    
    

}
public class Process 
{
	//current queue
	private int queue = 0;
	
	//time to send to CPU/Core
	private int submit_time;
	
	//process id
	private int id;
	
	//time for process to be completed
	private int timer;
	
	//time process arrives for the first time into the CPU
	private int time_in;
	
	//time process is completed
	private int time_out;
	
	private LineWriter lw;
	
	//initialize queue, id, timer and submit_time variables
	public Process(String str, LineWriter l, int q)
	{
		lw = l;
		queue = q;
		
		String temp = "";
		id = 0;
		timer = 0;
		submit_time = 0;
		
		for (int c = 0; c < str.length(); c++)
		{
			while (str.charAt(c) == ' ')
			{
				c++;
			}
			
			while ((c < str.length()) && (str.charAt(c) != ' '))
			{
				temp = temp + str.charAt(c);
				c++;
			}
			
			if (submit_time == 0)
				submit_time = Integer.valueOf(temp);
			else if (id == 0)
				id = Integer.valueOf(temp);
			else if (timer == 0)
				 timer = Integer.valueOf(temp);
			
			temp = "";
		}
	}
	
	//decrease time left for process to be completed
	public int getTimer()
	{
		return timer;
	}
	
	//set new time for process to be completed
	public void setTimer()
	{
		timer--;
	}
	//returns process id
	public int getId()
	{
		return id;
	}
	
	//set current queue
	public void setQueue(int x)
	{
		queue = x;
	}
	
	//return current queue
	public int getQueue()
	{
		return queue;
	}
	
	//return time when the process is send to the CPU
	public int getSendTime()
	{
		return submit_time;
	}
	
	//records the machine time that the process was send to the CPU
	public void setTimeIn( int x)
	{
		time_in = x;
	}
	
	//set the machine time when the CPU releases a process
	public void setTimeOut(int x)
	{
		time_out = x;
	}
	
	//retunrs machine time when process is completed
	public int getTimeOut()
	{
		return time_out;
	}
	
	public void EndProcess()
	{
		lw.println(id + "          		" + queue + "          		" + time_in +   "		" + time_out );
		
		System.out.println(id + "          		" + queue + "          		" + time_in +   "		" + time_out );
	}
	
	public int getTimeIn()
	{
		return time_in;
	}
}