how can u lock files in c
If all parties can play by the same rules there exists an extremely easy file
locking scheme: suppose a file named "foo" is a shared resource.
1) Try to create a file named "foo.lck"; if this fails, the file "foo.lck" already exists
and is supposed to contain the PID of the process that created it.
2) if "foo.lck" didn't exist before, it exists now and write the process' PID to the
file and close it.
3) do whatever must be done with "foo" and remove "foo.lck" afterwards.
If step 1) failed (i.e. "foo.lck" existed already), check if the process that created it
still exists (its PID is in the .lck file). If the process isn't there, it must've died
prematurely; change the PID in the .lck file and continue with step 3). If the
process still exists it is working on file "foo" so the file is locked; all your process
can do is wait.
kind regards,
Jos