Help in file locking !

Help in file locking !

Post by Anand Sitar » Sat, 11 Mar 1995 07:04:51



Hi,

Can anyone direct me how fcntl call works in locking a region of a file ?

I tried locking a file with this call,

fcntl(fd,F_SETLK,&lk);  

where lk is a struct of type flock (defined in fcntl.h). The structures
elements were set to ,

lk.l_type = F_WRLCK; lk.l_whence = SEEK_SET; lk.l_start = 0;
  lk.l_len = 0;

After locking the file opened by fd, I had a sleep call so that I can
invoke an another process to open the same file and write some junk
data.

The write from the other process (on the same file) SUCCEEDED !!

- Any help how locking needs to be done !!

-Thanks Bye

 
 
 

Help in file locking !

Post by Steve Emmers » Sun, 12 Mar 1995 01:32:25



Quote:>After locking the file opened by fd, I had a sleep call so that I can
>invoke an another process to open the same file and write some junk
>data.
>The write from the other process (on the same file) SUCCEEDED !!

File locking via fcntl() is *advisory* only: it won't prevent another
process from writing into the file.  That process must also use file
locking in a responsible way to obtain permission.
--



 
 
 

Help in file locking !

Post by King Clarence » Sun, 12 Mar 1995 04:32:43


: Hi,

: Can anyone direct me how fcntl call works in locking a region of a file ?

: After locking the file opened by fd, I had a sleep call so that I can
: invoke an another process to open the same file and write some junk
: data.

: The write from the other process (on the same file) SUCCEEDED !!

The lock that you obtained does not prevent other processes from
-writing- to the file, it prevents other processes from -locking-
the region that you locked.  What you want to do is lock the region
of interest (or the whole file as in your example), write to the file,
and then unlock it.  Do this in -all-processes involved, not just the
first one.

--
Clarence Gardner
King of All He Surveys (now where'd I put my transit?)

 
 
 

1. More mail file locking questions (lockf, NFS, /var/spool/mail/*.lock)

        Here's a problem I've run into:

        The problem I've seen relates to building mh-6.8 for SVR4
        (and correspondingly LOCKF). Now, it appears that while
        INC believes the mail file (NFS mounted) should be
        locked, it isn't (or it is lockf'ed and someone else
        [/bin/mail?] isn't honoring the lock; it says something
        like "New mail has arrived.."). It appears that /bin/mail
        (on 4.1.x and Solaris 2.x) uses lock files
        (/var/spool/mail/*.lock).  

        My question is, should mh ({l}emacs, popper,imap,...)
        use this style (and abandon lockf), or is there some
        other appropriate solution to this problem?

        Thanks,

        Dave    

        David M. Meyer                  Voice:     503/346-1747
        Senior Network Engineer         Pager:     503/342-9458
        Office of University Computing  FAX:       503/346-4397

        University of Oregon
        1225 Kincaid
        Eugene, OR 97403        

2. NIS problem: building group maps fails...

3. Locked files - who has it locked?

4. Xconfig needed for SPEA VEGA V7 1MB

5. Netscape plug-ins, helper files, Acrobat etc.

6. Making sense of a core files output in adb

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

8. SMC 8216 & slackware

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

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

11. Compare file modification date using tcsh built-ins

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

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