mutex.c 580 B

123456789101112131415161718192021222324252627
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void) {
  4. FILE* f = fopen("stdio/stdio.in", "r");
  5. flockfile(f);
  6. // Commenting this out should cause a deadlock:
  7. // flockfile(f);
  8. if (!ftrylockfile(f)) {
  9. puts("Mutex unlocked but it shouldn't be");
  10. return EXIT_FAILURE;
  11. }
  12. funlockfile(f);
  13. if (ftrylockfile(f)) {
  14. puts("Mutex locked but it shouldn't be");
  15. return EXIT_FAILURE;
  16. }
  17. if (!ftrylockfile(f)) {
  18. puts("Mutex unlocked but it shouldn't be");
  19. return EXIT_FAILURE;
  20. }
  21. funlockfile(f);
  22. }