[USER] Exit and wait syscalls
This commit is contained in:
parent
5e4946e8e4
commit
e3e661e7e5
26
init/init.c
26
init/init.c
@ -1,22 +1,32 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
(void) argc;
|
(void) argc;
|
||||||
(void) argv;
|
(void) argv;
|
||||||
|
|
||||||
int num = 5;
|
if(!fork())
|
||||||
|
|
||||||
if(fork())
|
|
||||||
{
|
{
|
||||||
num = 10;
|
if(!fork())
|
||||||
printf("I am the parent! - %d\n", num);
|
return 200;
|
||||||
} else {
|
return 100;
|
||||||
num = 3;
|
|
||||||
printf("I am the child! - %d\n", num);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(;;);
|
for(;;);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -43,3 +43,5 @@ typedef long (*syscall_handler_t)(long num, long, long, long, long, long, long);
|
|||||||
SYSCALL_DECL(write);
|
SYSCALL_DECL(write);
|
||||||
SYSCALL_DECL(brk);
|
SYSCALL_DECL(brk);
|
||||||
SYSCALL_DECL(fork);
|
SYSCALL_DECL(fork);
|
||||||
|
SYSCALL_DECL(exit);
|
||||||
|
SYSCALL_DECL(wait);
|
||||||
|
@ -3,6 +3,9 @@
|
|||||||
#include <thread.h>
|
#include <thread.h>
|
||||||
#include <scheduler.h>
|
#include <scheduler.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <mem.h>
|
||||||
|
#include <debug.h>
|
||||||
|
#include <list.h>
|
||||||
|
|
||||||
SYSCALL_DEF(fork)
|
SYSCALL_DEF(fork)
|
||||||
{
|
{
|
||||||
@ -23,5 +26,41 @@ SYSCALL_DEF(fork)
|
|||||||
|
|
||||||
// Return new pid
|
// Return new pid
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,8 @@ void syscall_init()
|
|||||||
SYSCALL_REGISTER(write, SYS_WRITE);
|
SYSCALL_REGISTER(write, SYS_WRITE);
|
||||||
SYSCALL_REGISTER(brk, SYS_BRK);
|
SYSCALL_REGISTER(brk, SYS_BRK);
|
||||||
SYSCALL_REGISTER(fork, SYS_FORK);
|
SYSCALL_REGISTER(fork, SYS_FORK);
|
||||||
|
SYSCALL_REGISTER(exit, SYS_EXIT);
|
||||||
|
SYSCALL_REGISTER(wait, SYS_WAIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,4 +3,6 @@
|
|||||||
#define SYS_WRITE 0x001
|
#define SYS_WRITE 0x001
|
||||||
#define SYS_BRK 0x002
|
#define SYS_BRK 0x002
|
||||||
#define SYS_FORK 0x003
|
#define SYS_FORK 0x003
|
||||||
|
#define SYS_EXIT 0x004
|
||||||
|
#define SYS_WAIT 0x005
|
||||||
#define SYS_DEBUG 0x3FF
|
#define SYS_DEBUG 0x3FF
|
||||||
|
@ -42,3 +42,15 @@ SYSCALL_DEF(gettid)
|
|||||||
SYSCALL_INIT();
|
SYSCALL_INIT();
|
||||||
return 0;
|
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);
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user