home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / proc.zip / proc2.c < prev    next >
C/C++ Source or Header  |  1994-08-13  |  10KB  |  399 lines

  1. /* PROC2.C
  2.  *
  3.  * Sample program using the DosQPocStatus() function
  4.  * for OS/2 2.x
  5.  *
  6.  * Kai Uwe Rommel - Wed 25-Mar-1992
  7.  *                  Sat 13-Aug-1994
  8.  *
  9.  * can be compiled with
  10.  * - 16-bit compiler in small and large data models (MS C 6.00A tested)
  11.  * - 32-bit compiler (IBM C Set++ tested)
  12.  */
  13.  
  14. #define INCL_NOPM
  15. #define INCL_DOSPROCESS
  16. #define INCL_DOSMODULEMGR
  17. #define INCL_DOSMEMMGR
  18. #include <os2.h>
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24.  
  25. #if defined(__32BIT__)
  26.   #define PTR(ptr, ofs)  ((void *) ((char *) (ptr) + (ofs)))
  27. #else
  28.   #define DosQueryModuleName DosGetModName
  29.   #define APIENTRY16 APIENTRY
  30.   #if defined(M_I86SM) || defined(M_I86MM)
  31.     #define PTR(ptr, ofs)  ((void *) ((char *) (ptr) + (ofs)))
  32.   #else
  33.     #define PTR(ptr, ofs)  ((void *) ((char *) (((ULONG) procstat & 0xFFFF0000) | (USHORT) (ptr)) + (ofs)))
  34.     /* kludge to transform 0:32 into 16:16 pointer in this special case */
  35.   #endif
  36. #endif
  37.  
  38.  
  39.  
  40. /* DosQProcStatus() = DOSCALLS.154 */
  41. USHORT APIENTRY16 DosQProcStatus(PVOID pBuf, USHORT cbBuf);
  42.  
  43. /* DosGetPrty = DOSCALLS.9 */
  44. USHORT APIENTRY16 DosGetPrty(USHORT usScope, PUSHORT pusPriority, USHORT pid);
  45.  
  46.  
  47. struct procstat
  48. {
  49.   ULONG  summary;
  50.   ULONG  processes;
  51.   ULONG  semaphores;
  52.   ULONG  unknown1;
  53.   ULONG  sharedmemory;
  54.   ULONG  modules;
  55.   ULONG  unknown2;
  56.   ULONG  unknown3;
  57. };
  58.  
  59.  
  60. struct process
  61. {
  62.   ULONG  type;
  63.   ULONG  threadlist;
  64.   USHORT processid;
  65.   USHORT parentid;
  66.   ULONG  sessiontype;
  67.   ULONG  status; /* see status #define's below */
  68.   ULONG  sessionid;
  69.   USHORT modulehandle;
  70.   USHORT threads;
  71.   ULONG  reserved1;
  72.   ULONG  reserved2;
  73.   USHORT semaphores;
  74.   USHORT dlls;
  75.   USHORT shrmems;
  76.   USHORT reserved3;
  77.   ULONG  semlist;
  78.   ULONG  dlllist;
  79.   ULONG  shrmemlist;
  80.   ULONG  reserved4;
  81. };
  82.  
  83. #define STAT_EXITLIST 0x01
  84. #define STAT_EXIT1    0x02
  85. #define STAT_EXITALL  0x04
  86. #define STAT_PARSTAT  0x10
  87. #define STAT_SYNCH    0x20
  88. #define STAT_DYING    0x40
  89. #define STAT_EMBRYO   0x80
  90.  
  91.  
  92. struct thread
  93. {
  94.   ULONG  type;
  95.   USHORT threadid;
  96.   USHORT threadslotid;
  97.   ULONG  blockid;
  98.   ULONG  priority;
  99.   ULONG  systime;
  100.   ULONG  usertime;
  101.   UCHAR  status; /* see status #define's below */
  102.   UCHAR  reserved1;
  103.   USHORT reserved2;
  104. };
  105.  
  106. #define TSTAT_READY   1
  107. #define TSTAT_BLOCKED 2
  108. #define TSTAT_RUNNING 5
  109. #define TSTAT_LOADED  9
  110.  
  111. char *threadstat[256] = {NULL, "Ready", "Blocked", NULL, NULL, "Running", 
  112.             NULL, NULL, NULL, "Loaded"};
  113.  
  114.  
  115. struct module
  116. {
  117.   ULONG  nextmodule;
  118.   USHORT modhandle;
  119.   USHORT modtype;
  120.   ULONG  submodules;
  121.   ULONG  segments;
  122.   ULONG  reserved;
  123.   ULONG  namepointer;
  124.   USHORT submodule[1];  /* varying, see member submodules */
  125. };
  126.  
  127.  
  128. struct semaphore
  129. {
  130.   ULONG  nextsem;
  131.   USHORT owner;
  132.   UCHAR  flag;
  133.   UCHAR  refs;
  134.   UCHAR  requests;
  135.   UCHAR  reserved1;
  136.   ULONG  reserved2;
  137.   USHORT reserved3;
  138.   USHORT index;
  139.   USHORT dummy;
  140.   UCHAR  name[1];       /* varying */
  141. };
  142.  
  143.  
  144. struct shmem
  145. {
  146.   ULONG  nextseg;
  147.   USHORT handle;
  148.   USHORT selector;
  149.   USHORT refs;
  150.   UCHAR  name[1];       /* varying */
  151. };
  152.  
  153.  
  154. struct procstat *procstat;
  155.  
  156.  
  157. void timestr(ULONG time, char *buffer)
  158. {
  159.   ULONG seconds, hundredths;
  160.  
  161.   seconds = time / 32;
  162.   hundredths = (time % 32) * 100 / 32;
  163.  
  164.   sprintf(buffer, "%ld:%02ld.%02ld", seconds / 60, seconds % 60, hundredths);
  165. }
  166.  
  167.  
  168. void proctree(USHORT pid, USHORT indent)
  169. {
  170.   struct process *proc;
  171.   UCHAR name[256], time[32];
  172.   USHORT prty = 0;
  173.   struct thread *thread;
  174.   ULONG cpu = 0;
  175.   int i;
  176.  
  177.   for ( proc = PTR(procstat -> processes, 0);
  178.         proc -> type != 3; /* not sure if there isn't another termination */
  179.         proc = PTR(proc -> threadlist,                          /* method */
  180.                    proc -> threads * sizeof(struct thread))
  181.       )
  182.   {
  183.     if ( proc -> parentid == pid && proc -> type != 0 )
  184.     {
  185.       DosQueryModuleName(proc -> modulehandle, sizeof(name), name);
  186.       DosGetPrty(PRTYS_PROCESS, &prty, proc -> processid);
  187.  
  188.       for ( cpu = 0, i = 0, thread = PTR(proc -> threadlist, 0);
  189.        i < proc -> threads; i++, thread++ )
  190.     cpu += (thread -> systime + thread -> usertime);
  191.  
  192.       timestr(cpu, time);
  193.  
  194.       printf("%5d  %5d   %02lX  %4d %04X %10s %*s%s\n",
  195.         proc -> processid, proc -> parentid, proc -> sessionid,
  196.         proc -> threads, prty, time, indent, "", name);
  197.  
  198.       proc -> type = 0;  /* kludge, mark it as already printed */
  199.       proctree(proc -> processid, indent + 2);
  200.     }
  201.   }
  202.  
  203.   if ( pid != 0 )
  204.     return;
  205.  
  206.   /* if at the root level, check for those processes that have lost *
  207.    * their parent process and show them as if they were childs of 0 */
  208.  
  209.   for ( proc = PTR(procstat -> processes, 0);
  210.         proc -> type != 3; /* not sure if there isn't another termination */
  211.         proc = PTR(proc -> threadlist,                          /* method */
  212.                    proc -> threads * sizeof(struct thread))
  213.       )
  214.   {
  215.     if ( proc -> type != 0 )
  216.     {
  217.       DosQueryModuleName(proc -> modulehandle, sizeof(name), name);
  218.       DosGetPrty(PRTYS_PROCESS, &prty, proc -> processid);
  219.  
  220.       for ( cpu = 0, i = 0, thread = PTR(proc -> threadlist, 0);
  221.        i < proc -> threads; i++, thread++ )
  222.     cpu += (thread -> systime + thread -> usertime);
  223.  
  224.       timestr(cpu, time);
  225.  
  226.       printf("%5d  %5d   %02lX  %4d %04X %10s %*s%s\n",
  227.         proc -> processid, proc -> parentid, proc -> sessionid,
  228.         proc -> threads, prty, time, indent, "", name);
  229.  
  230.       proctree(proc -> processid, indent + 2);
  231.     }
  232.  
  233.     proc -> type = 1;    /* kludge, reset mark */
  234.   }
  235. }
  236.  
  237.  
  238. void threadlist(void)
  239. {
  240.   struct process *proc;
  241.   struct thread *thread;
  242.   UCHAR name[256], systime[32], usertime[32], *status;
  243.   USHORT count;
  244.  
  245.   for ( proc = PTR(procstat -> processes, 0);
  246.         proc -> type != 3;
  247.         proc = PTR(proc -> threadlist,
  248.                    proc -> threads * sizeof(struct thread))
  249.       )
  250.   {
  251.     DosQueryModuleName(proc -> modulehandle, sizeof(name), name);
  252.     printf("%s\n", name);
  253.  
  254.     for ( count = 0, thread = PTR(proc -> threadlist, 0);
  255.           count < proc -> threads; count++, thread++ )
  256.     {
  257.       timestr(thread -> systime, systime);
  258.       timestr(thread -> usertime, usertime);
  259.       status = threadstat[thread -> status];
  260.       printf("%5d  %5d  %04lX %-7.7s %08lX %10s %10s\n", 
  261.          thread -> threadid, thread -> threadslotid, thread -> priority, 
  262.          status ? status : "Unknown", thread -> blockid, systime, usertime);
  263.     }
  264.   }
  265. }
  266.  
  267.  
  268. void modlist(int imports)
  269. {
  270.   struct module *mod;
  271.   UCHAR name[256];
  272.   ULONG count;
  273.  
  274.   for ( mod = PTR(procstat -> modules, 0); ;
  275.         mod = PTR(mod -> nextmodule, 0)
  276.       )
  277.   {
  278.     if ( !imports || mod -> submodules )
  279.       if ( imports )
  280.       {
  281.         printf("%04X  %s\n", mod -> modhandle, PTR(mod -> namepointer, 0));
  282.  
  283.         for ( count = 0; count < mod -> submodules; count++ )
  284.         {
  285.           DosQueryModuleName(mod -> submodule[count], sizeof(name), name);
  286.           printf("  %04X  %s\n", mod -> submodule[count], name);
  287.         }
  288.       }
  289.       else
  290.         printf(" %04X   %2d  %7ld  %6ld  %s\n", mod -> modhandle,
  291.                mod -> modtype ? 32 : 16, mod -> segments,
  292.                mod -> submodules, PTR(mod -> namepointer, 0));
  293.  
  294.     if ( mod -> nextmodule == 0L )
  295.       break;
  296.   }
  297. }
  298.  
  299.  
  300. void semlist(void)
  301. {
  302.   struct semaphore *sem;
  303.  
  304.   for ( sem = PTR(procstat -> semaphores, 16);
  305.         sem -> nextsem != 0L;
  306.         sem = PTR(sem -> nextsem, 0)
  307.       )
  308.     printf("%3d  %3d   %02X  %4d   %04X  \\SEM%s\n",
  309.            sem -> refs, sem -> requests, sem -> flag,
  310.            sem -> owner, sem -> index, sem -> name);
  311. }
  312.  
  313.  
  314. void shmlist(void)
  315. {
  316.   struct shmem *shmem;
  317.   UCHAR name[256];
  318.   ULONG size;
  319. #if defined(__32BIT__)
  320.   ULONG attrib;
  321.   PVOID base;
  322. #else
  323.   SEL sel;
  324. #endif
  325.  
  326.   for ( shmem = PTR(procstat -> sharedmemory, 0);
  327.         shmem -> nextseg != 0L;
  328.         shmem = PTR(shmem -> nextseg, 0)
  329.       )
  330.   {
  331.     strcpy(name, "\\SHAREMEM\\");
  332.     strcat(name, shmem -> name);
  333.  
  334. #if defined(__32BIT__)
  335.     DosGetNamedSharedMem(&base, name, PAG_READ);
  336.     size = -1;
  337.     attrib = PAG_SHARED | PAG_READ;
  338.     if ( DosQueryMem(base, &size, &attrib) )
  339.       size = 0;
  340.     DosFreeMem(base);
  341. #else
  342.     DosGetShrSeg(name, &sel);
  343.     if ( DosSizeSeg(sel, &size) )
  344.       size = 0;
  345.     DosFreeSeg(sel);
  346. #endif
  347.  
  348.     printf(" %04X   %04X   %08lX  %4d  %s\n", shmem -> handle,
  349.            shmem -> selector, size, shmem -> refs, name);
  350.   }
  351. }
  352.  
  353.  
  354. void main(void)
  355. {
  356.   procstat = malloc(0x8000);
  357.   DosQProcStatus(procstat, 0x8000);
  358.  
  359.   printf("\n» Process list\n");
  360.   printf("\n PID    PPID  SESS THRD PRIO    TIME    PROGRAM");
  361.   printf("\n------ ------ ---- ---- ---- ---------- ------->\n");
  362.  
  363.   proctree(0, 0);
  364.  
  365.   printf("\n» Thread list\n");
  366.   printf("\n TID    TSID  PRIO  STATE  BLOCKID   SYSTIME    USERTIME");
  367.   printf("\n------ ------ ---- ------- -------- ---------- ----------\n");
  368.  
  369.   threadlist();
  370.  
  371.   printf("\n» Module list\n");
  372.   printf("\nHANDLE TYPE SEGMENTS IMPORTS PATH");
  373.   printf("\n------ ---- -------- ------- ---->\n");
  374.  
  375.   modlist(0);
  376.  
  377.   printf("\n» Module tree\n");
  378.   printf("\n--------------\n");
  379.  
  380.   modlist(1);
  381.  
  382.   printf("\n» Semaphore list\n");
  383.   printf("\nREF  REQ  FLAG OWNER INDEX  NAME");
  384.   printf("\n---- ---- ---- ----- ------ ---->\n");
  385.  
  386.   semlist();
  387.  
  388.   printf("\n» Shared memory list\n");
  389.   printf("\nHANDLE  SEL    SEGSIZE    REF  NAME");
  390.   printf("\n------ ------ ---------- ----- ---->\n");
  391.  
  392.   shmlist();
  393.  
  394.   exit(0);
  395. }
  396.  
  397.  
  398. /* End of PROCS.C */
  399.