Segmented Virtual Memory Simulation Must match exact ouput R

Segmented Virtual Memory Simulation

Must match exact ouput

Ran program with First fit and Best fit for inserting an element

Cannot have two holes next to each other

Solution

import java.util.*;

import java.io.*;

class Node {

    boolean segment;     

    int     location;      

    int     size;          

    int     timeToDepart;  

    Node    next;

    Node (int locn, int sz, int endOfLife, Node nxt) {

       segment      = true;

        location     = locn;

        size         = sz;

        timeToDepart = endOfLife;

        next         = nxt;

    }

    Node (int locn, int sz, Node nxt) {

        segment     = false;

        location    = locn;

        size        = sz;

        next        = nxt;

    }

}

class Memory {

    private static int memorySize;     Node head;                     

    Node lastPlacement;            

    Memory (int size) {

            memorySize = size;

            Node n = new Node (0, memorySize, null);

            lastPlacement = head = n;

    }

    boolean place (int size, int timeOfDay, int lifeTime, boolean verbose) {

        if (isEmpty()) {

            Node current = new Node (0, size, 0, null);

            lastPlacement = head = current;

            return true;

        }

        else {

            Node current = lastPlacement; //We start searching for a hole at our lastPlacement

            while (current != null) {

                if (!current.segment && current.timeToDepart <= timeOfDay) {

                    if (current.size >= size) {

                        Node n = new Node (current.location, size, timeOfDay + lifeTime, current.next);

                        current.next = n;

                        current.size = current.size - size;

                        lastPlacement = current = n;

                        if (verbose) {

                            System.out.println (\"Segment at location: \" + lastPlacement.location + \"\\twith size: \" + lastPlacement.size + \"\\tdeparts at: \" + lastPlacement.timeToDepart);

                        }

                        return true;

                    }

                }

                current = current.next;

            }

            current = this.head;

            while (current != lastPlacement) {

                if (!current.segment && current.timeToDepart <= timeOfDay) {

                    if (current.size >= size) {

                        Node n = new Node (current.location + size, size, timeOfDay + lifeTime, current.next);

                        current.next = n;

                        current.size = current.size - size;

                        lastPlacement = current = n;

                        if (verbose) {

                            System.out.println (\"Segment at location: \" + lastPlacement.location + \"\\twith size: \" + lastPlacement.size + \"\\tdeparts at: \" + lastPlacement.timeToDepart);

                        }

                        return true;

                    }

                }

                current = current.next;

            }

        }

        return false;

    }

    void removeSegmentsDueToDepart (int timeOfDay) {

        if ((head.segment == true) && (head.timeToDepart <= timeOfDay)) {

            head.segment = false; // Allow node to become a hole

        }

        Node previous = head;

        while (previous.next != null) {

            Node current = previous.next;

            if ((current.segment == true) && (current.timeToDepart <= timeOfDay)) {

                current.segment = false;

            }

            if ((previous.segment == false) && (current.segment == false)) {

                if (current == lastPlacement) {

                    lastPlacement = previous;

                }

                previous.size += current.size;

                previous.next = current.next;

            }

            else {

                previous = current;

            }

        }

    }

    void printLayout() {

        Node current = head;

        int numNodes = 0;

        while (current != null) {

            numNodes++;

            current = current.next;

        }

        Node [] nodeArray = new Node [numNodes];

        int y = 0;

        current = head;

        while (current != null) {

            nodeArray[y] = current;

            y++;

            current = current.next;

        }

        nodeArray = sort (nodeArray, numNodes);

        for (int i = 0; i < numNodes; i++) {

            System.out.println (nodeArray[i].location + \"\\t\" + nodeArray[i].size + \"\\t\" + nodeArray[i].timeToDepart);

        }

    }

    Node [] sort (Node [] node, int length)

    {

        Node tempNode;

        for (int i = 0; i < ( length - 1 ); i++) {

            for (int j = 0; j < length - i - 1; j++) {

              if (node[j].location > node[j + 1].location)

              {

                tempNode    = node[j];

                node[j]     = node[j + 1];

                node[j + 1] = tempNode;

              }

            }

          }

        return node;

    }

    public boolean isEmpty () {

        if (head == null) {

            return true;

        }

        return false;

    }

}

public class EVBEP1 {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner sc = new Scanner(new File(\"p115sd5.txt\"));

        String line = \"\";

        boolean done = false;

        Memory memory = new Memory(0);

        int timeOfDay = 0;     

        int placements = 0;    

        long totalSpaceTime = 0;

        float meanOccupancy = 0;

        while (!done) {

            line = sc.nextLine();  

            String [] tokens = line.split(\" \");

            switch (tokens[0]) {

            case \"N\": {

                    System.out.println(\"Evan Clay Bechtol\");

                    break;

                }

            case \"C\": {

                    memory = new Memory(Integer.parseInt(tokens[1]));

                    break;

                }

            case \"E\": {

                    System.out.println();

                    done = true;   

                    break;

                }

            case \"A\": {

                    int size = Integer.parseInt(tokens[1]);

                    int lifeTime = Integer.parseInt(tokens[2]);

                    timeOfDay++;

                    memory.removeSegmentsDueToDepart(timeOfDay);

                    while (!memory.place(size, timeOfDay, lifeTime, true)) {

                        timeOfDay++;

                        memory.removeSegmentsDueToDepart(timeOfDay);

                        }

                    placements++;

                    System.out.printf(\"Segment of size\\t%4d\", size);

                    System.out.printf(\" placed at time\\t%4d\", timeOfDay);

                    System.out.printf(\" at location\\t%4d\", memory.lastPlacement.location);

                    System.out.printf(\" departs at\\t%4d\", memory.lastPlacement.timeToDepart);

                    break;

                }

            case \"P\": {

                    System.out.println ();

                    memory.printLayout();

                    break;

                }

            case \"R\": {

                    int size = Integer.parseInt(tokens[1]);

                    memory = new Memory(size);

                    int minSegSize = Integer.parseInt(tokens[2]);  

                    int maxSegSize = Integer.parseInt(tokens[3]);  

                    int maxLifeTime = Integer.parseInt(tokens[4]);

                    int numSegs = Integer.parseInt(tokens[5]);     

                    timeOfDay = 0;

                    placements = 0;

                    Random ran = new Random ();

                    while (placements < numSegs) {

                        timeOfDay++;

                        memory.removeSegmentsDueToDepart(timeOfDay);

                        int newSegSize = minSegSize + ran.nextInt(maxSegSize - minSegSize + 1);

                        int newSegLifetime = 1 + ran.nextInt(maxLifeTime);

                        totalSpaceTime += newSegSize * newSegLifetime;

                        while (!memory.place(newSegSize, timeOfDay, newSegLifetime, false)) {

                            timeOfDay++;

                            memory.removeSegmentsDueToDepart(timeOfDay);

                        }

                        placements++;

                    }

                    meanOccupancy = totalSpaceTime / (timeOfDay);

                    System.out.printf (\"Number of placements made = %6d\ \", placements);

                    System.out.printf (\"Mean occupancy of memory = %8.2f\ \", meanOccupancy);

                }

            }

        }

        sc.close();

    }

}

Segmented Virtual Memory Simulation Must match exact ouput Ran program with First fit and Best fit for inserting an element Cannot have two holes next to each o
Segmented Virtual Memory Simulation Must match exact ouput Ran program with First fit and Best fit for inserting an element Cannot have two holes next to each o
Segmented Virtual Memory Simulation Must match exact ouput Ran program with First fit and Best fit for inserting an element Cannot have two holes next to each o
Segmented Virtual Memory Simulation Must match exact ouput Ran program with First fit and Best fit for inserting an element Cannot have two holes next to each o
Segmented Virtual Memory Simulation Must match exact ouput Ran program with First fit and Best fit for inserting an element Cannot have two holes next to each o
Segmented Virtual Memory Simulation Must match exact ouput Ran program with First fit and Best fit for inserting an element Cannot have two holes next to each o
Segmented Virtual Memory Simulation Must match exact ouput Ran program with First fit and Best fit for inserting an element Cannot have two holes next to each o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site