home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / SH164X.ZIP / JOBS.C < prev    next >
C/C++ Source or Header  |  1990-09-07  |  5KB  |  249 lines

  1. /* Display running background processes
  2.  * Kai Uwe Rommel
  3.  * Sat 04-Aug-1990
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include <sys/types.h>
  11. #include <setjmp.h>
  12. #include <unistd.h>
  13. #include <dir.h>
  14. #include "sh.h"
  15.  
  16. #define INCL_NOPM
  17. #define INCL_DOSPROCESS
  18. #define INCL_DOSMODULEMGR
  19. #include <os2.h>
  20.  
  21.  
  22. extern USHORT APIENTRY DosQProcStatus(PVOID pBuf, USHORT cbBuf);
  23.  
  24.  
  25. struct process
  26. {
  27.   USHORT pid;
  28.   USHORT ppid;
  29.   USHORT threads;
  30.   USHORT children;
  31.   USHORT modhandle;
  32.   USHORT module;
  33. };
  34.  
  35.  
  36. struct module
  37. {
  38.   USHORT modhandle;
  39.   USHORT max_dependents;
  40.   USHORT *dependents;
  41.   UCHAR  *modname;
  42. };
  43.  
  44.  
  45. struct process **procs = NULL;
  46. struct module  **mods  = NULL;
  47.  
  48. USHORT max_procs = 0;
  49. USHORT cur_procs = 0;
  50. USHORT max_mods  = 0;
  51. USHORT cur_mods  = 0;
  52.  
  53.  
  54. int parse_processes(UCHAR * bBuf)
  55. {
  56.   USHORT sel, offs;
  57.   USHORT type, tpid;
  58.   USHORT count, kount;
  59.   UCHAR buffer[256];
  60.   UCHAR *cptr, *ptr;
  61.  
  62.   ptr = bBuf;
  63.   sel = SELECTOROF(ptr);
  64.  
  65.   while ( (type = *(USHORT *) ptr) != 0xFFFFU )
  66.   {
  67.     ptr += 2;
  68.     offs = *(USHORT *) ptr;
  69.     ptr += 2;
  70.  
  71.     switch ( type )
  72.     {
  73.  
  74.     case 0: /* process */
  75.  
  76.       if ( cur_procs >= max_procs )
  77.       {
  78.         max_procs += 50;
  79.  
  80.     if ( !(procs = realloc(procs, max_procs * sizeof(struct process *))) )
  81.           return 1;
  82.       }
  83.  
  84.       if ( !(procs[cur_procs] = calloc(1, sizeof(struct process))) )
  85.         return 1;
  86.  
  87.       procs[cur_procs] -> pid = *(USHORT *) ptr;
  88.       ptr += 2;
  89.       procs[cur_procs] -> ppid = *(USHORT *) ptr;
  90.       ptr += 2;
  91.       ptr += 2;
  92.       procs[cur_procs] -> modhandle = *(USHORT *) ptr;
  93.  
  94.       procs[cur_procs] -> threads = 0;
  95.       ++cur_procs;
  96.  
  97.       break;
  98.  
  99.     case 1: /* thread */
  100.  
  101.       ptr += 2;
  102.       tpid = *(USHORT *) ptr;
  103.  
  104.       for ( count = 0; count < cur_procs; count++ )
  105.     if ( procs[count] -> pid == tpid )
  106.     {
  107.       ++procs[count] -> threads;
  108.       break;
  109.     }
  110.  
  111.       break;
  112.  
  113.     case 2: /* module */
  114.  
  115.       if ( cur_mods >= max_mods )
  116.       {
  117.         max_mods += 50;
  118.  
  119.     if ( !(mods = realloc(mods, max_mods * sizeof(struct module *))) )
  120.           return 1;
  121.       }
  122.  
  123.       if ( !(mods[cur_mods] = calloc(1, sizeof(struct module))) )
  124.         return 1;
  125.  
  126.       mods[cur_mods] -> modhandle = *(USHORT *) ptr;
  127.       ptr += 2;
  128.       mods[cur_mods] -> max_dependents = *(USHORT *) ptr;
  129.       ptr += 2;
  130.       ptr += 2;
  131.       ptr += 2;
  132.  
  133.       if ( mods[cur_mods] -> max_dependents )
  134.       ptr += (mods[cur_mods] -> max_dependents) * 2;
  135.  
  136.       for ( cptr = buffer; *cptr++ = *ptr++; );
  137.  
  138.       if ( !(mods[cur_mods] -> modname = strdup(buffer)) )
  139.         return 1;
  140.  
  141.       ++cur_mods;
  142.  
  143.       break;
  144.  
  145.     case 3: /* system semaphore */
  146.       break;
  147.  
  148.     case 4: /* shared memory */
  149.       break;
  150.  
  151.     }
  152.  
  153.     ptr = MAKEP(sel, offs);
  154.   }
  155.  
  156.   for ( count = 0; count < cur_procs; count++ )
  157.     for ( kount = 0; kount < cur_mods; kount++ )
  158.       if ( procs[count] -> modhandle == mods[kount] -> modhandle )
  159.       {
  160.         procs[count] -> module = kount;
  161.     break;
  162.       }
  163.  
  164.   for ( count = 0; count < cur_procs; count++ )
  165.     for ( kount = 0; kount < cur_procs; kount++ )
  166.       if ( procs[count] -> pid == procs[kount] -> ppid )
  167.     (procs[count] -> children)++;
  168.  
  169.   return 0;
  170. }
  171.  
  172.  
  173. void proctree(int pid, int indent)
  174. {
  175.   USHORT count;
  176.   UCHAR *mName, pName[256];
  177.  
  178.   for (count = 0; count < cur_procs; count++)
  179.     if ( procs[count] -> ppid == pid )
  180.     {
  181.       if ( procs[count] -> module )
  182.       {
  183.         mName = mods[procs[count] -> module] -> modname;
  184.         DosGetModName(procs[count] -> modhandle, sizeof(pName), pName);
  185.       }
  186.       else
  187.       {
  188.         mName = "unknown";  /* Zombie process, i.e. result for DosCwait() */
  189.         pName[0] = 0;
  190.       }
  191.  
  192.       printf("[%d]\t%2d %-8s %*s%s\n", procs[count] -> pid,
  193.         procs[count] -> threads, mName, indent, "", pName);
  194.  
  195.       proctree(procs[count] -> pid, indent + 2);
  196.     }
  197. }
  198.  
  199.  
  200. int compare(struct process **p1, struct process **p2)
  201. {
  202.   return (*p1) -> pid - (*p2) -> pid;
  203. }
  204.  
  205.  
  206. void dojobs(C_Op *t)
  207. {
  208.   UCHAR *pBuf;
  209.   USHORT count;
  210.  
  211.   pBuf = malloc(0x2000);
  212.   DosQProcStatus(pBuf, 0x2000);
  213.  
  214.   if ( parse_processes(pBuf) )
  215.   {
  216.     printf("Error: Out of memory 2!\n");
  217.     DosExit(EXIT_PROCESS, 1);
  218.   }
  219.  
  220.   free(pBuf);
  221.  
  222.   qsort(procs, cur_procs, sizeof(struct process *), compare);
  223.   proctree(getpid(), 0);
  224.  
  225.   for (count = 0; count < cur_procs; count++)
  226.     free(procs[count]);
  227.  
  228.   for (count = 0; count < cur_mods; count++)
  229.   {
  230.     free(mods[count] -> modname);
  231.     free(mods[count]);
  232.   }
  233.  
  234.   free(procs);
  235.   free(mods);
  236.  
  237.   max_procs = max_mods = cur_procs = cur_mods = 0;
  238.   procs = NULL;
  239.   mods = NULL;
  240. }
  241.  
  242.  
  243. #ifdef TEST
  244. void main(void)
  245. {
  246.   dojobs(NULL);
  247. }
  248. #endif
  249.