public void LoadPagesOfProcessIntoMemory(Process processWithPagesToLoad) { Page[] pagesToLoad = processWithPagesToLoad.PagesOfProcess; int loadingProcessID = processWithPagesToLoad.ProcessID; int numOfPagesToLoad = pagesToLoad.Length; _pageTables.Add(loadingProcessID, new PageTable(numOfPagesToLoad)); PageTable pageTableOfProcess = _pageTables[loadingProcessID]; int i = 0; while (!_physicalMemory.IsFull && i < numOfPagesToLoad) { pageTableOfProcess.ChangeFrameNumberForPageNumber( pagesToLoad[i].PageNumber, _physicalMemory.InsertPage(pagesToLoad[i])); _addedPagesInfo.Add( new PageInfo() { ProcessID = loadingProcessID, PageNumber = pagesToLoad[i].PageNumber }); PageAddedToPhysicalMemoryEvent?.Invoke(loadingProcessID, pagesToLoad[i].PageNumber); ++i; } while (i < numOfPagesToLoad) { _virtualMemory.InsertPage(pagesToLoad[i++]); } processWithPagesToLoad.ProcessFinishedWorkEvent += RemovePages; }
private void RemovePages(int finishedProcessID) { ProcessFinishedWorkEvent?.Invoke(finishedProcessID); PageTable pageTable = _pageTables[finishedProcessID]; int frameNumber; for (int i = 0; i < pageTable.NumOfPages; ++i) { frameNumber = pageTable.FindFrameNumber(i); if (frameNumber != -1) { _physicalMemory.ExtractPage(frameNumber); } else { _virtualMemory.ExtractPage(finishedProcessID, i); } lock (_object) { int j = _addedPagesInfo.FindIndex(pI => { if (pI.ProcessID == finishedProcessID && pI.PageNumber == i) { return(true); } return(false); }); if (j != -1) { _addedPagesInfo.RemoveAt(j); } } } _pageTables.Remove(finishedProcessID); }