[USER] Fork syscall

This commit is contained in:
Thomas Lovén 2017-02-21 15:10:47 +01:00
parent a2cba95346
commit 5e4946e8e4
6 changed files with 63 additions and 10 deletions

View File

@ -6,16 +6,16 @@ int main(int argc, char **argv)
(void) argc;
(void) argv;
char *a = malloc(0x100);
printf("Pointer: %p\n", a);
char *b = malloc(0x100);
printf("Pointer: %p\n", b);
char *c = malloc(0x10);
printf("Pointer: %p\n", c);
c[0] = 5;
free(a);
free(b);
free(c);
int num = 5;
if(fork())
{
num = 10;
printf("I am the parent! - %d\n", num);
} else {
num = 3;
printf("I am the child! - %d\n", num);
}
for(;;);
return 0;

View File

@ -42,3 +42,4 @@ typedef long (*syscall_handler_t)(long num, long, long, long, long, long, long);
SYSCALL_DECL(write);
SYSCALL_DECL(brk);
SYSCALL_DECL(fork);

27
kernel/syscall/sys_proc.c Normal file
View File

@ -0,0 +1,27 @@
#include <syscall.h>
#include <process.h>
#include <thread.h>
#include <scheduler.h>
#include <string.h>
SYSCALL_DEF(fork)
{
SYSCALL_INIT();
// Copy process and memory space
process_t *p = get_current_process();
process_t *new = process_spawn(p);
// Copy thread
thread_t *th = new_thread(0, 1);
memcpy(&th->r, &get_current_thread()->r, sizeof(registers_t));
// Make new thread return 0
th->r.rax = 0;
process_attach(new, th);
scheduler_insert(th);
// Return new pid
return new->pid;
}

View File

@ -58,6 +58,7 @@ void syscall_init()
SYSCALL_REGISTER(debug, SYS_DEBUG);
SYSCALL_REGISTER(write, SYS_WRITE);
SYSCALL_REGISTER(brk, SYS_BRK);
SYSCALL_REGISTER(fork, SYS_FORK);
}
}

View File

@ -2,4 +2,5 @@
#define SYS_WRITE 0x001
#define SYS_BRK 0x002
#define SYS_FORK 0x003
#define SYS_DEBUG 0x3FF

View File

@ -1,4 +1,8 @@
#include "syscalls.h"
#include "syscall_num.h"
#include <pthread.h>
#include <signal.h>
long kernel_syscall(int num, ...);
struct pthread *__pt;
@ -19,3 +23,22 @@ SYSCALL_DEF(set_tid_address)
SYSCALL_INIT();
return 0;
}
SYSCALL_DEF(rt_sigprocmask)
{
SYSCALL_INIT(int, how, const sigset_t *, set, sigset_t *, oldset);
return 0;
}
SYSCALL_DEF(fork)
{
SYSCALL_INIT();
return kernel_syscall(SYS_FORK);
}
SYSCALL_DEF(gettid)
{
SYSCALL_INIT();
return 0;
}