home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d079 / monproc.lha / MonProc / getprocs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-04  |  3.5 KB  |  154 lines

  1. /*
  2.  *  GETPROCS.C - Grab all available processes and return them to you in a
  3.  *               simple exec list. The list is made up of  nodes--the "name"
  4.  *               fields pointing to the process structure. 
  5.  *
  6.  *             Phillip Lindsay (c) 1987 Commodore-Amiga Inc. 
  7.  *  You may use this source as long as the copyright notice is left intact.
  8.  *
  9.  *  Re-organized and re-worked by Davide P. Cervone, 4/25/87
  10.  */
  11.  
  12. #include <exec/types.h>
  13. #include <exec/memory.h>
  14. #include <exec/tasks.h>
  15. #include <exec/execbase.h>
  16. #include <libraries/dos.h>
  17. #include <libraries/dosextens.h>
  18. #include <stdio.h>
  19.  
  20. #ifdef MANX
  21.    #include <functions.h>
  22. #endif
  23.  
  24. #define NODESIZE        ((ULONG)sizeof(struct Node))
  25. #define MEMFLAGS        (MEMF_PUBLIC | MEMF_CLEAR)
  26.  
  27. extern struct Node *AllocMem(), *RemTail();
  28.  
  29. /*
  30.  *   GetNode()
  31.  *
  32.  *  Allocates a node structure for you and initializes the node
  33.  *  with the given values.
  34.  */     
  35.  
  36. struct Node *GetNode(name,type,pri)
  37. char *name;
  38. UBYTE type,pri;
  39. {
  40.    register struct Node *theNode;
  41.  
  42.    theNode =  AllocMem(NODESIZE,MEMFLAGS); 
  43.    if (theNode != NULL)
  44.    {
  45.       theNode->ln_Name = name;
  46.       theNode->ln_Type = type;
  47.       theNode->ln_Pri  = pri;
  48.    }
  49.    return(theNode);
  50. }
  51.  
  52.  
  53. /*
  54.  *  FreeNode()
  55.  *
  56.  *  Frees a given Exec node.  You must remove it from any list before
  57.  *  calling FreeNode()
  58.  */
  59.  
  60. void FreeNode(theNode)
  61. struct Node *theNode;
  62. {
  63.    FreeMem(theNode,NODESIZE);
  64. }
  65.  
  66.  
  67. /*
  68.  *  AddProcs()
  69.  *
  70.  *  Adds the processes from the ProcList onto the end of the list 'plist'.
  71.  */
  72.  
  73. void AddProcs(plist,ProcList)
  74. struct List *plist;
  75. struct List *ProcList;
  76. {
  77.    register struct Node  *aProc,*bProc;
  78.  
  79.    if (ProcList->lh_TailPred != (struct Node *)ProcList)
  80.    {
  81.       for (aProc=ProcList->lh_Head; aProc->ln_Succ; aProc=aProc->ln_Succ)
  82.       { 
  83.          if (aProc->ln_Type == NT_PROCESS)
  84.          { 
  85.             if (bProc = GetNode(aProc,0,0)) AddTail(plist,bProc);
  86.          }
  87.       }
  88.    }    
  89. }
  90.  
  91.  
  92. /*
  93.  *  GetProcList()
  94.  *
  95.  *  For each process in the system's task list, GetProcList appends an exec
  96.  *  node to the given list and fills in the node name field with a pointer
  97.  *  to the process strcuture.
  98.  *
  99.  *        plist     is an initialized Exec List.
  100.  *
  101.  *  WARNING:  This isn't a casual subroutine.  The returned list is valid as
  102.  *  long as a process is not ended or added after this subroutine is
  103.  *  called.  To be real safe you should probably check to see if the
  104.  *  process is still around before trying to look at the data structure.  
  105.  */
  106.  
  107. void GetProcList(plist)
  108. struct List *plist;
  109. {
  110.    extern   struct ExecBase   *SysBase;
  111.    register struct ExecBase   *execbase=SysBase;
  112.    register struct Node       *aProc;
  113.  
  114. /*
  115.  * I haven't clocked this code, but as a rule, you shouldn't stay disabled for
  116.  * more than 250ms.  I have no doubt I'm illegal, but you can't be too safe
  117.  * when dealing with something that changes with a blink of an interrupt.
  118.  */
  119.  
  120.    Disable();
  121.  
  122.    /*
  123.     *  Add our own process to the list
  124.     */
  125.    if (execbase->ThisTask->tc_Node.ln_Type == NT_PROCESS)
  126.    {
  127.       if (aProc = GetNode(execbase->ThisTask,0,0)) AddTail(plist,aProc);
  128.    }
  129.  
  130.    /*
  131.     *  Add the process from the ExecBase queues
  132.     */
  133.    AddProcs(plist,&execbase->TaskReady);
  134.    AddProcs(plist,&execbase->TaskWait);
  135.  
  136.    Enable();
  137. }
  138.  
  139.  
  140. /*
  141.  *  FreeProcList()
  142.  *
  143.  *  Frees all nodes in the given list.   FreeProcList() assumes the nodes
  144.  *  where initialized with GetNode().
  145.  */
  146.  
  147. void FreeProcList(plist)
  148. struct List *plist;
  149. {
  150.    register struct Node *theProc;
  151.  
  152.    while (theProc = RemTail(plist)) FreeNode(theProc);
  153. }
  154.