home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / tasklist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  4.2 KB  |  127 lines

  1. ;/* tasklist.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 tasklist.c
  3. Blink FROM LIB:c.o,tasklist.o TO tasklist LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. tasklist.c - Snapshots and prints the ExecBase task list
  7. */
  8. #include <exec/types.h>
  9. #include <exec/lists.h>
  10. #include <exec/nodes.h>
  11. #include <exec/memory.h>
  12. #include <exec/execbase.h>
  13.  
  14. #include <clib/alib_protos.h>
  15. #include <clib/exec_protos.h>
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #ifdef LATTICE
  21. int CXBRK(void) { return(0); }   /* disable SAS/C CTRL-C handing */
  22. int chkabort(void) {return(0); }
  23. #endif
  24.  
  25. static UBYTE *VersTag = "$VER: tasklist 37.2 (31.3.92)";
  26. extern struct ExecBase *SysBase;
  27.  
  28. /* Use extended structure to hold task information */
  29. struct TaskNode {
  30.     struct Node tn_Node;
  31.     ULONG tn_TaskAddress;
  32.     ULONG tn_SigAlloc;
  33.     ULONG tn_SigWait;
  34.     UBYTE tn_Name[32];
  35. };
  36.  
  37.  
  38.  
  39.  
  40. void main(int argc, char **argv)
  41. {
  42.     struct List *ourtasklist;
  43.     struct List *exectasklist;
  44.     struct Task *task;
  45.     struct TaskNode *node, *tnode, *rnode = NULL;
  46.     struct Node *execnode;
  47.  
  48.     /* Allocate memory for our list */
  49.     if (ourtasklist = AllocMem(sizeof(struct List), MEMF_CLEAR)) {
  50.         /* Initialize list structure (ala NewList()) */
  51.         ourtasklist->lh_Head = (struct Node *)&ourtasklist->lh_Tail;
  52.         ourtasklist->lh_Tail = 0;
  53.         ourtasklist->lh_TailPred = (struct Node *)&ourtasklist->lh_Head;
  54.  
  55.         /* Make sure tasks won't switch lists or go away */
  56.         Disable();
  57.  
  58.         /* Snapshot task WAIT list */
  59.         exectasklist = &(SysBase->TaskWait);
  60.         for (execnode = exectasklist->lh_Head;
  61.                  execnode->ln_Succ; execnode = execnode->ln_Succ)
  62.         {
  63.             if (tnode = AllocMem(sizeof(struct TaskNode), MEMF_CLEAR))
  64.             {
  65.                 /* Save task information we want to print */
  66.                 strncpy(tnode->tn_Name, execnode->ln_Name, 32);
  67.                 tnode->tn_Node.ln_Pri = execnode->ln_Pri;
  68.                 tnode->tn_TaskAddress = (ULONG)execnode;
  69.                 tnode->tn_SigAlloc = ((struct Task *)execnode)->tc_SigAlloc;
  70.                 tnode->tn_SigWait = ((struct Task*)execnode)->tc_SigWait;
  71.                 AddTail(ourtasklist, (struct Node *)tnode);
  72.             }
  73.             else break;
  74.         }
  75.  
  76.         /* Snapshot task READY list */
  77.         exectasklist = &(SysBase->TaskReady);
  78.         for (execnode = exectasklist->lh_Head;
  79.                  execnode->ln_Succ; execnode = execnode->ln_Succ)
  80.         {
  81.             if (tnode = AllocMem(sizeof(struct TaskNode), MEMF_CLEAR))
  82.             {
  83.                 /* Save task information we want to print */
  84.                 strncpy(tnode->tn_Name, execnode->ln_Name, 32);
  85.                 tnode->tn_Node.ln_Pri = execnode->ln_Pri;
  86.                 tnode->tn_TaskAddress = (ULONG)execnode;
  87.                 tnode->tn_SigAlloc = ((struct Task *)execnode)->tc_SigAlloc;
  88.                 tnode->tn_SigWait = ((struct Task*)execnode)->tc_SigWait;
  89.                 AddTail(ourtasklist, (struct Node *)tnode);
  90.                 if(!rnode)  rnode = tnode;  /* first READY task */
  91.             }
  92.             else
  93.                 break;
  94.         }
  95.  
  96.         /* Re-enable interrupts and taskswitching */
  97.         Enable();
  98.  
  99.         /* Print now (printing above would have defeated a Forbid or Disable) */
  100.         printf("Pri Address     SigAlloc    SigWait    Taskname\n");
  101.  
  102.         node = (struct TaskNode *)(ourtasklist->lh_Head);
  103.         printf("\nWAITING:\n");
  104.         while (tnode = (struct TaskNode *)node->tn_Node.ln_Succ)
  105.         {
  106.             if(tnode == rnode)
  107.                 printf("\nREADY:\n");  /* we set rnode above */
  108.             printf("%02d  0x%08lx  0x%08lx  0x%08lx %s\n",
  109.                     node->tn_Node.ln_Pri, node->tn_TaskAddress, node->tn_SigAlloc,
  110.                     node->tn_SigWait, node->tn_Name);
  111.  
  112.             /* Free the memory, no need to remove the node, referenced once only */
  113.             FreeMem(node,sizeof(struct TaskNode));
  114.             node = tnode;
  115.         }
  116.         FreeMem(ourtasklist, sizeof(struct List));
  117.  
  118.         /* Say who we are */
  119.         printf("\nTHIS TASK:\n");
  120.         task = FindTask(NULL);
  121.         printf("%02d  0x%08lx  0x%08lx  0x%08lx %s\n",
  122.                 task->tc_Node.ln_Pri, task, task->tc_SigAlloc,
  123.                 task->tc_SigWait, task->tc_Node.ln_Name);
  124.  
  125.     }
  126. }
  127.