Question
I need to create a Java program that simulates the process and memory management of an operating system. The memory management algorithm should be passed
I need to create a Java program that simulates the process and memory management of an operating system. The memory management algorithm should be passed a parameter to be able to handle First fit, Best Fit and Worst fit ,
OpenMem.java - finds open memory assign main memory, i.e. RAM to be 256
Process Management Overview
Process Management in operating systems is responsible for deciding which processes will get processor time (CPU) and for how long. Some of the functions include: keeping track of the status for all active processes (ready, waiting, running) deciding which process get processor time setting order of execution for the processes that are in the Ready state (QReady) and the Waiting state (QWaiting) allocation and de-allocation of a processor to a process allocation and de-allocation of memory to a process keeping track of the status of open and used memory Processes are active once they enter the operating system and have 3 states (ready, waiting, running) until completion. Once completed the process is terminated and is no longer active. Once a process that is ready to run, it's memory request is checked to be available, i.e. Open memory can be assigned to the process. If no open memory is available, then the process cannot run and is place back to the end of QReady. Key Point: When a process gets context switched (STATE: Running => Ready), it's allocated memory stays with it, i.e. it is NOT freed. Memory that is allocated to a process is only freed when the process terminates (STATE: Running=>Terminated). A running process will stop running on a CPU event, which could be for an I/O block, paging block, interrupt, process completion or other. If the process is not completed, then transition STATE: Running => Ready |
This project will develop the functionality to manage the process states for Ready, Waiting, and Running based on CPU events |
Base code is as follows:
//PCB.java
import java.util.Random ;
public class PCB implements Comparable
PCB__K += 1 ; //=====> Increment the static variable for Process ID PCB_state = "Ready" ; PCB_ID = PCB__K ; pgmCounter = "" ; CPU_used = 0 ; CPU_max = random__X.nextInt(50) + 1 ; // Assign max to be between 1:50 timeWaiting = 0 ; memBase = 0 ; memLimit = random__X.nextInt(50) + 26 ; // Assign memory needed between 25:75 } // constructor for memory tracking public PCB (int m0) { PCB__K = 0 ; PCB_state = "@" ; PCB_ID = 0 ; pgmCounter = "" ; CPU_used = 0 ; CPU_max = 0 ; timeWaiting = 0 ; memBase = 0 ; memLimit = m0 ; } public String showPCB() { return "state: " + PCB_state + "\tID: " + Integer.toString(PCB_ID) // // + "\tK: " + pgmCounter // + "\tCPU used: " + Integer.toString(CPU_used) // + "\tCPU max: " + Integer.toString(CPU_max) // + "\tWait: " + Integer.toString(timeWaiting) + "\tmemBase: " + Integer.toString(memBase) + "\tmemLimit: "+ Integer.toString(memLimit) ; }
public int compareTo (PCB pcb0) { int mem0 = pcb0.get_memBase(); return this.memBase - mem0 ; } // set methods public String get_state() { return PCB_state; } public int get_ID() { return PCB_ID; } public int get_CPU_used() { return CPU_used; } public int get_CPU_max() { return CPU_max; } public String get_pgmCounter() { return pgmCounter; } public int get_timeWaiting() { return timeWaiting; } public int get_memBase() { return memBase; } public int get_Limit() { return memLimit; } // Set methods , void returns no value
public void set_PCB_ID (int id0) { PCB_ID = id0 ; } public void set_state(String state0) { PCB_state = state0 ; } public void set_CPU_used(int CPU0) { CPU_used = CPU0 ; }
public void add_CPU_used(int c0) { CPU_used += c0 ; } public void set_CPU_max(int CPU0) { CPU_max = CPU0 ; } public void set_pgmCounter(String pgmCounter0) { pgmCounter = pgmCounter0 ; } public void set_timeWaiting(int timeWaiting0) { timeWaiting = timeWaiting0 ; } public void add_timeWaiting(int t0) { timeWaiting += t0 ; } public void set_memBase (int m0) { memBase = m0 ; } public void set_memLimit (int m0) { memLimit = m0 ; }
}
//*****************************************************
//processmanagement.java
import java.util.LinkedList ; import java.util.Random ;
public class ProcessMgmt { public static void main(String args[]) { //#010 Initialization of fields and data structures /////////////// int QREADY__T = 25 ; final int BLOCKIO = 3 ; final int INTERRUPT = 2 ; final int COMPLETED = 1 ; final int BLOCKPAGE = 0 ; Random random__X = new Random(); CPU_event event = new CPU_event(); int CPU_runtime ; int event__X ; LinkedList
/////////////////////////////////////////////////////////////////////// //#080 ===> end of Initialization System.out.println(" *****\t\t\tReady Queue\t\t\t*****"); for (PCB pcbLoop : QReady) //#090 Loop that executes based on the no. of nodes in the LL System.out.println(pcbLoop.showPCB()); //#095 Print the PCB for an LL node //#0100 Process until the active processes are all completed /////// while ( QReady.size() > 0 ) { //#0110 Next process to Run PCB_Running = QReady.removeFirst(); //#0140 Assign the first node from QReady to Running PCB_Running.set_state("Running") ; //#0145 Set the state value to "Running" CPU_runtime = random__X.nextInt(20) + 1 ; //#0150 Get a random no. between 0 and 20 //#0160 Tally and set the CPU used for the process PCB_Running.add_CPU_used(CPU_runtime) ;
System.out.println(" *****\t\t\tRunning Process\t\t\t*****"); System.out.println(PCB_Running.showPCB());
//#0180 Tally and set the wait times for the processes in QReady for (PCB pcbLoop : QReady) { pcbLoop.add_timeWaiting(CPU_runtime); } //#0190 Check if the process completed (CPU used exceeds the CPU max) if (PCB_Running.get_CPU_used() > PCB_Running.get_CPU_max()) { PCB_Running.set_state("Completed"); System.out.println(" >>>>>\tProcess\t" + PCB_Running.showPCB() + "\t<<<<<" ); continue; // iterate to the next in the WHILE loop } //----------------------------------------------------------------- // Process did not complete, so continue //----------------------------------------------------------------- //#0200 Simulate the type of Block on the Process (I/O Block, Memory Paging Block, Interrupt) event__X = event.get_CPU_event() ; if (event__X == COMPLETED) { PCB_Running.set_state("Completed"); System.out.println(" >>>>>\tProcess\t" + PCB_Running.showPCB() + "\t<<<<<" ); } else { PCB_Running.set_state("Ready"); //#230 Set the state to "Ready" from "Running" switch (event__X) { case INTERRUPT : { //#240 Add the PCB to the START of the QReady System.out.println("*****\t\t\tContext Switch\tINTERRUPT event\t\t\t*****"); break; } case BLOCKPAGE : { //#242 Add to the top 3rd of QReady System.out.println("*****\t\t\tContext Switch\tPAGE event\t\t\t*****"); break; } case BLOCKIO : { //#244 Add the PCB to the middle of QReady System.out.println("*****\t\t\tContext Switch\tI/O event\t\t\t*****"); break; } default : { System.out.println("*****\t\t\tContext Switch\tTIME event\t\t\t*****"); break; } } } System.out.println(" *****\t\t\tContext Switch\tReady Queue\t\t\t*****"); for (PCB pcbLoop : QReady) System.out.println(pcbLoop.showPCB()); } } }
//****************************************************
//Openmem.java
import java.util.LinkedList;
public class OpenMem {
public OpenMem () {} public boolean findOpenMem ( PCB PCB_Ready ,LinkedList
//@0240 replace the open memory
//@0260 allocate the used memory @@ QMemUsed @@ //@0280 set the base for the process //@0300 push the used memory System.out.printf("@0300 Used\t%s " ,memUsed.showPCB()); break ; // exit out of the FOR loop for memory } } return memFound__B ; } }
//**************************************************
Driver program
import java.util.Collections; import java.util.LinkedList ; import java.util.Random ;
public class MemoryManager { public static void main(String args[]) { Random random__X = new Random(); int QREADY__T = 10 ; final int mem__T = 256; LinkedList
for (int ii = 0; ii < QREADY__T; ii++) { PCB pcb0 = new PCB(); //#0050 new PCB STATE:New pcb0.set_state("Ready"); //#0060 set PCB STATE:Ready QReady.add(pcb0) ; //#0070 Add PCB object onto QReady } for (PCB loopI : QReady) System.out.printf("%s " ,loopI.showPCB()) ; // End of Initialization \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ //#0100 Iterator Interface \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ OpenMem om = new OpenMem(); //#0120 create the iterator for QReady java.util.Iterator
// add the memory back to the QMemOpen QMemOpen.add(PCB_Done); for (PCB loopI : QMemOpen) System.out.printf("@0420 Open\t%s " ,loopI.showPCB()); for (PCB loopI : QMemUsed) System.out.printf("@0440 Used\t%s " ,loopI.showPCB()); System.out.println("====="); } //#0500 Sort the QMemOpen Collections.sort(QMemOpen); for (PCB loopI : QMemOpen) { System.out.printf("@0500 sorted\t%s ", loopI.showPCB()) ; } //#0600 Defragmentation processing } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started