vendredi 11 septembre 2015

Using Linked List into OSP 2 TaskCB

I am learning OSP 2 and how to use to implement method in the TaskCB, as well as an implementation of a Linked List. So I got my TaskCB class work fine, but when I create a Linked List it gives me errors such as

Possible cause: Failed to add/remove a thread to/from task

Image of output

Anybody can help me to point out what I am missing or any suggest would be great! Thank you!

Here is my TaskCB and Linked Like code so far.

import osp.FileSys.*;
import osp.Hardware.*;
import osp.IFLModules.*;
import osp.Memory.*;
import osp.Ports.*;
import osp.Threads.*;

/**
 * The student module dealing with the creation and killing of tasks. A task
 * acts primarily as a container for threads and as a holder of resources.
 * Execution is associated entirely with threads. The primary methods that the
 * student will implement are do_create(TaskCB) and do_kill(TaskCB). The student
 * can choose how to keep track of which threads are part of a task. In this
 * implementation, an array is used.
 * 
 * @OSPProject Tasks
 */
public class TaskCB extends IflTaskCB {
    /**
     * The task constructor. Must have
     * 
     * super();
     * 
     * as its first statement.
     * 
     * @OSPProject Tasks
     */

    private LinkedList<ThreadCB> threadList;
    private LinkedList<PortCB> portList;
    private LinkedList<OpenFile> openFilesTable;

    private static int virtureMemorySize = (int) Math.pow(2,
        MMU.getVirtualAddressBits());

    public TaskCB() {

        super();
    }

    /**
     * This method is called once at the beginning of the simulation. Can be
     * used to initialize static variables.
     * 
     * @OSPProject Tasks
     */
    public static void init() {
        // your code goes here

    }

    /**
     * Sets the properties of a new task, passed as an argument.
     * 
     * Creates a new thread list, sets TaskLive status and creation time,
     * creates and opens the task's swap file of the size equal to the size (in
     * bytes) of the addressable virtual memory.
     * 
     * @return task or null
     * 
     * @OSPProject Tasks
     */
    static public TaskCB do_create() {

        TaskCB theTask = new TaskCB();
        PageTable pagTable = new PageTable(theTask);
        theTask.setPageTable(pagTable);

        theTask.threadList = new LinkedList<ThreadCB>();
        theTask.portList = new LinkedList<PortCB>();
        theTask.openFilesTable = new LinkedList<OpenFile>();

        theTask.setCreationTime(HClock.get());
        theTask.setStatus(TaskLive);
        theTask.setPriority(0);

        FileSys.create(SwapDeviceMountPoint + theTask.getID(),
            virtureMemorySize);
        OpenFile swapFile = OpenFile.open(SwapDeviceMountPoint + theTask.getID(),
            theTask);

        if ( swapFile == null ) {
            ThreadCB.dispatch();
//            theTask.do_kill();
            return null;
        }

        theTask.setSwapFile(swapFile);
        ThreadCB.create(theTask);

        return theTask;

    }

    /**
     * Kills the specified task and all of it threads.
     * 
     * Sets the status TaskTerm, frees all memory frames (reserved frames may
     * not be unreserved, but must be marked free), deletes the task's swap
     * file.
     * 
     * @OSPProject Tasks
     */
    public void do_kill() {

        for ( int i = threadList.size() - 1; i >= 0; i-- ) {
            ((ThreadCB) this.threadList.get(i)).kill();
        }

        for ( int i = this.portList.size() - 1; i >= 0; i-- ) {
            ((PortCB) this.portList.get(i)).destroy();
        }

        this.setStatus(TaskTerm);
        this.getPageTable().deallocateMemory();

        for ( int i = openFilesTable.size() - 1; i >= 0; i-- ) {
            OpenFile swapFile = (OpenFile) openFilesTable.get(i);
            if ( swapFile != this.getSwapFile() ) {
                swapFile.close();
            }
        }

        this.getSwapFile().close();
        FileSys.delete(SwapDeviceMountPoint + this.getID());

    }

    /**
     * Returns a count of the number of threads in this task.
     * 
     * @OSPProject Tasks
     */
    public int do_getThreadCount() {
        return threadList.size();
    }

    /**
     * Adds the specified thread to this task.
     * 
     * @return FAILURE, if the number of threads exceeds MaxThreadsPerTask;
     *         SUCCESS otherwise.
     * 
     * @OSPProject Tasks
     */
    public int do_addThread(ThreadCB thread) {
        if ( threadList.size() >= ThreadCB.MaxThreadsPerTask ) {
            return TaskCB.FAILURE;
        }
        threadList.addFirst(thread);
        return TaskCB.SUCCESS;
    }

    /**
     * Removes the specified thread from this task.
     * 
     * @OSPProject Tasks
     */
    public int do_removeThread(ThreadCB thread) {
        if ( this.threadList.contains(thread) ) {
            this.threadList.remove(thread);
            return TaskCB.SUCCESS;
        }
        else
            return TaskCB.FAILURE;
    }

    /**
     * Return number of ports currently owned by this task.
     * 
     * @OSPProject Tasks
     */
    public int do_getPortCount() {
        return portList.size();
    }

    /**
     * Add the port to the list of ports owned by this task.
     * 
     * @OSPProject Tasks
     */
    public int do_addPort(PortCB newPort) {
        if ( portList.size() >= PortCB.MaxPortsPerTask ) {
            return TaskCB.FAILURE;
        }
        portList.addFirst(newPort);
        return TaskCB.SUCCESS;
    }

    /**
     * Remove the port from the list of ports owned by this task.
     * 
     * @OSPProject Tasks
     */
    public int do_removePort(PortCB oldPort) {
        if ( this.portList.contains(oldPort) ) {
            portList.remove(oldPort);
            return TaskCB.SUCCESS;
        }

        return TaskCB.FAILURE;
    }

    /**
     * Insert file into the open files table of the task.
     * 
     * @OSPProject Tasks
     */
    public void do_addFile(OpenFile file) {
        this.openFilesTable.addLast(file);

    }

    /**
     * Remove file from the task's open files table.
     * 
     * @OSPProject Tasks
     */
    public int do_removeFile(OpenFile file) {
        if ( file.getTask() != this )
            return TaskCB.FAILURE;
        openFilesTable.remove(file);
        return TaskCB.SUCCESS;
    }




import java.util.*;

public class LinkedList<V> implements Iterable<V>
{
   private Node<V> head;
   private int count = 0;

 /**
   *  Constructs an empty list
   */
   public LinkedList()
   {
      head = null;
   }
 /**
   *  Returns true if the list is empty
   *
   */
   public boolean isEmpty()
   {
      return head == null;
   }

   public int size() {
       return count;
   }
 /**
   *  Inserts a new node at the beginning of this list.
   *
   */
   public void addFirst(V item)
   {
      head = new Node<V>(item, head);
   }
 /**
   *  Returns the first element in the list.
   *
   */
   public V getFirst()
   {
      if(head == null) throw new NoSuchElementException();

      return head.data;
   }
 /**
   *  Removes the first element in the list.
   *
   */
   public V removeFirst()
   {
      V tmp = getFirst();
      head = head.next;
      return tmp;
   }
 /**
   *  Inserts a new node to the end of this list.
   *
   */
   public void addLast(V item)
   {
      if( head == null)
         addFirst(item);
      else
      {
         Node<V> tmp = head;
         while(tmp.next != null) tmp = tmp.next;

         tmp.next = new Node<V>(item, null);
      }
   }
 /**
   *  Returns the last element in the list.
   *
   */
   public V getLast()
   {
      if(head == null) throw new NoSuchElementException();

      Node<V> tmp = head;
      while(tmp.next != null) tmp = tmp.next;

      return tmp.data;
   }
 /**
   *  Removes all nodes from the list.
   *
   */
   public void clear()
   {
      head = null;
   }
 /**
   *  Returns true if this list contains the specified element.
   *
   */
   public boolean contains(V x)
   {
      for(V tmp : this)
         if(tmp.equals(x)) return true;

      return false;
   }
 /**
   *  Returns the data at the specified position in the list.
   *
   */
   public V get(int pos)
   {
      if (head == null) throw new IndexOutOfBoundsException();

      Node<V> tmp = head;
      for (int k = 0; k < pos; k++) tmp = tmp.next;

      if( tmp == null) throw new IndexOutOfBoundsException();

      return tmp.data;
   }
 /**
   *  Returns a string representation
   *
   */
   public String toString()
   {
      StringBuffer result = new StringBuffer();
      for(Object x : this)
        result.append(x + " ");

      return result.toString();
   }
 /**
   *  Inserts a new node after a node containing the key.
   *
   */
   public void insertAfter(V key, V toInsert)
   {
      Node<V> tmp = head;

      while(tmp != null && !tmp.data.equals(key)) tmp = tmp.next;

      if(tmp != null)
         tmp.next = new Node<V>(toInsert, tmp.next);
   }
 /**
   *  Inserts a new node before a node containing the key.
   *
   */
   public void insertBefore(V key, V toInsert)
   {
      if(head == null) return;

      if(head.data.equals(key))
      {
         addFirst(toInsert);
         return;
      }

      Node<V> prev = null;
      Node<V> cur = head;

      while(cur != null && !cur.data.equals(key))
      {
         prev = cur;
         cur = cur.next;
      }
      //insert between cur and prev
      if(cur != null)
         prev.next = new Node<V>(toInsert, cur);
   }
 /**
   *  Removes the first occurrence of the specified element in this list.
   *
   */
   public void remove(V key)
   {
      if(head == null)
         throw new RuntimeException("cannot delete");

      if( head.data.equals(key) )
      {
         head = head.next;
         return;
      }

      Node<V> cur  = head;
      Node<V> prev = null;

      while(cur != null && !cur.data.equals(key) )
      {
         prev = cur;
         cur = cur.next;
      }

      if(cur == null)
         throw new RuntimeException("cannot delete");

      //delete cur node
      prev.next = cur.next;
   }

 /*******************************************************
 *
 *  The Node class
 *
 ********************************************************/
   private static class Node<AnyType>
   {
      private AnyType data;
      private Node<AnyType> next;

      public Node(AnyType data, Node<AnyType> next)
      {
         this.data = data;
         this.next = next;
      }
   }

 /*******************************************************
 *
 *  The Iterator class
 *
 ********************************************************/

   public Iterator<V> iterator()
   {
      return new LinkedListIterator();
   }

   private class LinkedListIterator  implements Iterator<V>
   {
      private Node<V> nextNode;

      public LinkedListIterator()
      {
         nextNode = head;
      }

      public boolean hasNext()
      {
         return nextNode != null;
      }

      public V next()
      {
         if (!hasNext()) throw new NoSuchElementException();
         V res = nextNode.data;
         nextNode = nextNode.next;
         return res;
      }

      public void remove() { throw new UnsupportedOperationException(); }
   }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire