sem.c 749 B

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