Skip to content

Commit

Permalink
update syscall settickets, getpinfo; add a test program
Browse files Browse the repository at this point in the history
  • Loading branch information
unconsolable committed Mar 14, 2021
1 parent cf26159 commit c956041
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"files.associations": {
"types.h": "c",
"param.h": "c"
"param.h": "c",
"traps.h": "c",
"defs.h": "c",
"user.h": "c"
}
}
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ UPROGS=\
_usertests\
_wc\
_zombie\
_lotterytest\

fs.img: mkfs README $(UPROGS)
./mkfs fs.img README $(UPROGS)
Expand Down Expand Up @@ -217,7 +218,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
41 changes: 41 additions & 0 deletions lotterytest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "types.h"
#include "user.h"

int main()
{
int pid1, pid2, pid3;
printf(1, "Run 1 CPU to get best results\n");
printf(1, "Get ticks status via Ctrl+P(procdump())\n");
if ((pid1 = fork()) > 0) {
if ((pid2 = fork()) > 0) {
if ((pid3 = fork()) > 0) {
struct pstat p;
getpinfo(&p);
for (int i = 0; i < NPROC; i++) {
if (p.inuse[i]) {
printf(1, "%d %d %d\n", p.tickets[i], p.pid[i], p.ticks[i]);
}
}
exit();
} else if (pid3 == 0) {
settickets(10);
printf(1, "child3\n");
while (1) {
}
exit();
}
} else if (pid2 == 0) {
settickets(20);
printf(1, "child2\n");
while (1) {
}
exit();
}
} else if (pid1 == 0) {
settickets(30);
printf(1, "child1\n");
while (1) {
}
exit();
}
}
31 changes: 21 additions & 10 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
struct {
struct spinlock lock;
struct proc proc[NPROC];
int totaltickets;
} ptable;

static struct proc *initproc;
Expand All @@ -27,7 +26,6 @@ void
pinit(void)
{
initlock(&ptable.lock, "ptable");
ptable.totaltickets = 0;
}

// Must be called with interrupts disabled
Expand Down Expand Up @@ -143,6 +141,7 @@ userinit(void)
p->tf->esp = PGSIZE;
p->tf->eip = 0; // beginning of initcode.S
p->tickets = 10;
p->totalticks = 0;

safestrcpy(p->name, "initcode", sizeof(p->name));
p->cwd = namei("/");
Expand All @@ -153,7 +152,6 @@ userinit(void)
// because the assignment might not be atomic.
acquire(&ptable.lock);

ptable.totaltickets += p->tickets;
p->state = RUNNABLE;

release(&ptable.lock);
Expand Down Expand Up @@ -218,10 +216,10 @@ fork(void)

pid = np->pid;
np->tickets = curproc->tickets;
np->totalticks = 0;

acquire(&ptable.lock);

ptable.totaltickets += curproc->tickets;
np->state = RUNNABLE;

release(&ptable.lock);
Expand Down Expand Up @@ -255,6 +253,10 @@ exit(void)
end_op();
curproc->cwd = 0;

acquire(&tickslock);
curproc->totalticks += ticks - curproc->lastrunticks;
release(&tickslock);

acquire(&ptable.lock);

// Parent might be sleeping in wait().
Expand Down Expand Up @@ -303,6 +305,8 @@ wait(void)
p->name[0] = 0;
p->killed = 0;
p->state = UNUSED;
p->totalticks = 0;
p->lastrunticks = 0;
release(&ptable.lock);
return pid;
}
Expand Down Expand Up @@ -337,7 +341,9 @@ scheduler(void)
for(;;){
// Enable interrupts on this processor.
sti();

acquire(&tickslock);
int curticks = ticks;
release(&tickslock);
// Loop over process table looking for process to run.
acquire(&ptable.lock);

Expand Down Expand Up @@ -365,11 +371,12 @@ scheduler(void)
// cprintf("finding...ticketcounter = %d, lottery = %d, pid = %d, p->state = %d\n", ticketcounter, lottery, p->pid, p->state);
}
}
// cprintf("found. total = %d, lottery = %d, pid = %d\n", ptable.totaltickets, lottery, p->pid);
// cprintf("found. total = %d, lottery = %d, pid = %d\n", totalticket, lottery, p->pid);
// 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;
p->lastrunticks = curticks;
switchuvm(p);
p->state = RUNNING;

Expand Down Expand Up @@ -413,6 +420,9 @@ sched(void)
void
yield(void)
{
acquire(&tickslock);
myproc()->totalticks += ticks - myproc()->lastrunticks;
release(&tickslock);
acquire(&ptable.lock); //DOC: yieldlock
myproc()->state = RUNNABLE;
sched();
Expand Down Expand Up @@ -453,6 +463,9 @@ sleep(void *chan, struct spinlock *lk)
if(lk == 0)
panic("sleep without lk");

acquire(&tickslock);
p->totalticks += ticks - p->lastrunticks;
release(&tickslock);
// Must acquire ptable.lock in order to
// change p->state and then call sched.
// Once we hold ptable.lock, we can be
Expand Down Expand Up @@ -551,7 +564,7 @@ procdump(void)
state = states[p->state];
else
state = "???";
cprintf("%d %s %s", p->pid, state, p->name);
cprintf("%d %s %s %d %d\n", p->pid, state, p->name, p->totalticks, p->tickets);
if(p->state == SLEEPING){
getcallerpcs((uint*)p->context->ebp+2, pc);
for(i=0; i<10 && pc[i] != 0; i++)
Expand All @@ -565,9 +578,7 @@ int settickets(int new)
{
struct proc *curproc = myproc();
acquire(&ptable.lock);
ptable.totaltickets -= curproc->tickets;
curproc->tickets = new;
ptable.totaltickets += new;
release(&ptable.lock);
return 0;
}
Expand All @@ -590,7 +601,7 @@ int getpinfo(struct pstat *ps)
ps->pid[i] = p->pid;
ps->tickets[i] = p->tickets;
// TODO: store the ticks for the process
ps->ticks[i] = 0;
ps->ticks[i] = p->totalticks;
}

return 0;
Expand Down
2 changes: 2 additions & 0 deletions proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ struct proc {
struct inode *cwd; // Current directory
char name[16]; // Process name (debugging)
int tickets; // Tickets for lottery scheduler
int totalticks; // Running ticks in total
int lastrunticks; // The tick of last running
};

// Process memory is laid out contiguously, low addresses first:
Expand Down
2 changes: 2 additions & 0 deletions usys.S
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ SYSCALL(sbrk)
SYSCALL(sleep)
SYSCALL(uptime)
SYSCALL(getreadcount)
SYSCALL(settickets)
SYSCALL(getpinfo)

0 comments on commit c956041

Please sign in to comment.