Quote:>I was using vi to edit a file and I mistakenly opened the same file on
>top of itself. I got the message that the file was locked, realized my
>mistake and exited vi. When I tried to vi the file again, I got the
>message that the file was locked. I ended up recreating the file and
>killing the vi job but this set me wondering how this works.
>Where does linux keep track of the files it locked? Is there a way to
>lock / unlock file manually?
This is generally handled in an application-specific way. vim, anyway,
creates a file called "~/.foo.swp" if you open a file called "foo". When
you open another file, vim checks ~/.*.swp to see if that file is being
edited by anyone else, and warns you if it is.
Many programs do something similar. If instance 0 of a program opens a
file, it'll create a temporary file saying "(PID of instance 0) is using
this file." That way, instance 1..N can check to see if that file is
being used. There doesn't seem to be any standard for the location of
these temp files, although there is a /var/lock directory on all systems
I've seen.
flock(), fcntl(), and semaphores can also be used to implement file
locking--this would take too long to explain in a Usenet post, but the
basic idea is the same except that you can get the kernel to lock a
filehandle so that only a specific process can write to it. Any good book
on Unix C programming should give insight into how to mess with this.
--
Matt G / Dances With Crows /\ "Man could not stare too long at the face
\----[this space for rent]-----/ \ of the Computer or her children and still
\There is no Darkness in Eternity \ remain as Man." --David Zindell "So did
But only Light too dim for us to see\ they become Gods, or Usenetters?" --/me