diff --git a/Makefile b/Makefile index 09d790c..9ace969 100644 --- a/Makefile +++ b/Makefile @@ -181,6 +181,7 @@ UPROGS=\ _usertests\ _wc\ _zombie\ + _test_fork fs.img: mkfs README $(UPROGS) ./mkfs fs.img README $(UPROGS) diff --git a/proc.c b/proc.c index 74657fc..fd82a0b 100644 --- a/proc.c +++ b/proc.c @@ -545,3 +545,18 @@ procdump(void) cprintf("\n"); } } + +int sys_nb_children() { + int n = 0; + struct proc *p = 0; + acquire(&ptable.lock); + for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){ + if(p->state == UNUSED) + continue; + if(p->parent == myproc()) + n++; + } + release(&ptable.lock); + + return n; +} diff --git a/syscall.c b/syscall.c index ee85261..115488b 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_nb_children(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_nb_children] sys_nb_children, }; void diff --git a/syscall.h b/syscall.h index 33d0377..fb4e3e7 100644 --- a/syscall.h +++ b/syscall.h @@ -22,3 +22,4 @@ #define SYS_link 19 #define SYS_mkdir 20 #define SYS_close 21 +#define SYS_nb_children 22 diff --git a/test_fork.c b/test_fork.c new file mode 100644 index 0000000..7ed7281 --- /dev/null +++ b/test_fork.c @@ -0,0 +1,16 @@ +#include "types.h" +#include "user.h" + +int main() { + for(int i = 0; i< 4; i++) { + if(fork() == 0) break; + } + + printf(1, "%d - %d children\n", getpid(), nb_children()); + sleep(1); + + for(int i = 0; i<4; i++) + wait(); + + exit(); +} diff --git a/user.h b/user.h index e48c7f7..7166ad7 100644 --- a/user.h +++ b/user.h @@ -25,6 +25,7 @@ int getpid(void); char* sbrk(int); int sleep(int); int uptime(void); +int nb_children(); // ulib.c int stat(const char*, struct stat*); diff --git a/usys.S b/usys.S index 8bfd8a1..347d725 100644 --- a/usys.S +++ b/usys.S @@ -29,3 +29,4 @@ SYSCALL(getpid) SYSCALL(sbrk) SYSCALL(sleep) SYSCALL(uptime) +SYSCALL(nb_children)