meter.c 594 B

1234567891011121314151617181920212223242526272829
  1. #include "unp.h"
  2. #include <sys/mman.h>
  3. /*
  4. * Allocate an array of "nchildren" longs in shared memory that can
  5. * be used as a counter by each child of how many clients it services.
  6. * See pp. 467-470 of "Advanced Programming in the Unix Environment."
  7. */
  8. long *
  9. meter(int nchildren)
  10. {
  11. int fd;
  12. long *ptr;
  13. #ifdef MAP_ANON
  14. ptr = Mmap(0, nchildren*sizeof(long), PROT_READ | PROT_WRITE,
  15. MAP_ANON | MAP_SHARED, -1, 0);
  16. #else
  17. fd = Open("/dev/zero", O_RDWR, 0);
  18. ptr = Mmap(0, nchildren*sizeof(long), PROT_READ | PROT_WRITE,
  19. MAP_SHARED, fd, 0);
  20. Close(fd);
  21. #endif
  22. return(ptr);
  23. }