home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / wizards / 5350 < prev    next >
Encoding:
Text File  |  1992-12-31  |  3.1 KB  |  92 lines

  1. Newsgroups: comp.unix.wizards
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!uwm.edu!ux1.cso.uiuc.edu!news.iastate.edu!john
  3. From: john@iastate.edu (John Hascall)
  4. Subject: Re: NLIST Discussion
  5. Message-ID: <C04uFD.Kw1@news.iastate.edu>
  6. Sender: news@news.iastate.edu (USENET News System)
  7. Organization: Iowa State University, Ames, IA
  8. References: <72555@cup.portal.com>
  9. Date: Thu, 31 Dec 1992 16:42:47 GMT
  10. Lines: 80
  11.  
  12. Dave-Nuttall@cup.portal.com (David Dave Nuttall) writes:
  13. }I need to locate reference material which discusses the function
  14. }"nlist".  If you have anysuggestions, please post or e-mail me.
  15.  
  16. The nlist function is really quite simple to use.  Basically all it does is
  17. read the symbols of out a (non-stripped) image.  Your system should have a
  18. "man nlist" and in include file (say, /usr/include/nlist.h) which should
  19. give details.  Here is a fairly simple example of its use:
  20.  
  21. #include <stdio.h>
  22. #include <nlist.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #include <sys/fixpoint.h>
  27.  
  28. struct  nlist Nl[] =            /* an array of the symbols that you */
  29. {                    /*   are interested in the value of */
  30.         { "_avenrun" },            /* name of a symbol (_ optional?) */
  31. #define X_AVENRUN       0        /* index of sym (coding convention) */
  32.         { NULL },            /* NULL to terminate the list */
  33. };
  34.  
  35. /*
  36.  * get load average (most recent) as an integer
  37.  * this version for DECstations which store the load avg as 3 FIXEDPOINTS
  38.  */
  39. int loadavg()
  40. {
  41.         static int      kmfd = -1;    /* file descriptor for /dev/kmem */
  42.         long            avenrun[3];    /* really fixedpoint, look like long */
  43.  
  44.     /*
  45.      * To get the value of avenrun we will need to read it from
  46.      * /dev/kmem (once we find out where it is with the nlist()
  47.      * function).  Since we get called often lets just open it once.
  48.      * We also only need get the Nlist once.
  49.      */
  50.         if (kmfd < 0) {
  51.         /*
  52.          * Must be the first time through, open /dev/kmem
  53.          * and mark it close on exec so our child processes
  54.          * close it when they exec.
  55.          */
  56.                 if ((kmfd = open("/dev/kmem", O_RDONLY, 0)) < 0) return (-1);
  57.                 (void)ioctl(kmfd, FIOCLEX, NULL);
  58.  
  59.         /*
  60.          * get the Nlist out of /vmunix
  61.          * (something more clever might allow the name as an arg)
  62.          */
  63.                 nlist("/vmunix", Nl);
  64.         }
  65.  
  66.     /*
  67.      * If the first entry of the Nlist array has n_type of 0 (zero)
  68.      * then it failed (probably because it was stripped).
  69.      */
  70.         if (Nl[0].n_type == 0) return (-1);
  71.  
  72.     /*
  73.      * all is good, Nl[X_AVEBRUN].n_value tells us where to seek
  74.      * in /dev/kmem to read the current value of the variable
  75.      * We go ahead and read all 3 entries of the array even though
  76.      * we'll only use the zeroth.
  77.      */
  78.         if ((lseek(kmfd, (off_t)Nl[X_AVENRUN].n_value, SEEK_SET) == -1) ||
  79.             (read(kmfd, (char *)avenrun, sizeof(avenrun))) != sizeof(avenrun)) {
  80.                 return (-1);
  81.         }
  82.     /*
  83.      * convert/round it to the form we want (an integer)
  84.      */
  85.         return ((int)(FIX_TO_DBL(avenrun[0]) + 0.5));
  86. }
  87. -- 
  88. John Hascall                   ``An ill-chosen word is the fool's messenger.''
  89. Systmes Software Engineer
  90. Project Vincent
  91. Iowa State University Computation Center  +  Ames, IA  50011  +  515/294-9551
  92.