From 0bd861c19f480643f158063209434a2bbd6ebc59 Mon Sep 17 00:00:00 2001 From: Matheus Date: Sat, 8 Oct 2022 22:03:46 -0300 Subject: [PATCH] process removal: remove a few bugs and race conditions --- src/sys/sched/scheduler.c | 7 ------- src/sys/syscalls/exit.c | 12 ++++++++++-- src/sys/syscalls/waitpid.c | 16 ++++++++-------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/sys/sched/scheduler.c b/src/sys/sched/scheduler.c index 3b6b84b3..39ee3893 100644 --- a/src/sys/sched/scheduler.c +++ b/src/sys/sched/scheduler.c @@ -362,13 +362,6 @@ void sched_dequeue(){ timer_resume(); - thread_t* curthread = arch_getcls()->thread; - - // context and thread destruction will happen in the waitpid() - // the pointer to this thread will reside in proc->threads - - curthread->proc->threads = curthread; - // for safety diff --git a/src/sys/syscalls/exit.c b/src/sys/syscalls/exit.c index c61de29f..ed199222 100644 --- a/src/sys/syscalls/exit.c +++ b/src/sys/syscalls/exit.c @@ -4,6 +4,7 @@ #include #include #include +#include void syscall_exit(int status){ proc_t* proc = arch_getcls()->thread->proc; @@ -44,10 +45,17 @@ void syscall_exit(int status){ } spinlock_release(&init->lock); - spinlock_release(&proc->lock); - event_signal(&proc->parent->childevent, true); + // context and thread destruction will happen in the waitpid() + // the pointer to this thread will reside in proc->threads + + proc->threads = arch_getcls()->thread; + + arch_interrupt_disable(); + + spinlock_release(&proc->lock); + sched_dequeue(); __builtin_unreachable(); diff --git a/src/sys/syscalls/waitpid.c b/src/sys/syscalls/waitpid.c index 7a122da2..d98d2f59 100644 --- a/src/sys/syscalls/waitpid.c +++ b/src/sys/syscalls/waitpid.c @@ -38,25 +38,24 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){ proc_t* proc = arch_getcls()->thread->proc; bool loop = true; - - + proc_t* child; proc_t* sibling; while(1){ - - child = proc->child; - sibling = NULL; // now find the child spinlock_acquire(&proc->lock); - + child = proc->child; + sibling = NULL; + while(child){ if(pid > 0 && child->pid != pid) continue; if(child->state == PROC_STATE_ZOMBIE){ + loop = false; break; } @@ -90,6 +89,9 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){ sibling->sibling = child->sibling; spinlock_release(&sibling->lock); } + else{ + proc->child = child->sibling; + } *status = child->status; // XXX safer way of doing this @@ -98,7 +100,6 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){ thread_t* thread = child->threads; // TODO free ctx - free(thread->regs); free(thread->kernelstackbase); free(thread); @@ -106,7 +107,6 @@ syscallret syscall_waitpid(pid_t pid, int *status, int options){ retv.ret = child->pid; retv.errno = 0; - return retv;