diff --git a/xv6-public/Makefile b/xv6-public/Makefile old mode 100755 new mode 100644 index b4fd9a2..af4e706 --- a/xv6-public/Makefile +++ b/xv6-public/Makefile @@ -172,6 +172,7 @@ UPROGS=\ _usertests\ _wc\ _zombie\ + _getpcount\ fs.img: mkfs README $(UPROGS) ./mkfs fs.img README $(UPROGS) diff --git a/xv6-public/getpcount.c b/xv6-public/getpcount.c new file mode 100644 index 0000000..e5e54b1 --- /dev/null +++ b/xv6-public/getpcount.c @@ -0,0 +1,11 @@ +#include "types.h" +#include "stat.h" +#include "user.h" + +int +main(void){ + int running_count; + running_count = getpcount(); + printf(1, "Processes currently running: %d\n", running_count); + exit(); +} diff --git a/xv6-public/syscall.c b/xv6-public/syscall.c old mode 100755 new mode 100644 index 799ebc2..35e7c3a --- a/xv6-public/syscall.c +++ b/xv6-public/syscall.c @@ -98,6 +98,7 @@ extern int sys_unlink(void); extern int sys_wait(void); extern int sys_write(void); extern int sys_uptime(void); +extern int sys_getpcount(void); static int (*syscalls[])(void) = { [SYS_fork] sys_fork, @@ -121,6 +122,7 @@ static int (*syscalls[])(void) = { [SYS_link] sys_link, [SYS_mkdir] sys_mkdir, [SYS_close] sys_close, +[SYS_getpcount] sys_getpcount, }; void diff --git a/xv6-public/syscall.h b/xv6-public/syscall.h old mode 100755 new mode 100644 index bc5f356..694e78c --- a/xv6-public/syscall.h +++ b/xv6-public/syscall.h @@ -20,3 +20,4 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_getpcount 22 diff --git a/xv6-public/sysproc.c b/xv6-public/sysproc.c old mode 100755 new mode 100644 index 027a5e5..d712cd3 --- a/xv6-public/sysproc.c +++ b/xv6-public/sysproc.c @@ -6,6 +6,28 @@ #include "memlayout.h" #include "mmu.h" #include "proc.h" +#include "spinlock.h" + +struct { + struct spinlock lock; + struct proc proc[NPROC]; +} ptable; + +int +sys_getpcount(void) +{ + struct proc *p; + acquire(&ptable.lock); + int count = 0; + for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + // Process states: UNUSED, EMBRYO, SLEEPING, RUNNABLE, RUNNING, ZOMBIE + if(p->state == SLEEPING){count++;} + else if(p->state == RUNNABLE){count++;} + else if(p->state == RUNNING){count++;} + } + release(&ptable.lock); + return count; +} int sys_fork(void) diff --git a/xv6-public/user.h b/xv6-public/user.h old mode 100755 new mode 100644 index f45b8d5..dd28e61 --- a/xv6-public/user.h +++ b/xv6-public/user.h @@ -23,6 +23,7 @@ int getpid(void); char* sbrk(int); int sleep(int); int uptime(void); +int getpcount(void); // ulib.c int stat(char*, struct stat*); diff --git a/xv6-public/usys.S b/xv6-public/usys.S old mode 100755 new mode 100644 index 8bfd8a1..2175e63 --- a/xv6-public/usys.S +++ b/xv6-public/usys.S @@ -29,3 +29,4 @@ SYSCALL(getpid) SYSCALL(sbrk) SYSCALL(sleep) SYSCALL(uptime) +SYSCALL(getpcount)