Silly error with flock

Jesse Rendleman jesse at camelot.sgi.com
Fri Jan 11 11:37:48 AEST 1991


In article <1991Jan9.030948.18665 at ccu1.aukuni.ac.nz>, russell at ccu1.aukuni.ac.nz (Russell J Fulton;ccc032u) writes:
> I am trying to use flock but I always get an "Bad file number" error.
> Here is my test program:
> 
> #include <sys/types.h>
> #include <sys/file.h>
> #include <stdio.h>
> 
> main() {
>     int lock;
> 
>     lock = open("test.lock",O_RDONLY); /* test.lock does exist */
>     if ( lock <= 0) perror("open");
> 
>     if(flock(lock,LOCK_EX) == -1) perror("flock");
> 
>     getchar();
>     flock(lock,LOCK_UN);
>     close (lock);
> }
> 
> Always prints "flock: Bad file number"!
> 
> Any ideas?? It must be something so obvious that I have totally missed it!

from the flock(3b) man page...

     Unlike BSD flock(2), attempts to acquire an exclusive lock on an fd
     opened for reading but not writing will fail with EBADF, as will attempts
     to acquire a shared lock on a write-only fd.



More information about the Comp.sys.sgi mailing list