mutex.c 530 B

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