Commit d39c7d8e authored by benkma's avatar benkma

Lab 16 completed

parent cc89b9e1
......@@ -139,3 +139,14 @@ void cpustate::init_idle_task() {
idle_task_ = knew<proc>();
idle_task_->init_kernel(-1, idle);
}
bool cpustate::hasUserProcess() {
spinlock_guard guard(runq_lock_);
while(this->runq_.front() != nullptr) {
if(runq_.front()->pagetable_ != early_pagetable){
return true;
}
this->runq_.pop_front();
}
return false;
}
......@@ -39,6 +39,13 @@ proc::~proc() {
delete pagetable_;
}
bool proc::isUserProcess(){
if(this->pagetable_->entry[256] == early_pagetable->entry[256]) {
return true;
}
return false;
}
// proc::init_user(pid, pt)
// Initialize this `proc` as a new runnable user process with PID `pid`
// and initial page table `pt`.
......
......@@ -58,6 +58,7 @@ void init_process() {
initProc->syscall_waitpid(0,W_NOHANG);
initProc->yield();
}
haltIfNoUserProcesses();
assert(false);
}
......@@ -74,6 +75,20 @@ void create_init_process() {
cpus[INIT_PID % ncpu].enqueue(initProc);
}
void haltIfNoUserProcesses() {
int index = 0;
bool containsHaltProcess = false;
while(index < 16) {
if(cpus[index].hasUserProcess()){
containsHaltProcess = true;
}
index++;
}
if(!containsHaltProcess) {
process_halt();
}
}
// boot_process_start(pid, name)
// Load application program `name` as process number `pid`.
// This loads the application's code and data into memory, sets its
......@@ -384,7 +399,7 @@ int proc::syscall_sleep(uint64_t msec){
unsigned long now = ticks;
unsigned long end = round_up(now,10) + msec;
while (now < end) {
if (!ptable[ppid_]->exited_.empty() || !this->exited_.empty()) {
if (!this->exited_.empty()) {
return E_INTR;
}
yield();
......
......@@ -88,6 +88,8 @@ struct __attribute__((aligned(4096))) proc {
inline irqstate lock_pagetable_read();
inline void unlock_pagetable_read(irqstate& irqs);
bool isUserProcess();
private:
static int load_segment(const elf_program& ph, proc_loader& ld);
......@@ -151,6 +153,8 @@ struct __attribute__((aligned(4096))) cpustate {
void enable_irq(int irqno);
void disable_irq(int irqno);
bool hasUserProcess();
private:
void init_cpu_hardware();
void init_idle_task();
......@@ -504,4 +508,5 @@ inline irqstate proc::lock_pagetable_read() {
inline void proc::unlock_pagetable_read(irqstate&) {
}
void haltIfNoUserProcesses();
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment