/**************************************************************** * * This method handles the next command in the pcb. It loads the * process into a new page table, then it loads it into memory. * If it is a halt command, it removes the process from memory * and the page tablefrom the list of page tables. * * Returns a -1 if it is at the end of the pcb otherwise it returns * the number of commands left. * ***************************************************************/ public int Next() { if (pcb.Count < 1) { return(-1); } // remove the next command and place into c Command c = pcb[0]; pcb.RemoveAt(0); //previousPcb.Insert(0, pcb[0]); //previousFreeFrames.Insert(0, freeFrames); // check if it is a halt command or not if (c.TextSize >= 0) { //previousPageTables.Insert(0, pageTable); //previousMemory.Insert(0, memory); // create a new page table for the new process PageTable pt = new PageTable(c); pt.SetPages(); pageTable.Add(pt); // load the page into memory LoadIntoMemory(pageTable.Last()); } else // it was a halt command { // loops through all memory frames and if it contains the process // that is being halted, remove it from memory for (int x = 0; x <= 7; x++) { if (memory[x] != null && memory[x].ProcessID == c.ProcessID) { memory[x] = null; // add that frame to the free frames list freeFrames.Add(x); } } // loops through page tables and remove the page table that is for the // halting process for (int i = pageTable.Count - 1; i >= 0; i--) { if (pageTable[i].ProcessID == c.ProcessID) { pageTable.Remove(pageTable[i]); } } } return(pcb.Count); }
//public int Previous() //{ // pcb.Insert(0, previousPcb[0]); // previousPcb.RemoveAt(0); // memory = previousMemory[0]; // previousMemory.RemoveAt(0); // pageTable = previousPageTables[0]; // previousPageTables.RemoveAt(0); // freeFrames = previousFreeFrames[0]; // previousFreeFrames.RemoveAt(0); // return previousPcb.Count; //} /***************************************************************** * * This method takes a page table and loads it into memory. * ****************************************************************/ private void LoadIntoMemory(PageTable pt) { MFrame mf; // loops through page table and adds each page to the next available frame // in memory foreach (ProcessPage p in pt.Pages) { mf = new MFrame(p); // grab the next free frame int freeFrame = freeFrames[0]; freeFrames.RemoveAt(0); // set the page tabels frame value to the frame its being placed in p.FrameNumber = freeFrame; // set the frame value of the frame mf.FrameNumber = freeFrame; // place frame into memory in the free frame memory[freeFrame] = mf; } }