Need help for timeslicing

Discussion in 'Java' started by husain_14, Nov 10, 2007.

  1. husain_14

    husain_14 New Member

    Joined:
    Nov 9, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello everyone. Ill just give you a brief description of the problem. I need to generate an application that works on a 2 state Operating System model - the 2 states being Ready state and Running state. In a multi processing system, processes that are being executed are given time slices and once their time slice is up (no matter how much they have been executed) they go and wait for their next turn to come. This scheduling is mostly done through the round robin method. So putting the above mentioned states in the picture, we have the Ready state which is a normal queue and a Running state which hold the process that is actually being executed by the processor at the moment. After its executed the process goes to the back of the queue and waits for its turn in a simple FIFO practice.

    Now onto my problem. Ive implemented the queues through linked lists function and have a created constructors for objects(objects will act as processes in my model instead of actual process themselves). The objects contain within themselves a few values. One thing that I havent added yet to the values is the total execution time duration of the object which will taken as user input. Another input to be taken at the start of the program will be the time slice period which will be uniform for all. So lets say an object has a total time of 20s and the time slice is 2s so the object will require ten passes through the running state to completely execute.

    For me to implement the time slicing, I needed to know about timers and timestamps - a subject on which I have zero info. So hence I ask you for help. If somebody can implement the time slicing function and add it is an option in the menu that would be great. For example 'To start multiprogramming press 6' this option would start moving the objects through the imaginary running state and once completed move it back into the ready queue and so on. My code on which I have worked uptil now is attached below. os2 is the main class, pcb is the object class and the linkedlist and queue classes are there to implement the queueing functions.

    If any confusions dont hesitate to ask. Since I sort of need this in a hurry hence Ill be checking this forum almost every hour for the next 20 hours atleast. Thanks in advance guys. :D
     
  2. husain_14

    husain_14 New Member

    Joined:
    Nov 9, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    The files..

    The files that Ive worked on till now... Im using jdk1.5!

    Code:
    public class LinkedList
    {
    	// reference to the head node.
    	private Node head;
    	private int listCount;
    	
    	// LinkedList constructor
    	public LinkedList()
    	{
    		// this is an empty list, so the reference to the head node
    		// is set to a new node with no data
    		head = new Node(null);
    		listCount = 0;
    	}
    	
    	public void add(Object data)
    	// post: appends the specified element to the end of this list.
    	{
    		Node temp = new Node(data);
    		Node current = head;
    		// starting at the head node, crawl to the end of the list
    		while(current.getNext() != null)
    		{
    			current = current.getNext();
    		}
    		// the last node's "next" reference set to our new node
    		current.setNext(temp);
    		listCount++;// increment the number of elements variable
    	}
    	
    	public void add(Object data, int index)
    	// post: inserts the specified element at the specified position in this list.
    	{
    		Node temp = new Node(data);
    		Node current = head;
    		// crawl to the requested index or the last element in the list,
    		// whichever comes first
    		for(int i = 1; i < index && current.getNext() != null; i++)
    		{
    			current = current.getNext();
    		}
    		// set the new node's next-node reference to this node's next-node reference
    		temp.setNext(current.getNext());
    		// now set this node's next-node reference to the new node
    		current.setNext(temp);
    		listCount++;// increment the number of elements variable
    	}
    	
    	public Object get(int index)
    	// post: returns the element at the specified position in this list.
    	{
    		// index must be 1 or higher
    		if(index <= 0)
    			return null;
    		
    		Node current = head.getNext();
    		for(int i = 1; i < index; i++)
    		{
    			if(current.getNext() == null)
    				return null;
    			
    			current = current.getNext();
    		}
    		return current.getData();
    	}
    	
    	public boolean remove(int index)
    	// post: removes the element at the specified position in this list.
    	{
    		// if the index is out of range, exit
    		if(index < 1 || index > size())
    			return false;
    		
    		Node current = head;
    		for(int i = 1; i < index; i++)
    		{
    			if(current.getNext() == null)
    				return false;
    			
    			current = current.getNext();
    		}
    		current.setNext(current.getNext().getNext());
    		listCount--; // decrement the number of elements variable
    		return true;
    	}
    	
    	public int size()
    	// post: returns the number of elements in this list.
    	{
    		return listCount;
    	}
    	
    	public String toString()
    	{
    		Node current = head.getNext();
    		String output = "";
    		while(current != null)
    		{
    			output += "[" + current.getData().toString() + "]";
    			current = current.getNext();
    		}
    		return output;
    	}
    	
    
    
    	
     	 
    
    
    	private class Node
    	{
    		// reference to the next node in the chain,
    		// or null if there isn't one.
    		Node next;
    		// data carried by this node.
    		// could be of any type you need.
    		Object data;
    		
    
    		// Node constructor
    		public Node(Object _data)
    		{
    			next = null;
    			data = _data;
    		}
    		
    		// another Node constructor if we want to
    		// specify the node to point to.
    		public Node(Object _data, Node _next)
    		{
    			next = _next;
    			data = _data;
    		}
    		
    		// these methods should be self-explanatory
    		public Object getData()
    		{
    			return data;
    		}
    		
    		public void setData(Object _data)
    		{
    			data = _data;
    		}
    		
    		public Node getNext()
    		{
    			return next;
    		}
    		
    		public void setNext(Node _next)
    		{
    			next = _next;
    		}
    
    
    
    	}
    	public void dataPrint() {  
    	 for (int i = 1; i <= size(); i++) {
          System.out.print("");
        System.out.println(get(i));
    
    	    }
      	}
    
    }
    Code:
    public class Queue {
    	private LinkedList list;
    	
    	// Queue constructor
    	public Queue()
    	{
    		// Create a new LinkedList.
    		list = new LinkedList();
    	}
    
    	public boolean isEmpty()
    	// Post: Returns true if the queue is empty. Otherwise, false.
    	{
    		return (list.size() == 0);
    	}
    
    	public void enqueue(Object item)
    	// Post: An item is added to the back of the queue.
    	{
    		// Append the item to the end of our linked list.
    		list.add(item);
    	}
    
    	public Object dequeue()
    	// Pre: this.isEmpty() == false
    	// Post: The item at the front of the queue is returned and 
    	//         deleted from the queue. Returns null if precondition
    	//         not met.
    	{
    		// Store a reference to the item at the front of the queue
    		//   so that it does not get garbage collected when we 
    		//   remove it from the list.
    		// Note: list.get(...) returns null if item not found at
    		//   specified index. See postcondition.
    		Object item = list.get(1);
    		// Remove the item from the list.
    		// My implementation of the linked list is based on the
    		//   J2SE API reference. In both, elements start at 1,
    		//   unlike arrays which start at 0.
    		list.remove(1);
    		
    		// Return the item
    		return item;
    	}
    
    
    
    	public Object print1()
    	{
    	return list.get(1);
    	}
    
    
    
    		public Object print2()
    	{
    	return list.get(2);
    	}
    
    		public Object print3()
    	{
    	return list.get(3);
    	}
    
    	
    	public void displaya()
    	{
    	list.dataPrint();
    	}
    
    	public Object exit1()
    	{
    	return list.get(1);
    	}
    
    
    
    
    
    
    	public Object peek()
    	// Pre: this.isEmpty() == false
    	// Post: The item at the front of the queue is returned and 
    	//         deleted from the queue. Returns null if precondition
    	//         not met.
    	{
    		// This method is very similar to dequeue().
    		// See Queue.dequeue() for comments.
    		return list.get(1);
    	}
    }
    Code:
    public class pcb{
    
           
    
    
    int id;
    String pname;
    
    
    pcb(int theid, String thepname)
    {
    
    id = theid;
    pname = thepname;
    
    
    
    
    		}
    
    String display() 
    {
    System.out.print("ID= "+ id +"  " + "process name:  ");
    return pname;
    }
    
    
    public String toString(){
    return ("ID= "+ id +"  " + "process name:  " + pname);
    }
    
    
    	
    
    
    }
    Code:
    import java.util.*;
    public class os2{
    
           public static void main(String[] args){
    
    int input;
    String mname;
    int mid=1;
    
    Queue q = new Queue();
    
    Queue eq = new Queue();
    
    
    
    
    for(int count = 0; count >=0; count++)
    
    {
    
    	System.out.println("");
    	System.out.println("");
           System.out.println("Please select any of the following 4 options");
    
           System.out.println("type 1 for 'creating a process'  ");
    
           System.out.println("type 2 for 'killing a process'  ");
    
           System.out.println("type 3 for 'displaying all current running processes' ");
    
           System.out.println("type 4 for 'displayin those processes which have been exited' ");
    
           System.out.println("type 5 for EXIT ");
    
    	
    
    	System.out.println("");
    	System.out.println("***********************************************************");
    
    input = SavitchIn.readInt();
      
    
          switch (input){
             case 1:
    		 System.out.println("CREATING A PROCESS"); 
    
    
    
    	System.out.println("please enter the name of the process you want to create!");
    	System.out.print("input is=  "); 
    
    	mname = SavitchIn.readLine();
    	
    	pcb process1;
    	process1=new pcb(mid,mname);
    	
    	//System.out.println(process1.display());
    	//process1.display();
    
    	q.enqueue(process1);
    	mid=mid+1;
    
             	break;
             case 2: 
    		System.out.println("KILLING A PROCESS"); 
    		System.out.println("");
    		System.out.println("Now the process on the top of the list will be killed! :)");
    		System.out.println("");
    		//System.out.println("KIlled: " + ((pcb)q.peek()).display());
    
    		
    		eq.enqueue((pcb)q.peek());
    
    		q.dequeue();
    		
    			
    	    	break;
    
    
    
    
             case 3: 
    		System.out.println("DISPLAYING ALL CURRENT PROCESSES"); 
    		q.displaya();
    		break;
    	
    
    	case 4:  
    		System.out.println("DISPLAYING ALL exited PROCESSES"); 
    
    		eq.displaya();
    		
             	break;       
    
    
    	
     	case 5:
    	
    	 System.out.println("bye bye!"); 
    
    	System.exit(0);
    	break;
    	
    	 default:
    	
    	 System.out.println("told ya enter numbers from 1 to 4 only! stupid"); 
    
    	
    	break;
    
    
    
          }
    	}
    
    }
    
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice