2
0

mutex.c 604 B

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