[USER] Exit and wait syscalls

This commit is contained in:
Thomas Lovén 2017-02-24 15:30:45 +01:00
parent 5e4946e8e4
commit e3e661e7e5
6 changed files with 76 additions and 9 deletions

View File

@ -1,22 +1,32 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
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;
}

View File

@ -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);

View File

@ -3,6 +3,9 @@
#include <thread.h>
#include <scheduler.h>
#include <string.h>
#include <mem.h>
#include <debug.h>
#include <list.h>
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;
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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);
}