Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site wgivax.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!mcnc!unccvax!wgivax!mo From: mo@wgivax.UUCP (Michael O'Shea) Newsgroups: net.unix-wizards Subject: Re: flock(2) on 4.2bsd Message-ID: <156@wgivax.UUCP> Date: Thu, 27-Mar-86 09:16:39 EST Article-I.D.: wgivax.156 Posted: Thu Mar 27 09:16:39 1986 Date-Received: Mon, 31-Mar-86 23:58:47 EST References: <2721@sdcrdcf.UUCP> Lines: 64 >I am using the LOCK_EX (exclusive lock) operation on a file. >The manual states that if a lock already exists, the call should >block until lock is available. But in testing, >if the lock exits, flock returns with an error, EWOULDBLOCK. > >It behaves as if I specified (LOCK_EX|LOCK_NB) for the operation, >the LOCK_NB being no blocking, but I have NOT! > >Am I doing something wrong? You don't give an extract from the code which is doing this, but assuming you are doing it as the manual states, it should work fine. I use the following type of construct in several pieces of code on a Vax 11/780 running vanilla 4.2, and have never had any problems with them acting as expected (program waits for 5 minutes to get lock; if it can't get lock, prints appropriate message and exits): #include #include jmp_buf lockjump; xyz() { int alarmtrap(); FILE *fopen(); /* open file */ if((file = fopen(NAME,"a+")) == NULL) { perror(NAME); exit(-1); } /* wait to get lock for 5 minutes, then exit */ signal(SIGALRM, alarmtrap); alarm(300); if(setjmp(lockjump) == 0) { if(flock(file->_file,LOCK_EX)) { fprintf(stderr,"ERROR LOCKING %s\n",NAME); exit(-1); } alarm(0); } else { fprintf(stderr,"\n\n\n\n"); fprintf(stderr,"Please try at a later time, "); fprintf(stderr,"someone else is hogging program right now!\n"); fprintf(stderr,"\n\n\n\n"); exit(1); } } alarmtrap() { alarm(0); longjmp(lockjump,1); }