home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / solaris / 216 < prev    next >
Encoding:
Internet Message Format  |  1992-09-15  |  3.6 KB

  1. Xref: sparky comp.unix.solaris:216 comp.sys.sun.misc:4241 comp.unix.programmer:4673
  2. Path: sparky!uunet!auspex-gw!guy
  3. From: guy@Auspex.COM (Guy Harris)
  4. Newsgroups: comp.unix.solaris,comp.sys.sun.misc,comp.unix.programmer
  5. Subject: Re: swapctl() problems
  6. Keywords: swap swapctl
  7. Message-ID: <14658@auspex-gw.auspex.com>
  8. Date: 16 Sep 92 00:29:06 GMT
  9. References: <1992Sep15.215809.20252@icd.teradyne.com>
  10. Sender: news@auspex-gw.auspex.com
  11. Followup-To: comp.unix.solaris
  12. Organization: Auspex Systems, Santa Clara
  13. Lines: 79
  14. Nntp-Posting-Host: auspex.auspex.com
  15.  
  16. >     The specific problem is that function seems to want you to
  17. > define a struct which has as one of its members an array of structs. 
  18. > However, the struct declaration in /usr/include/swap.h declares that
  19. > array to be of length 1.  This causes no end of problems which I can't
  20. > seem to get around.
  21.  
  22. A fairly standard C-language trick; take a look at the SV message queue
  23. stuff, for example.
  24.  
  25. Standard C doesn't support the notion of variable-length arrays, so you
  26. can't declare a structure that contains such an array, which is what the
  27. structure filled in by the SC_LIST call *really* is.  So, instead, such
  28. a structure is declared with a fixed-length array, and you have to
  29. depend on compile-time array-bounds checking not being done; you also
  30. have to create such a structure yourself, e.g. by dynamically
  31. allocating it.
  32.  
  33. Furthermore, standard C doesn't support zero-element arrays, so a data
  34. structure with a variable-length array as the last element is often
  35. declared as having a one-element array.
  36.  
  37. Assuming that there's one entry in "swt_ent" for every swap resource,
  38. you can use SC_GETNSWP to find out how many entries there are:
  39.  
  40.     int n_swap_entries;
  41.  
  42.     n_swap_entries - swapctl(SC_GETNSWP, NULL);
  43.     if (n_swap_entries == -1)
  44.         complain and quit;
  45.     if (n_swap_entries == 0)
  46.         note that there are, miraculously, no swap entries, and quit;
  47.  
  48. Then allocate a table to hold that many entries:
  49.  
  50.     swaptbl_t *swaptbl;
  51.  
  52.     swaptbl = (swaptbl_t *)malloc(
  53.         sizeof (swaptbl_t) + (n_swap_entries - 1)*sizeof (struct swapent));
  54.  
  55. It's "n_swap_entries - 1" because the first entry is accounted for by
  56. "sizeof (swaptbl_t)".
  57.  
  58. Then put the number of elements into the structure:
  59.  
  60.     swaptbl->swt_n = n_swap_entries;
  61.  
  62. Then fetch the entries:
  63.  
  64.     n_swap_entries = swapctl(SC_LIST, (void *)swaptbl);
  65.     if (n_swap_entries == -1) {
  66.         if (errno == ENOMEM)
  67.             try again, with a bigger array;
  68.         else
  69.             complain and quit;
  70.     }
  71.  
  72. and loop through them.
  73.  
  74. NOTE: "complain" in "complain and quit" doesn't mean "print some typical
  75. dumb UNIX error message saying that the call failed", it means "print
  76. some atypical *useful* error message that prints the error string
  77. associated with 'errno' - using 'perror()' or 'strerror()' - so that if
  78. it fails you have a clue as to why".  (It doesn't mean "print some
  79. typical only-marginally-less-dumb UNIX error message giving the
  80. numerical value of 'errno'", either; computers are at *least* as good as
  81. humans at translating cryptic numeric values such as that into error
  82. messages.)
  83.  
  84. "Try again, with a bigger array" deals with the case where a new entry
  85. was added to the table between the two "swapctl()" calls.
  86.  
  87. If SC_GETNSWP *doesn't* return the number of entries you should expect
  88. to get back from SC_LIST (although, if not, I've no idea why the hell
  89. SC_GETNSWP exists), you'll have to go through some more pain, starting
  90. at some fixed table size and, for example, doubling it each time the
  91. SC_LIST call fails with ENOMEM.  Hopefully, that's not the case....
  92.  
  93. (No, I don't have a SunOS 5.0 system with a C compiler handy on which to
  94. try the above, so you'll have to do it yourself.)
  95.