sem.c 805 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <errno.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/sem.h>
  7. int main(void)
  8. {
  9. int k, r;
  10. union semun {
  11. int val;
  12. struct semid_ds *buf;
  13. unsigned short int *array;
  14. struct seminfo *__buf;
  15. } sd;
  16. struct semid_ds sd_buf;
  17. k = semget(IPC_PRIVATE, 10, IPC_CREAT | 0666 );
  18. printf("semget(IPC_CREAT) = %d\n", k);
  19. if (k < 0) {
  20. fprintf(stderr, "semget failed: %s\n", strerror(errno));
  21. return 1;
  22. }
  23. sd.buf = &sd_buf;
  24. r = semctl(k, 0, IPC_STAT, sd);
  25. printf("semctl(k) = %d\n", r);
  26. if (r < 0) {
  27. perror("semctl IPC_STAT failed");
  28. return 1;
  29. }
  30. printf("sem_nsems = %lu\n", sd_buf.sem_nsems);
  31. if (sd_buf.sem_nsems != 10) {
  32. fprintf(stderr, "failed: incorrect sem_nsems!\n");
  33. return 1;
  34. }
  35. printf("succeeded\n");
  36. return 0;
  37. }