public bool FeedProcess(Process proc) { bool response = false; if (proc.Type == "Allocate") { ProcessAllocateEventArgs arg = new ProcessAllocateEventArgs(); response = AllocateProcess(proc, out arg); if (response == true) { OnAllocated(arg); } } else if (proc.Type == "DeAllocate") { ProcessDeAllocateEventArgs arg = new ProcessDeAllocateEventArgs(); response = DeAllocateProcess(proc, out arg); if (response == true) { OnDeAllocated(arg); } } return(false); }
public override bool AllocateProcess(Process proc, out ProcessAllocateEventArgs arg) { // Best fit algorithm from Bergmann's class BlockFit b, newSpace; List <BlockFit> viableSpaces = new List <BlockFit>(); int size = proc.MemoryInKB; newSpace = new BlockFit(); int start_position = 0; // adds all possible fitting blocks to list viableSpaces for (int i = 0; i < avail.Count; i++) { b = avail[i]; if (b.blockLength >= size) { viableSpaces.Add(b); } } // orders the blocks so index 0 is the best fit List <BlockFit> ordered = viableSpaces.OrderBy(f => f.blockLength).ToList(); // modifies the allocated and available lists for adding this new memory block if (viableSpaces.Count != 0) { for (int i = 0; i < viableSpaces.Count; i++) { BlockFit block_check = viableSpaces[i]; if (block_check.blockLength == ordered[0].blockLength) { // creates a new allocation block newSpace.start_pos = block_check.start_pos; newSpace.blockLength = size; newSpace.ID = proc.ID; allocated.Add(newSpace); //updates the available block block_check.start_pos = block_check.start_pos + size - 1; block_check.blockLength = block_check.blockLength - size; //updates for the start position for the display start_position = newSpace.start_pos; } } } // places into memory location arg = new ProcessAllocateEventArgs { ProcessID = proc.ID, ProcessName = proc.Name, BlockLength = size, StartBlock = start_position }; Processes.Add(proc); return(true); }
public abstract bool AllocateProcess(Process objProcess, out ProcessAllocateEventArgs arg);
public override bool AllocateProcess(Process proc, out ProcessAllocateEventArgs arg) { MemoryBlock b, newBlock; }
public override bool AllocateProcess(Process proc, out ProcessAllocateEventArgs arg) { //Find the power of process size e.g 65K = 2 ^ 7 int b = (int)Math.Ceiling(Math.Log(proc.MemoryInKB, 2)); //Find length of the block to allocate. 2 ^ 7 = 128K int blockLength = (int)Math.Pow(2, b); //Define start and ending index of where the block wil be places in Memory int startIndex = 0, endIndex = 0; //Divide memory until find the right block for (int i = MemorySize; i >= 0; i = i / 2) { if (i == blockLength) { //i - 1 because index start from 0 e.g. 128 will be 127 endIndex = i - 1; //start index will start from 0 startIndex = i - blockLength; //if any of the item between start and end index are assigned OR start index is not a multiple of block length //then keep incrementing start index while ((Memory[startIndex].IsAssigned || Memory[endIndex].IsAssigned) || (startIndex > 1 && startIndex % blockLength != 0)) { startIndex++; endIndex = startIndex + blockLength - 1; if (endIndex >= MemorySize) { break; } } //else quit the loop break; } } //this condition means algo could not find a space big enough to allocate the block if (Memory[startIndex].IsAssigned || Memory[endIndex].IsAssigned) { //"No space available to allocate"; arg = new ProcessAllocateEventArgs(); return(false); } //assigning all items between start and end index to the process for (int i = 0; i < MemorySize; i++) { if (i >= startIndex && i <= endIndex && Memory[i].IsAssigned == false) { Memory[i].ProcessId = proc.ID; Memory[i].IsAssigned = true; if (i == startIndex) { Memory[i].IsStart = true; } if (i == endIndex) { Memory[i].IsEnd = true; break; } } } // display message about where block has been placed in the memory arg = new ProcessAllocateEventArgs(); arg.ProcessID = proc.ID; arg.ProcessName = proc.Name; for (int k = 0; k < MemorySize; k++) { if (Memory[k].ProcessId == proc.ID && Memory[k].IsStart == true) { arg.StartBlock = k; arg.BlockLength = blockLength; } } Processes.Add(proc); return(true); }