Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EX2 ten ticks #229

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ kernel
kernelmemfs
mkfs
.gdbinit
.idea
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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''
Expand Down Expand Up @@ -181,6 +181,8 @@ UPROGS=\
_usertests\
_wc\
_zombie\
_hello\
_ticktest\

fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
Expand Down Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Test syscall hello.

#include "types.h"
#include "stat.h"
#include "user.h"

int
main(void)
{
hello();
exit();
}
25 changes: 17 additions & 8 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ scheduler(void)
{
struct proc *p;
struct cpu *c = mycpu();
int ticks_run;
c->proc = 0;

for(;;){
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
#define SYS_link 19
#define SYS_mkdir 20
#define SYS_close 21
#define SYS_hello 22
7 changes: 7 additions & 0 deletions sysproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ sys_uptime(void)
release(&tickslock);
return xticks;
}

int
sys_hello(void)
{
cprintf("Hello from xv6!\n");
return 0;
}
31 changes: 31 additions & 0 deletions ticktest.c
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 1 addition & 0 deletions user.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
Expand Down
1 change: 1 addition & 0 deletions usys.S
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ SYSCALL(getpid)
SYSCALL(sbrk)
SYSCALL(sleep)
SYSCALL(uptime)
SYSCALL(hello)