So I'm trying to think of a possible way to access a list of the current running processes in xv6. My thought process is to gain access to the ptable, so I can loop through it and increment a counter for all that are running. However, if I'm making a system call for it (so writing to sysproc.c), I don't have access to the ptable as it seems (to me, I'm still poking around) to be exclusive to proc.c
Is there anyway I can access the table to loop through and find running processes, or is there another approach I should be considering?
Edit: Would it be easier just creating a counter variable within a header file, and each time a process is created increment it and once a process exits decrement it?
However, if I'm making a system call for it (so writing to sysproc.c), I don't have access to the ptable as it seems (to me, I'm still poking around) to be exclusive to proc.c
You're right. You don't have access to it inside of sysproc.c. You have access to the entire xv6 source presumably, which means that you can add a function in proc.c (not sysproc.c) to iterate over this ptable and do stuff.
Is there anyway I can access the table to loop through and find running processes, or is there another approach I should be considering?
Accessing it directly outside of proc.c will not work. Fortunately for you, calling a proc.c function that accesses the ptable will work. If you look at sysproc.c, it makes calls to functions that are written inside of proc.c, which do use the ptable.
For example, you can look at the 'exit' function and see where the relevant code is in xv6 to get an idea of how a syscall that uses a ptable could work:
proc.c: //Implementation of exit function
...
2353 void
2354 exit(void)
2355 {
...
2379 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
...
2391 }
The exit function, in proc.c, is able to loop through the process table which is instantiated in proc.c.
defs.h:
...
0357 // proc.c
0358 struct proc* copyproc(struct proc*);
0359 void exit(void);
0360 int fork(void);
...
The defs.h file contains a forward declaration of the exit function.
sysproc.c: //Implementation of a syscall 'exit'
...
3414 int
3415 sys_exit(void)
3416 {
3417 exit();
3418 return 0; // not reached
3419 }
The sys_exit function sysproc.c contains the full implementation of the exit syscall, which calls exit() from the proc.c, which at one point interates over the ptable.
If you're planning on iterating over the ptable, you'd want to consider writing a function your syscall can call to do whatever it needs to do, which is why I included the exit syscall as an example.
Hope this helps with your understanding!