void MemoryManager::executeCycle() { ++_m_cycle_num; for (unsigned long i = 0; i < _running_queue.size(); ++i) { process_t& _proc = _running_queue[i]; _proc._burst_time = (_proc._burst_time - 1); if (_proc._burst_time == 0) { _proc._can_swap_out = true; } } if (hasReadyProcess()) { if (swapIn(pullNextFromReadyQueue())) { _ready_queue.erase(_ready_queue.begin()); } else { bool proc_swapped = false; for (unsigned long i = 0; i < _running_queue.size(); i++) { process_t& _proc = _running_queue[i]; if (_proc._can_swap_out) { if (swapOut(_proc)) { _proc._can_swap_out = false; _proc._can_swap_in = true; _ready_queue.push_back(_running_queue.at(i)); _running_queue.erase(_running_queue.begin() + i); proc_swapped = true; } } if (proc_swapped) { proc_swapped = false; break; } } } } double _ratio = getMemRatio(); if (_ratio > MAX_MEM_RATION) { std::cout << "\tRUNNING COMPACTION - MAX RATIO HIT (" << _ratio << ")" << std::endl; doCompaction(); printMemMap(); } }
// Atomically release lock and sleep on chan. // Reacquires lock when awakened. void sleep(void *chan, struct spinlock *lk) { if(proc == 0) panic("sleep"); if(lk == 0) panic("sleep without lk"); // Must acquire ptable.lock in order to // change p->state and then call sched. // Once we hold ptable.lock, we can be // guaranteed that we won't miss any wakeup // (wakeup runs with ptable.lock locked), // so it's okay to release lk. if(lk != &ptable.lock){ //DOC: sleeplock0 acquire(&ptable.lock); //DOC: sleeplock1 release(lk); } // Go to sleep. proc->chan = chan; proc->state = SLEEPING; if(proc->pid > 2 && swap_enabled){ release(&ptable.lock); swapOut(proc); acquire(&ptable.lock); }else{ proc->state = SLEEPING_SUSPENDED; } sched(); // Tidy up. proc->chan = 0; // Reacquire original lock. if(lk != &ptable.lock){ //DOC: sleeplock2 release(&ptable.lock); acquire(lk); } }
// Allocate page tables and physical memory to grow process from oldsz to // newsz, which need not be page aligned. Returns new size or 0 on error. int allocuvm(pde_t *pgdir, uint oldsz, uint newsz) { char *mem; uint a; if (newsz >= KERNBASE) return 0; if (newsz < oldsz) return oldsz; a = PGROUNDUP(oldsz); for (; a < newsz; a += PGSIZE) { mem = kalloc(); if (mem == 0) { deallocuvm(pgdir, newsz, oldsz); return 0; } memset(mem, 0, PGSIZE); mappages(pgdir, (char*) a, PGSIZE, v2p(mem), PTE_W | PTE_U); #ifndef NONE /* a&k start */ if (isNotInitShell(proc)) { int ans = 0; if ((proc->swapData).nPhysicalPages >= MAX_PSYC_PAGES) ans = swapOut(pgdir); if (ans < 0) panic("Can't swap out!"); #if defined(FIFO) || defined(SCFIFO) int index = addMemAddr(PGROUNDDOWN(a)); (proc->swapData).creationTime[index] = ticks; #endif #ifdef NFU addMemAddr(PGROUNDDOWN(a)); #endif } /* a&k end */ #endif } return newsz; }
void ofxFBOTexture::end() { swapOut(); setupScreenForThem(); }
int pageFaultAction(uint va) { pde_t *pgdir = proc->pgdir; swapOut(pgdir); swapIn(va); return 0; }