lockFile.c 380 B

1234567891011121314151617181920
  1. #include <stdio.h>
  2. /*if run serially, works fine.
  3. If run in parallel, someone will attempt to delete
  4. a locked file, which will fail */
  5. int main(void)
  6. {
  7. FILE* file;
  8. int i;
  9. const char* fname = "lockedFile.txt";
  10. file = fopen(fname, "w");
  11. for(i = 0; i < 10000; i++)
  12. {
  13. fprintf(file, "%s", "x");
  14. fflush(file);
  15. }
  16. fclose(file);
  17. return remove(fname);
  18. }