locking files

locking files

Post by chanabl.. » Thu, 13 Jul 2000 04:00:00



Hello, I was wondering how linux locks files.

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?

Thanks!

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

locking files

Post by Dances With Cro » Thu, 13 Jul 2000 04:00:00




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

 
 
 

1. Locked files - who has it locked?

I am using advisory locks on SUNOS 4.1.1 and I find that sometimes I
have lockf calls failing to acquire locks when I don't believe that
any process has the file opened or locked.  This seems to happen when
the file is on a NFS file system and the process that previously have had
the lock terminated.

My question:  When I find myself in this state, is there anyway to tell
which process owns the lock (if any)?  The crash documents don't discuss
this and I don't have the sources to peek around kernel data structures
to figure out what's going on.

Or, is there well known problems with Sun's lock manager (lockd) that
is the culprit.  After killing off lockd and statd on all machines
concerned, and waiting 20 minutes, the lock mysteriously went away.

If you can help, please respond by email.

Thanks,

Brian Vetter

2. WWW Browsers - text

3. "Can't read lock file /tmp/.X0-lock"

4. Problem installing FreeBSD 3.0

5. modem locked but I can't find the lock file

6. Help!! I can't get linux to boot

7. Apache: Cannot open lock file: /usr/logs/httpd.lock.10064

8. Starting X gives blank screen

9. how to remove a lock from a locked file?

10. Fatal Server Error: Could not create lock file in /tmp/.tX0-lock???

11. mail problem: file permissions for lock file

12. Locking Files with FILE pointers

13. Can't create lock file /etc/mtab~ :File exists ????????