I'm definately not an expert on the subject, but I did something very similar for an assignment last semester when we were required to take snapshots of processes. Unfortunately this method requires digging into the kernel which is probably not what you want to do.
I found this article helpful.
Anyways here are some snippets.
write_lock_irq(&tasklist_lock);
for_each_process(task) {
if (system_or_user == 0)
print_mem_user(task);
if (system_or_user == 1)
print_mem_system(task);
}
write_unlock_irq(&tasklist_lock);
The idea you need to lock down some data structures or sometimes the kernel will hang. "for_each_process" is a macro defined somewhere but I don't remember how it works D:
static void print_mem_system(struct task_struct *task)
{
struct mm_struct *mm;
if (task -> mm == NULL){ // this is how you distinguish system processes from user processes
myarraypid[totalnumberofprocesses] = task -> pid; // store process id's into myarraypid[], which you can later copy back to user space for printing/display. Additional information would be found in a "task_struct" which is Linux's implementation of a process.
}
}
Some of my classmates took different approaches and dived into the source of the "ps" utility. I believe I was working on Linux 2.6.18-92.1.13.e15. Disclaimer: This worked for me but your mileage may vary. I could very well be off the wall and I don't want to lead you down the wrong direction.