From e3e661e7e5ad8e572cefcd79dca74c96f7e97a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Fri, 24 Feb 2017 15:30:45 +0100 Subject: [PATCH] [USER] Exit and wait syscalls --- init/init.c | 26 +++++++++++++++++-------- kernel/include/syscall.h | 2 ++ kernel/syscall/sys_proc.c | 41 ++++++++++++++++++++++++++++++++++++++- kernel/syscall/syscall.c | 2 ++ libc/syscall_num.h | 2 ++ libc/thread.c | 12 ++++++++++++ 6 files changed, 76 insertions(+), 9 deletions(-) diff --git a/init/init.c b/init/init.c index 425ecc7..79833ec 100644 --- a/init/init.c +++ b/init/init.c @@ -1,22 +1,32 @@ #include #include #include +#include int main(int argc, char **argv) { (void) argc; (void) argv; - int num = 5; - - if(fork()) + if(!fork()) { - num = 10; - printf("I am the parent! - %d\n", num); - } else { - num = 3; - printf("I am the child! - %d\n", num); + if(!fork()) + return 200; + return 100; } + for(int j=0; j <= 10; j++) + { + if(!fork()) + return j; + } + while(1) + { + int retval; + int pid = wait(&retval); + printf("Pid: %d exited with %d\n", pid, retval); + } + + for(;;); return 0; } diff --git a/kernel/include/syscall.h b/kernel/include/syscall.h index 3eb2368..8ef4f02 100644 --- a/kernel/include/syscall.h +++ b/kernel/include/syscall.h @@ -43,3 +43,5 @@ typedef long (*syscall_handler_t)(long num, long, long, long, long, long, long); SYSCALL_DECL(write); SYSCALL_DECL(brk); SYSCALL_DECL(fork); +SYSCALL_DECL(exit); +SYSCALL_DECL(wait); diff --git a/kernel/syscall/sys_proc.c b/kernel/syscall/sys_proc.c index adae757..7499d67 100644 --- a/kernel/syscall/sys_proc.c +++ b/kernel/syscall/sys_proc.c @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include SYSCALL_DEF(fork) { @@ -23,5 +26,41 @@ SYSCALL_DEF(fork) // Return new pid return new->pid; - +} + +__attribute__((noreturn)) SYSCALL_DEF(exit) +{ + SYSCALL_INIT(int, status); + + process_t *proc = get_current_process(); + + process_exit(proc, status); + + schedule(); + debug_error("PANIC - This line should be unreachable (%s:%d)\n", __FILE__, __LINE__); + for(;;); +} + +SYSCALL_DEF(wait) +{ + SYSCALL_INIT(int *, status); + + process_t *proc = get_current_process(); + int pid = 0; + while(!pid) + { + LIST_FOREACH(proc->children, process_t, p, siblings) + { + if(p->state == PROC_STATE_ZOMBIE) + { + pid = p->pid; + *status = p->status; + // Destroy the process + process_free(p); + break; + } + } + schedule(); + } + return pid; } diff --git a/kernel/syscall/syscall.c b/kernel/syscall/syscall.c index 9de60da..b9b03ed 100644 --- a/kernel/syscall/syscall.c +++ b/kernel/syscall/syscall.c @@ -59,6 +59,8 @@ void syscall_init() SYSCALL_REGISTER(write, SYS_WRITE); SYSCALL_REGISTER(brk, SYS_BRK); SYSCALL_REGISTER(fork, SYS_FORK); + SYSCALL_REGISTER(exit, SYS_EXIT); + SYSCALL_REGISTER(wait, SYS_WAIT); } } diff --git a/libc/syscall_num.h b/libc/syscall_num.h index 01da47e..0ab8f51 100644 --- a/libc/syscall_num.h +++ b/libc/syscall_num.h @@ -3,4 +3,6 @@ #define SYS_WRITE 0x001 #define SYS_BRK 0x002 #define SYS_FORK 0x003 +#define SYS_EXIT 0x004 +#define SYS_WAIT 0x005 #define SYS_DEBUG 0x3FF diff --git a/libc/thread.c b/libc/thread.c index 7397af7..1e77f5d 100644 --- a/libc/thread.c +++ b/libc/thread.c @@ -42,3 +42,15 @@ SYSCALL_DEF(gettid) SYSCALL_INIT(); return 0; } + +SYSCALL_DEF(exit_group) +{ + SYSCALL_INIT(int, result); + return kernel_syscall(SYS_EXIT, result); +} + +SYSCALL_DEF(wait4) +{ + SYSCALL_INIT(int, pid, int *, result, int, options, void *, rusage); + return kernel_syscall(SYS_WAIT, result); +}