[net.unix-wizards] flock

davem@sdcrdcf.UUCP (David Melman) (03/25/86)

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?

David  Melman
uucp: sdcrdcf!davem
Subject: mail

I removed the 'm' flag from our uucp mailer in our sendmail.cf file.
Now multiple addresses reach their destinations on burdvax.

David

mo@wgivax.UUCP (Michael O'Shea) (03/27/86)

>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 <setjmp.h>
#include <signal.h>

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);
}