I've done all the steps to create a system call, and then created a user program and run it:
proc.c
int countChildren (int pid) {
struct proc *p;
int count = 0;
acquire(&ptable.lock);
for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
if(p->parent->pid == pid) count++;
release(&ptable.lock);
return count;
}
sysproc.c
int
sys_getchildren(void)
{
int pid;
argint(0, &pid);
return countChildren(pid);
}
userProgram.c
...
#include "types.h"
#include "user.h"
int main (void) {
int n1 = fork();
int n2 = fork();
int n3 = fork();
int n4 = fork();
if (n1 > 0 && n2 > 0 && n3 > 0 && n4 > 0) {
printf(1,"parent\n");
printf(1," getchildren = %d \n", getchildren());
}
exit();
}
But the result is not what I expected, below is the result: