home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / RUNNING.ZIP / RUNNING.DOC < prev    next >
Text File  |  1990-02-12  |  17KB  |  501 lines

  1. RUNNING: Shows Processes Running in OS/2
  2. ----------------------------------------
  3.  
  4. ** Version 1.02 has changed the output format and removed the -p flag **
  5.  
  6.  
  7. This program is released to the public domain and may be distributed
  8. freely by any means.
  9.  
  10. I have included the source code to RUNNING and to SLAY.  Please feel
  11. free to use them as long as the code is not used for commercial
  12. profit.  I would appreciate receiving copies of modifications at any
  13. of the below addresses.
  14.  
  15. RUNNING is a simple program which provides a listing of all processes
  16. currently being used by OS/2.  It provides information about the
  17. processes such as their PID, their PARENT PID, the internal module ID
  18. #, the number of children processes, the number of threads currently
  19. running in each process, and dependency links to resources. 
  20. Resources are DLL's or font files.
  21.  
  22. Here is a sample of a listing of processes (not resources):
  23.  
  24.                                               ┌──────── Threads ────────┐
  25. PID   PPID  Mod#  Process Name          Chld  Str Rdy Blk Fzn Crt Bkd Oth
  26. ----  ----  ----  --------------------  ----  --- --- --- --- --- --- ---
  27. 00c1  0012  025c  BRIEF                   1            2                
  28. 000f  0005  0457  CLOCK2                  0            1                
  29. 001c  0005  0271  CMD                     0            1                
  30. 000d  0005  0494  DESK                    0        2                    
  31. 000e  0005  0476  FREEMEM                 0        1                    
  32. 0006  0005  01fb  HARDERR                 0            3                
  33. 000c  0005  03b7  PMEXEC                  0            1                
  34. 0005  0000  016a  PMSHELL                11           12                
  35. 0008  0005  02d7  PMSPOOL                 2            2                
  36. 0004  0000  0174  QH                      0            8                
  37. 000b  0008  0393  SPLDVWRK                0            2                
  38. 000a  0008  0259  SPLPRM                  0            6                
  39. 0007  0005  0304  VIOHELP                 0            1                
  40.  
  41. The PID is the process ID which might be used by a program such as
  42. SLAY (Included with RUNNING) or KILL (Copyright (c) 1988, Ed Braaten and 
  43. available on many BBSs - inconvenient because it accepts PIDs in
  44. decimal format instead of hexadecimal format) to totally remove a 
  45. process from the system.  The PPID is the parent process' PID and 
  46. the module # is a number used internally to identify the process.  
  47. The process name is the NAME name from the DEF file when the process is 
  48. linked.  The number of child processes are followed by the number of
  49. threads categorized by state as follows:
  50.  
  51. Str - Start                       Crt - Critical Section
  52. Rdy - Ready                       Bkd - Background
  53. Blk - Blocked                     Oth - Other (Zombie, boost)
  54. Frz - Frozen
  55.  
  56. If you invoke RUNNING with the -r flag, you will get a listing of the
  57. module dependencies and the resource module listing.  It will look like
  58. this:
  59.  
  60. PID   PPID  Mod#  Process Name          Chld  Str Rdy Blk Fzn Crt Bkd Oth
  61. ----  ----  ----  --------------------  ----  --- --- --- --- --- --- ---
  62. 00c1  0012  025c  BRIEF                   1            2                
  63. ****  Depends on: 0084 0085 007b 0046 0178
  64.                   007c 008f
  65. 000f  0005  0457  CLOCK2                  0            1                
  66. ****  Depends on: 0084 0153 0154 0158
  67. 0006  0005  01fb  HARDERR                 0            3                
  68. ****  Depends on: 0084 007b 0058 008f 0085
  69.                   0072 007c
  70. 000c  0005  03b7  PMEXEC                  0            1                
  71. ****  Depends on: 0158 0084 0153 0154 0157
  72.                   015c
  73. 0007  0005  0304  VIOHELP                 0            1                
  74. ****  Depends on: 031a 0084 0153 015c
  75.  
  76. Resources (DLLs) In Use:
  77. Handle  Resource Name         Dependencies
  78. ------  --------------------  -----------------------------------------------
  79. 0057    ANSICALL               0084 007b 007c
  80. 0037    BKSCALLS               0057 0084 007b 0085 007c
  81. 0204    BMSCALLS               0084 0058 007c
  82. 0071    BVSCALLS               0057 0084 0058 0085 007c
  83. 02a8    COURIER                <None>
  84. 0116    DISPLAY                007c 0084 0058 0085 0153 013f
  85. 0084    DOSCALL1               0000 007b 0085 007c
  86. 00c0    DOSCALLS               <None>
  87. 0154    PMGPI                  0084 0085 013f 012f 0153
  88. 0158    PMSHAPI                0084 0085 0157 0153
  89. 02e8    PMSPL                  0084 0153
  90. 0153    PMWIN                  0116 0084 0085 0154 013f 0157 0158
  91. ....
  92.  
  93. We can see that CLOCK2 above uses DOSCALL1, PMWIN, PMGPI, and PMSHAPI as
  94. its resources (0084, 0154, 0158, 0153).
  95.  
  96. RUNNING has the following command line flags:
  97.  
  98.     -r   causes resources to be shown
  99.     -n   supresses sorting by name.
  100.     -?   shows the options (as does -h)
  101.  
  102.  
  103. SLAY is a program which simply attempts to kill the process that you
  104. indicate on the command line.  The PID that you pass to SLAY MUST BE
  105. HEXIDECIMAL (e.g. slay b3 or slay 18d) NOT DECIMAL!
  106.  
  107. RUNNING uses the undocumented function DosQProcStatus() which is
  108. used by the programs PSTAT.EXE and PS.EXE which are provided with
  109. OS/2.  This function was brought to my attention by Franz Krainer in
  110. a message in the OS2 echo (10-19-89), and helped fulfil a need that I
  111. have had for a long time (since I moved over to OS/2 from QNX) to
  112. know what exactly is running in my system at any moment.  The
  113. function works with OS/2 1.1 and should also work with version 1.2.
  114.  
  115. Some folks have had problems with RUNNING when using IBM's OS/2 1.1 EE
  116. which seems to be cleared up when they installed IBM's patches dated
  117. 9 Feb 1989.
  118.  
  119. If you have questions or comments, you can send them to me Netmail at
  120. my BBS, The Programmer's Oasis (1:151/402).  You can also call the
  121. BBS at (919)-226-6984 (North Carolina).  You can drop me a line through
  122. BIX (ID=laforet) or GEnie (ID=XTX74591). Finally, you can Email me
  123. through Compuserve (ID 76120,110).
  124.  
  125. February 12, 1990
  126.  
  127.  
  128.  
  129. --------------------------------------------------------------------
  130. Information for DosQProcStatus()
  131. --------------------------------------------------------------------
  132.  
  133. The following information is derived from key messages in the OS/2
  134. echomail available on many fine BBS's and may help to explain more
  135. about the underlying function in RUNNING:  
  136.  
  137.  
  138. Date:  10-19-89  00:29
  139. From:  Franz Krainer
  140. To:    All
  141. Subj:  More About The Function Behind Ps.exe And Pstat.exe
  142.  
  143. The undocumented function in OS/2 v1.2 which is used by PSTAT.EXE and PS.EXE
  144. to get system information about processes, threads etc. has to be declared in
  145. the following way:
  146. --------------------------------------------------------------------------
  147. /***   DosQProcStatus
  148.  *
  149.  *   Fills a buffer with system information about threads, processes,
  150.  *   dynylink-libraries, system semaphores and named shared segments.
  151.  *
  152.  */
  153. USHORT APIENTRY DosQProcStatus(
  154.         PVOID pBuf,               /* address of a transfer buffer  */
  155.         USHORT cbBuf);            /* size of buffer in bytes       */
  156. --------------------------------------------------------------------------
  157. pBuf  is the adress of a buffer, cbBuf  is the size of the buffer.
  158. OS/2 fills this buffer with system information. The amount of information you
  159. will get depends on how many system resources are actually used.
  160. The size of the buffer (and therefore the value of cbBuf) should be around 4
  161. kBytes. This should be enough, even in the case of a heavy loaded system.
  162. The data you will get back is structured as a linked list. Each entry starts
  163. with a 16-bit code (0001 = thread information entry, 0004 = named shared
  164. segment etc.). The second 16-bit value is the pointer to the next entry
  165. followed by specific information about the entry.
  166. Franz.
  167. --- FD 2.00
  168.  * Origin: Ockham's Razor (Vienna/Austria) (2:310/11.17)
  169.  
  170.  
  171. -------------------------
  172. Date:  10-21-89
  173. From:  Chris Laforet
  174. To:    All
  175. Subj:  Re: More About The Function Behind Ps.exe And Pstat.exe
  176.  
  177. In a message by Franz Krainer dated 10-19-89 @ 0:29:
  178.  
  179. > The undocumented function in OS/2 v1.2 which is used by PSTAT.EXE and 
  180. > PS.EXE to get system information about processes, threads etc. has to 
  181. > be declared in the following way:
  182. >
  183. > USHORT APIENTRY DosQProcStatus(
  184. >         PVOID pBuf,               /* address of a transfer buffer  */
  185. >         USHORT cbBuf);            /* size of buffer in bytes       */
  186. >
  187. > pBuf  is the adress of a buffer, cbBuf  is the size of the buffer.
  188. > OS/2 fills this buffer with system information. 
  189.  
  190. I have been searching for a way to discover the processes running on an OS/2
  191. system for quite a while and was happy to find this information.  I have been
  192. playing with it and have put together a simple program to help dump the
  193. information:
  194.  
  195. --------------- Start -----------------
  196. /* Compile with -AS -Lp */
  197.  
  198. #define INCL_DOS
  199. #include <stdio.h>
  200. #include <os2.h>
  201.  
  202. extern USHORT APIENTRY DosQProcStatus(PVOID pBuf,USHORT cbBuf);
  203. BYTE bBuf[0x2000];
  204.  
  205. int main(void)
  206.   {
  207.   USHORT count;
  208.   USHORT kount;
  209.   USHORT fold;
  210.   USHORT type;
  211.   USHORT next = 0;
  212.   PIDINFO pid;
  213.  
  214.   DosGetPID(&pid);
  215.   printf("Process ID  = 0x%04x\n",pid.pid);
  216.   printf("Process TID = 0x%04x\n",pid.tid);
  217.   printf("Parent ID   = 0x%04x\n",pid.pidParent);
  218.   if (!DosQProcStatus(bBuf,sizeof(bBuf)))
  219.     {
  220.     printf("Buffer address is %p:\n\n",bBuf);
  221.     do
  222.       {
  223.       printf("[Buffer + 0x%04x] : ",next);
  224.       if ((type = *(unsigned short *)(bBuf + next)) != 0xffff)
  225.         {
  226.         next += 2;
  227.         printf("Type = %u\n",type);
  228.         count = next + 2;
  229.         next = *(unsigned short *)(bBuf + next);
  230.         next -= (USHORT)(bBuf) & 0xffff;
  231.         printf("Next @ Buffer + 0x%04x\n",next);
  232.  
  233.         switch (type)
  234.           {
  235.           case 0:
  236.             printf("Dump:\n\t");
  237.             for (fold = 0; count < next; count++, fold++)
  238.               {
  239.               if (fold && !(fold % 16))
  240.                 printf("\n\t");
  241.               printf("%02x ",bBuf[count]);
  242.               }
  243.             break;
  244.           case 1:
  245.             printf("Thread Dump:\n\t");
  246.             for (fold = 0; count < next; count++, fold++)
  247.               {
  248.               if (fold && !(fold % 16))
  249.                 printf("\n\t");
  250.               printf("%02x ",bBuf[count]);
  251.               }
  252.             break;
  253.           case 2:
  254.             kount = *(unsigned short *)(bBuf + (count + 2));
  255.             kount <<= 1;
  256.             kount += (count + 8);
  257.             printf("Process Dump:\n\t");
  258.             for (fold = 0; count < kount; count++, fold++)
  259.               {
  260.               if (fold && !(fold % 16))
  261.                 printf("\n\t");
  262.               printf("%02x ",bBuf[count]);
  263.               }
  264.             printf("\nProcess Name: ");
  265.             for ( ; count < next; count++)
  266.               {
  267.               if (!bBuf[count])
  268.                 break;
  269.               printf("%c",bBuf[count]);
  270.               }
  271.             break;
  272.           case 3:
  273.             kount = count + 6;
  274.             printf("System Semaphore Dump:\n\t");
  275.             for (; count < kount; count++)
  276.               printf("%02x ",bBuf[count]);
  277.             printf("\nSemaphore Name: ");
  278.             for ( ; count < next; count++)
  279.               {
  280.               if (!bBuf[count])
  281.                 break;
  282.               printf("%c",bBuf[count]);
  283.               }
  284.             break;
  285.           case 4:
  286.             kount = count + 6;
  287.             printf("Shared Segment Dump:\n\t");
  288.             for (; count < kount; count++)
  289.               printf("%02x ",bBuf[count]);
  290.             printf("\nShared Segment Name: ");
  291.             for ( ; count < next; count++)
  292.               {
  293.               if (!bBuf[count])
  294.                 break;
  295.               printf("%c",bBuf[count]);
  296.               }
  297.             break;
  298.           default:
  299.             printf("Unknown Dump:\n\t");
  300.             for (fold = 0; count < next; count++, fold++)
  301.               {
  302.               if (fold && !(fold % 16))
  303.                 printf("\n\t");
  304.               printf("%02x ",bBuf[count]);
  305.               }
  306.             break;
  307.           }
  308.         printf("\n\n");
  309.         }
  310.       }
  311.     while (type != 0xffff);
  312.     printf("End of Chain\n\n");
  313.     }
  314.   return(0);
  315.   }
  316. --------------- Stop -----------------
  317.  
  318. > Each entry starts with a 16-bit code (0001 = thread information 
  319. > entry, 0004 = named shared segment, etc.)
  320.  
  321. Type 0 appears to link threads with parents (?!)
  322. Type 1 IS thread information
  323. Type 2 is process information
  324. Type 3 is system semaphore information
  325. Type 4 IS named dhared segment information
  326.  
  327. I believe that in type 2 records (process), the second byte is the process-
  328. type (see typeProcess in the LINFOSEG structure).  The third and fourth
  329. bytes seem to be a word which indicates how many extra words to add before
  330. the name (if this is 0, then there are 2 words AFTER that word before the
  331. name, if 1 then there are 3 words, etc.).  I suspect that these extra words
  332. *might* somehow indicate child processes(?)...
  333.  
  334. It is my hope that someone else might be able to figure out more on this 
  335. elusive information.  
  336.  
  337.  
  338.  
  339. ---------------------
  340. Date:  10-21-89
  341. From:  Chris Laforet
  342. To:    All
  343. Subj:  Re: More About The Function Behind Ps.exe And Pstat.exe
  344.  
  345. Here is more information that I have figured out on the return from this 
  346. function:
  347.  
  348. Here are the following interpretations of the 16-bit codes:
  349.     
  350.     0      Links PID's with their parents
  351.     1      Thread Information
  352.     2      Process Information
  353.         3      System Semaphore information
  354.     4      Named Shared Segment Information
  355.       0xffff   End of Chain
  356.  
  357.  
  358. For Type 0:
  359. ----------
  360.  
  361. word[0] - Type (0)
  362. word[1] - Offset to next type
  363. word[2] - PID
  364. word[3] - Parent PID
  365. word[4] - Unknown
  366. word[5] - Module ID number (for want of a better term)
  367. ...
  368.  
  369.  
  370. For Type 1:
  371. ----------
  372.  
  373. word[0] - Type (1)
  374. word[1] - Offset to next type
  375. word[2] - Handle number - probably used by scheduler (?)
  376. word[3] - PID of thread
  377. word[4] - Thread ID
  378. ...
  379.  
  380.  
  381. For Type 2:
  382. ----------
  383.  
  384. word[0] - Type (2)
  385. word[1] - Offset to next type
  386. word[2] - Module ID number (links this to type 0 records and dependents)
  387.           (If NO link to a PID record, then this is a resource (DLL, font))
  388. word[3] - Number of dependencies
  389. word[4] - Offset to list of dependencies
  390. word[5] - Offset to process name
  391. word[6 to (6 + word[3]))] 
  392.         - Dependent Module ID numbers
  393.         - Process name (nul-terminated string);
  394.  
  395.  
  396. For Type 3:
  397. ----------
  398.  
  399. word[0] - Type (3)
  400. word[1] - Offset to next type
  401. word[2] - Unknown
  402. word[3] - Unknown
  403. word[4] - Unknown
  404.         - System semaphore name (nul-terminated string)
  405.  
  406.  
  407.  
  408. For Type 4:
  409. ----------
  410.  
  411. word[0] - Type (4)
  412. word[1] - Offset to next type
  413. word[2] - Unknown
  414. word[3] - Unknown
  415. word[4] - Unknown
  416.         - Named Shared Segment name (nul-terminated string)
  417.  
  418.  
  419. I have created a simple little program which uses this information and which
  420. lists all processes running on your machine with their PID's and Parent PIDs
  421. as well as the dependencies.  It is called RUNNING.EXE and is available at
  422. The Programmer's Oasis and I will try to get it over to Fernwood tonight.
  423.  
  424. If anyone comes up with any more information on some of the unknowns above,
  425. please keep this thread going!
  426.  
  427.  
  428. ----- Structures from QPS.C (Available through Programmer's Oasis) ------
  429.  
  430. typedef union _ps_info PS_info;
  431.  
  432. struct _reference_info
  433.   { /* type 0 */
  434.     unsigned       TYPE;
  435.     PS_info        *NEXT;
  436.     unsigned       PID;
  437.     unsigned       PARENT_PID;
  438.     unsigned       SGID;
  439.     unsigned       MODULE_HANDLE;
  440.     unsigned       *MODULE_REFS;     /* points to modules handle numbers */
  441.     unsigned       MODULE_REF_COUNT;
  442.     unsigned       *SHARED_MEM_REFS; /* points to shared mem handle numbers */
  443.     unsigned       SHARED_MEM_COUNT;
  444.     unsigned       *SEMAPHORE_REFS;  /* points to semaphore handle numbers */
  445.     unsigned       SEMAPHORE_COUNT;
  446.   };
  447.  
  448. struct _thread_info
  449.   { /* type 1 */
  450.     unsigned       TYPE;
  451.     PS_info        *NEXT;
  452.     unsigned       PD_INDEX;
  453.     unsigned       PID;
  454.     unsigned       TID;
  455.     unsigned       PRIORITY;
  456.     unsigned       STATE;
  457.     unsigned long  BLOCK_ID;
  458.   };
  459.  
  460. struct _module_info
  461.   { /* type 2 */
  462.     unsigned       TYPE;
  463.     PS_info        *NEXT;
  464.     unsigned       HANDLE;
  465.     unsigned       NUM_DEPENDENCIES;      /* number of imported modules */
  466.     unsigned       DEPENDENCIES_PTR;
  467.     char           *MODULE_NAME_PTR;
  468.     unsigned       DEPENDENT_HANDLE_LIST; /* imported handle list */
  469.   };
  470.  
  471. struct _semaphore_info
  472.   { /* type 3 */
  473.     unsigned       TYPE;
  474.     PS_info        *NEXT;
  475.     unsigned       FLAGS;
  476.     unsigned       REF;
  477.     unsigned       HANDLE;
  478.     char           SEM_NAME;
  479.   };
  480.  
  481. struct _shared_mem_info
  482.   { /* type 4 */
  483.     unsigned       TYPE;
  484.     PS_info        *NEXT;
  485.     unsigned       HANDLE;
  486.     unsigned       SELECTOR;
  487.     unsigned       REF;
  488.     char           SEG_NAME;
  489.   };
  490.  
  491. typedef union _ps_info
  492.   {
  493.     struct _reference_info   t0;
  494.     struct _thread_info      t1;
  495.     struct _module_info      t2;
  496.     struct _semaphore_info   t3;
  497.     struct _shared_mem_info  t4;
  498.   } PS_info;
  499.  
  500. ----------------------------
  501.