Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!uwm.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!sgi!shinobu!odin!camelot!jesse From: jesse@camelot.sgi.com (Jesse Rendleman) Newsgroups: comp.sys.sgi Subject: Re: Silly error with flock Message-ID: <1991Jan11.003748.3049@odin.corp.sgi.com> Date: 11 Jan 91 00:37:48 GMT References: <1991Jan9.030948.18665@ccu1.aukuni.ac.nz> <1991Jan10.214011.86@ux1.cso.uiuc.edu> Sender: news@odin.corp.sgi.com (Net News) Distribution: comp Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 30 In article <1991Jan9.030948.18665@ccu1.aukuni.ac.nz>, russell@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 > #include > #include > > 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.