diff --git a/.gitignore b/.gitignore index 3e2c9deafe..10a5f5d5a5 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ kernel kernelmemfs mkfs .gdbinit +.idea \ No newline at end of file diff --git a/Makefile b/Makefile index 09d790cf63..d8db67ff7e 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ TOOLPREFIX := $(shell if i386-jos-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/d endif # If the makefile can't find QEMU, specify its path here -# QEMU = qemu-system-i386 +QEMU = qemu-system-i386 # Try to infer the correct QEMU ifndef QEMU @@ -76,7 +76,7 @@ AS = $(TOOLPREFIX)gas LD = $(TOOLPREFIX)ld OBJCOPY = $(TOOLPREFIX)objcopy OBJDUMP = $(TOOLPREFIX)objdump -CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -fno-omit-frame-pointer +CFLAGS = -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -fno-omit-frame-pointer CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector) ASFLAGS = -m32 -gdwarf-2 -Wa,-divide # FreeBSD ld wants ``elf_i386_fbsd'' @@ -181,6 +181,8 @@ UPROGS=\ _usertests\ _wc\ _zombie\ + _hello\ + _ticktest\ fs.img: mkfs README $(UPROGS) ./mkfs fs.img README $(UPROGS) @@ -217,7 +219,7 @@ QEMUGDB = $(shell if $(QEMU) -help | grep -q '^-gdb'; \ then echo "-gdb tcp::$(GDBPORT)"; \ else echo "-s -p $(GDBPORT)"; fi) ifndef CPUS -CPUS := 2 +CPUS := 1 endif QEMUOPTS = -drive file=fs.img,index=1,media=disk,format=raw -drive file=xv6.img,index=0,media=disk,format=raw -smp $(CPUS) -m 512 $(QEMUEXTRA) diff --git a/hello.c b/hello.c new file mode 100644 index 0000000000..38c1f5cc6f --- /dev/null +++ b/hello.c @@ -0,0 +1,12 @@ +// Test syscall hello. + +#include "types.h" +#include "stat.h" +#include "user.h" + +int +main(void) +{ + hello(); + exit(); +} diff --git a/proc.c b/proc.c index 806b1b184b..e8f90193c7 100644 --- a/proc.c +++ b/proc.c @@ -324,6 +324,7 @@ scheduler(void) { struct proc *p; struct cpu *c = mycpu(); + int ticks_run; c->proc = 0; for(;;){ @@ -333,21 +334,29 @@ scheduler(void) // Loop over process table looking for process to run. acquire(&ptable.lock); for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + // Looking for RUNNABLE process if(p->state != RUNNABLE) continue; - // Switch to chosen process. It is the process's job - // to release ptable.lock and then reacquire it - // before jumping back to us. - c->proc = p; - switchuvm(p); - p->state = RUNNING; + cprintf("Scheduler starts running process %d\n", p->pid); - swtch(&(c->scheduler), p->context); - switchkvm(); + for (ticks_run = 1; ticks_run <= 10; ticks_run++) { + if (p->state == ZOMBIE) + break; + + c->proc = p; + switchuvm(p); + p->state = RUNNING; + + swtch(&(c->scheduler), p->context); + switchkvm(); + + cprintf("Process %d finished running %d ticks\n", p->pid, ticks_run); + } // Process is done running for now. // It should have changed its p->state before coming back. + cprintf("Finished running process %d\n", p->pid); c->proc = 0; } release(&ptable.lock); diff --git a/syscall.c b/syscall.c index ee85261602..1e5198ec90 100644 --- a/syscall.c +++ b/syscall.c @@ -103,6 +103,7 @@ extern int sys_unlink(void); extern int sys_wait(void); extern int sys_write(void); extern int sys_uptime(void); +extern int sys_hello(void); static int (*syscalls[])(void) = { [SYS_fork] sys_fork, @@ -126,6 +127,7 @@ static int (*syscalls[])(void) = { [SYS_link] sys_link, [SYS_mkdir] sys_mkdir, [SYS_close] sys_close, +[SYS_hello] sys_hello, }; void diff --git a/syscall.h b/syscall.h index bc5f35651c..dffbc21df3 100644 --- a/syscall.h +++ b/syscall.h @@ -20,3 +20,4 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_hello 22 \ No newline at end of file diff --git a/sysproc.c b/sysproc.c index 0686d295b6..2c41a4b887 100644 --- a/sysproc.c +++ b/sysproc.c @@ -89,3 +89,10 @@ sys_uptime(void) release(&tickslock); return xticks; } + +int +sys_hello(void) +{ + cprintf("Hello from xv6!\n"); + return 0; +} diff --git a/ticktest.c b/ticktest.c new file mode 100644 index 0000000000..38a755dce0 --- /dev/null +++ b/ticktest.c @@ -0,0 +1,31 @@ +#include "types.h" +#include "stat.h" +#include "user.h" + +#define NUM_PROCESSES 5 + +void busy_work() { + for (int i = 0; i <= 100000000; i++){ + continue; + } + exit(); +} + +int main(void) { + int i; + + for (i = 0; i < NUM_PROCESSES; i++) { + int pid = fork(); + if (pid == 0) { + // Child process: sleep + busy_work(); + } + } + + // Parent process: wait for all children + for (i = 0; i < NUM_PROCESSES; i++) { + wait(); + } + + exit(); +} diff --git a/user.h b/user.h index 4f99c52ba6..2f95ca5b4c 100644 --- a/user.h +++ b/user.h @@ -23,6 +23,7 @@ int getpid(void); char* sbrk(int); int sleep(int); int uptime(void); +int hello(void); // ulib.c int stat(const char*, struct stat*); diff --git a/usys.S b/usys.S index 8bfd8a1bc4..0072273f06 100644 --- a/usys.S +++ b/usys.S @@ -29,3 +29,4 @@ SYSCALL(getpid) SYSCALL(sbrk) SYSCALL(sleep) SYSCALL(uptime) +SYSCALL(hello)