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