flock weirdness

Harald Evensen (haralde@pvv.ntnu.no)
Mon, 21 Oct 1996 14:56:01 +0200


I got some strange results when toying with flock() the other day:
(included is a small program)

#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

main() {
int fd, err;

fd = open("test", O_RDONLY);
if(fd > 0) {
do {
errno = 0;
err = flock(fd, LOCK_EX|LOCK_NB);
if(err == -1)
fprintf(stderr, "errno=%d(%s)\n", errno, strerror(errno));
sleep(2);
} while(errno != 0);
}
}

I first lock the file (test) with 1 process. Then run this program.
errno=2(Try again)
errno=11(Try again)
errno=11(Try again)
errno=11(Try again)

Why is errno 2 (ENOENT) the first time ? even tho strerror gives the correct
results. According to the man pages the only error it can return is
EWOULDBLOCK (EAGAIN).

Just wondering.

regards, Harald