How to add a system call to find the number of processes in xv6

0

I have added this function in proc.c file

int getNumProc(void)
{
  struct proc *p;
  int count = 0;

  acquire(&ptable.lock);

  for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
  {
     if(p->state != UNUSED)
        count++;
  }

  release(&ptable.lock);

 return count;
}

I have made all the necessary modifications in the following files:

  1. defs.h
  2. sysproc.c
  3. syscall.h
  4. usys.S
  5. syscall.c
  6. user.h

I also created a user program called totproc.c to call this system call and added this user program in Makefile at relevant places. When I type totproc command in XV6 shell the command does print that there a 3 processes. But alongside the result, it also prints the following error :

pid 4 totproc: trap 14 err 5 on cpu 1 eip 0xffffffff addr 0xffffffff--kill proc

What could be wrong here? If you were to write a system call to find the number of processes, how would you write it?

unix
operating-system
system-calls
xv6
asked on Stack Overflow Sep 1, 2019 by Daksh • edited Sep 1, 2019 by Mathieu

2 Answers

0

You seems to be in the right way but looks like you are missing something.

The error you are getting is being produced when an unexpected interrupt is received (in trap.c). Specifically, trap number 14 is T_PGFLT (according to trap.h). This means the MMU answered with a page fault interrupt when some address was being tried to access, in other words, you are probably having a memory overwrite or access violation somewhere.

Consider sharing you user space application code.

answered on Stack Overflow Sep 1, 2019 by Omer Efrat
0

Well , I figured out the problem. Turned out , the problem was not in my system call but in the user program totproc.c that made the system call. My initial totproc.c looked like this :

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

int main()
{
  printf(1 , "No. of Process: %d" , getNumProc());
  return 0;
}

The properly working totproc.c is like below :

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

int main()
{
  printf(1 , "No. of Process: %d" , getNumProc());
  exit();
}
answered on Stack Overflow Sep 7, 2019 by Daksh

User contributions licensed under CC BY-SA 3.0