aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2024-02-20 19:02:36 +0000
committersotech117 <michael_foiani@brown.edu>2024-02-20 19:02:36 +0000
commitd0c413bd585e5dc1ee2fc2bc8b4af110ef5900c6 (patch)
treee22098e4ebc68f14af241afb2876186408e33d5e
parentd2b13e08f8d6111b7f7e79a47a124fc47dd9433c (diff)
add cancellation test, will ask about bug at meeting
-rw-r--r--kernel/proc/kthread.c9
-rw-r--r--kernel/proc/proc.c9
-rw-r--r--kernel/test/proctest.c24
3 files changed, 39 insertions, 3 deletions
diff --git a/kernel/proc/kthread.c b/kernel/proc/kthread.c
index 46dd627..fa447ae 100644
--- a/kernel/proc/kthread.c
+++ b/kernel/proc/kthread.c
@@ -99,6 +99,9 @@ kthread_t *kthread_create(proc_t *proc, kthread_func_t func, long arg1,
list_init(&new_thread->kt_mutexes);
new_thread->kt_recent_core = 0;
+ // put this thread on the process's thread list
+ list_insert_tail(&proc->p_threads, &new_thread->kt_plink);
+
dbg(DBG_THR, "SUCCESFULLY created a new THREAD with proc name=%s, id=%d\n", proc->p_name, proc->p_pid);
return new_thread;
@@ -160,6 +163,9 @@ void kthread_cancel(kthread_t *thr, void *retval)
KASSERT(thr != curthr);
// TODO: ask about the use of check_curthr_cancelled() in syscall_handler()
+ int status = (int) retval;
+ dbg(DBG_THR, "Cancelling thread with proc name=%s, id=%d, status=%d\n",
+ thr->kt_proc->p_name, thr->kt_proc->p_pid, status);
thr->kt_retval = retval;
sched_cancel(thr);
@@ -172,6 +178,7 @@ void kthread_cancel(kthread_t *thr, void *retval)
void kthread_exit(void *retval)
{
// NOT_YET_IMPLEMENTED("PROCS: kthread_exit");
-
+ dbg(DBG_THR, "Exiting thread with proc name=%s, id=%d, status=%d\n",
+ curthr->kt_proc->p_name, curthr->kt_proc->p_pid, (int) retval);
proc_thread_exiting(retval);
}
diff --git a/kernel/proc/proc.c b/kernel/proc/proc.c
index d0566ec..7350acb 100644
--- a/kernel/proc/proc.c
+++ b/kernel/proc/proc.c
@@ -242,6 +242,8 @@ proc_t *proc_create(const char *name)
void proc_cleanup(long status)
{
// NOT_YET_IMPLEMENTED("PROCS: proc_cleanup");
+ dbg(DBG_PROC, "proc_cleanup called on proc with pid=%d with exit status=%d\n", curproc->p_pid, status);
+
if (curproc->p_pid == PID_INIT)
{
@@ -258,7 +260,7 @@ void proc_cleanup(long status)
list_link_t *link;
list_iterate(&curproc->p_children, child, proc_t, p_child_link)
{
- // remove insert to init process
+ // remove & insert to init process
list_insert_tail(&curproc->p_pproc->p_children, &child->p_child_link);
list_remove(&child->p_child_link);
child->p_pproc = proc_initproc;
@@ -303,12 +305,14 @@ void proc_thread_exiting(void *retval)
void proc_kill(proc_t *proc, long status)
{
// NOT_YET_IMPLEMENTED("PROCS: proc_kill");
+ dbg(DBG_PROC, "proc_kill called on proc with pid=%d\n", proc->p_pid);
KASSERT(proc != curproc && "proc_kill called on curproc");
list_iterate(&proc->p_threads, thr, kthread_t, kt_plink)
{
- kthread_cancel(thr, (void *) status);
+ dbg(DBG_PROC, "cancelling thread on proc with pid=%d with status=%ld\n", proc->p_pid, status);
+ kthread_cancel(thr, (void *)status);
}
}
@@ -429,6 +433,7 @@ pid_t do_waitpid(pid_t pid, int *status, int options)
// sleep until this specific child process exits
while (child->p_state != PROC_DEAD)
{
+ dbg(DBG_PROC, "waiting for specific child, calling sched_sleep_on\n");
sched_sleep_on(&curproc->p_wait);
}
diff --git a/kernel/test/proctest.c b/kernel/test/proctest.c
index 9ab4c65..f49da72 100644
--- a/kernel/test/proctest.c
+++ b/kernel/test/proctest.c
@@ -274,6 +274,28 @@ void test_out_of_order_termination()
}
+/*
+ Test threads' cancellation fields.
+*/
+void test_cancellation()
+{
+ proc_t *new_proc1 = proc_create("proc test 1");
+ kthread_t *new_kthread1 = kthread_create(new_proc1, test_func, new_proc1->p_pid, new_proc1);
+ test_assert(new_kthread1->kt_cancelled == 0, "Thread should not be cancelled");
+ sched_make_runnable(new_kthread1);
+
+ proc_kill(new_proc1, 1);
+
+ test_assert(new_kthread1->kt_cancelled == 1, "Thread should be cancelled");
+
+ // wait for the thread to finish
+ int status;
+ int ret = do_waitpid(new_proc1->p_pid, &status, 0);
+ test_assert(ret != -ECHILD, "Should have found the process");
+ test_assert(status == 1, "Returned status not set correctly");
+}
+
+
long proctest_main(long arg1, void *arg2)
{
dbg(DBG_TEST, "\nStarting Procs tests\n");
@@ -290,6 +312,8 @@ long proctest_main(long arg1, void *arg2)
test_proc_kill_all();
dbg(DBG_TEST, "\nStarting test_out_of_order_termination\n");
test_out_of_order_termination();
+ dbg(DBG_TEST, "\nStarting test_cancellation\n");
+ test_cancellation();
test_fini();
return 0;