Search code examples
clinuxunixxv6

Not able to implement ps command to xv6


I've trying to implement a ps user program in xv6 that will allow me to see information about all running processes including how many times each has been chosen to run and the process ID of each using the system call getpinfo.

My problem is that even though everything compiles when I use ps it runs but it outputs blank information to the console

int sys_getpinfo(void) {
  // pass process info
  struct pstat *a;
  argptr(0, (void *)&a, sizeof(*a));
  struct proc *p;
  // iterate over the process list skipping unused processes
  acquire(&ptable.lock);
  int i = 0;
  for (p = ptable.proc; p < &ptable.proc[NPROC]; p++) {
    if (p->state == UNUSED) {
      continue;
    } else {
      a->pid[i] = p->pid;         // I think this line is wrong
      a->tickets[i] = p->tickets; // I think this line is wrong
    }
    i++;
  }
  release(&ptable.lock);
  return 0;
}

Here is the definition of the struct

#ifndef _PSTAT_H_
#define _PSTAT_H_
#include "param.h"
struct pstat {
  int num_processes;  // the total number of non-UNUSED processes in the process
                      // table
  int pid[NPROC];     // the PID of each process
  int tickets[NPROC]; // the number of tickets this process has
  int ticks[NPROC];   // the number of ticks each process has accumulated
};
#endif // _PSTAT_H_

And here is the ps.c code

#include "mmu.h"
#include "param.h"
#include "proc.h"
#include "pstat.h"
#include "types.h"
#include "user.h"
int main(int argc, char *argv[]) {
  struct pstat info = {};
  getpinfo(&info);
  printf(1, "PID\tTICKETS\tTICKS\n");
  for (int i = 0; i < info.num_processes; ++i) {
    printf(1, "%d\t%d\t%d\n", info.pid[i], info.tickets[i], info.ticks[i]);
  }
  exit();
}

My problem is that even though everything compiles when I use ps it runs but it outputs blank information something like : pid tickets ticks instead of : pid 1 tickets 10 ticks 11

I believe it has something to with my passing the struct pstat value and also trying to get the right information from the processes but I'm not sure


Solution

  • In main, info.num_processes is zero.

    In sys_getpinfo, you need to set num_processes. At the bottom, just before the return do:

    a->num_processes = i;