title: Number of Running Processes on a Minix system from C code
body: <p>So, this seemed simple at first, but after crawling Google and here, the answer doesn't seem as simple as I first thought.</p>
  
  <p>Basically, I'm editing a MINIX kernel as part of a practical for my Operating Systems course, and I have to add a little function that spits out the number of running processes when you hit a function key in the Information Server. I've figured out how to integrate the functionality so all the other stuff works, but for the life of me, I can not figure out how to get the current number of processes running in the system into my C code and into a variable to print out.</p>
  
  <p>First I thought there'd be a nifty Syscall like SYS_NUMPROCS or something that'd return the value, but no luck.
  Then, I tried piping output from a system("ps -ax | wc -l") to a file and the file wouldn't create. I tried using popen() and no luck there either - even with a simple "ls" read into a buffer, it just bombs the code and "hangs" the run of the code, so there's no output.</p>
  
  <p>So now I'm truly stumped, and any help would be super awesome, because at this point I've exhausted all the obvious options.</p>
  
  <p>The only two things I can think of now would be a loop counting all the processes, but first you have to get to the system's process list, and I've heard vague things said about /proc/ as a directory, but I haven't a clue how to access/run through that or how it'd link up to getting the number of processes in the first place.</p>
  
  <p>Thanks a stack (lol pun), guys :)</p>
  
  <p>Also, I haven't included code explicitly because nothing I've written aside from basic printf'ing for cosmetic output, because none of the things I've tried gave me any joy :/</p>
  
  <p>Edit notes: Guys, this is a kernel edit - I'm writing the function to printf the information in a system C file, then recompiling the kernel and rebooting the system to test. It's a UNIX (MINIX) kernel, not a Linux kernel, and it's not a user mode program.</p>
  
  <p>My code for popen(), as some of you requested, is as follows:</p>
  
  <pre><code>public void cos_dmp(){
      char buffer[512];
      FILE * f;
  
      f = popen("ps -ax | wc -l","r");
  
      fgets(buffer, sizeof(buffer),f);
  
    //buffer should now contain result of popen()
  
       printf(buffer);
  }
  </code></pre>
  
  <p>That's a bit of a hacked together version from what I remember and keeping it ultra simple and showing you guys that's what I was trying to do. Again though, there must be a better way to do this aside from essentially calling the output of a system() call.</p>
  
  <p>Edit again: the above code woks perfectly from a user program but won't work from the kernel function. Anybody have an idea why?:/</p>