Should two exclusive flock(2)'s by same process succeed?

Brain in Neutral bin at primate.wisc.edu
Tue Jul 25 03:03:50 AEST 1989


Should a single process be able to acquire an exclusive lock on a file
twice?  It would seem to me "no", given the usual meaning of "exclusive".
However, differing systems give me differing results for the following
program.

# include	<sys/file.h>
# include	<errno.h>

main ()
{
extern int	errno;
int		f1, f2;

	f1 = open ("junk", O_WRONLY|O_CREAT, 0644);
	if (f1 < 0)
	{
		perror ("f1 open");
		exit (1);
	}
	printf ("f1 opened\n");
	if (flock (f1, LOCK_EX|LOCK_NB) < 0)
	{
		if (errno == EWOULDBLOCK)
		{
			perror ("f1 flock1");
			exit (1);
		}
		perror ("f1 flock2");
		exit(1);
	}
	printf ("f1 locked\n");
	f2 = open ("junk", O_WRONLY|O_CREAT, 0644);
	if (f2 < 0)
	{
		perror ("f2 open");
		exit (1);
	}
	printf ("f2 opened\n");
	if (flock (f2, LOCK_EX|LOCK_NB) < 0)
	{
		if (errno == EWOULDBLOCK)
		{
			perror ("f2 flock1");
			exit (1);
		}
		perror ("f2 flock2");
		exit(1);
	}
	printf ("f2 locked\n");
}

On a VAX (Ultrix 2.2 or 1.2) I get the following output, as expected:

	f1 opened
	f1 locked
	f2 opened
	f2 flock1: Operation would block

On a MIPS M/120 (RISC/os 4.0) I get, unexpectedly:

	f1 opened
	f1 locked
	f2 opened
	f2 locked

In other words, the process acquires two exclusive locks.  Am I correct
in thinking this is a bug in flock?

Paul DuBois
dubois at primate.wisc.edu



More information about the Comp.unix.questions mailing list