Advertisement
Jakzon123

pstree.c

Feb 24th, 2023
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. #include "types.h"
  2. #include "stat.h"
  3. #include "user.h"
  4. #include "uproc.h"
  5.  
  6. // create maxprocs const
  7. #define MAXPROCS 64
  8.  
  9. int main(int argc, char *argv[]) {
  10.     // create uproc struct with MAXPROCS entries
  11.     struct uproc *uproc = malloc(MAXPROCS*sizeof(struct uproc));
  12.  
  13.     // get the number of processes currently running
  14.     int proc_num = getprocs(MAXPROCS, uproc);
  15.  
  16.     // if an error ocurs during getprocs, deal with it here based on error code
  17.     if (proc_num == -1) {
  18.         // int pass error occurred, print appropriate statement and exit
  19.         printf(1, "Kernel returned an error while passing an integer.\n");
  20.         exit();
  21.     } else if (proc_num == -2) {
  22.         // ptr pass error occurred, print appropriate statement and exit
  23.         printf(1, "Kernel returned an error while passing a pointer.\n");
  24.         exit();
  25.     }
  26.  
  27.     // iterate through the number of currently-running processes
  28.     // for each process, determine if the pid is the root
  29.     // once root is found, determine if they are connected to the root
  30.     // else, print at third level
  31.     // declare counter i and set to zero
  32.     int i = 0;
  33.  
  34.     for (; i < proc_num; i++) {
  35.         if (uproc[i].pid == 1) {
  36.             // this is the root node, print as such
  37.             printf(1, "%s[%d]\n", uproc[i].name, uproc[i].pid-1);
  38.         } else if (uproc[i].ppid == 1) {
  39.             // this is a CHILD of the root node, print as such
  40.             printf(1, "%8s[%d]\n", uproc[i].name, uproc[i].pid-1);
  41.         } else {
  42.             // this is NOT a root node NOR is it a child of root
  43.             // print out generic
  44.             printf(1, "        %s[%d]\n", uproc[i].name, uproc[i].pid-1);
  45.         }
  46.  
  47.     }
  48.     // process is done, end now
  49.     exit();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement