[BraLUG] File auf NFS Freigabe sperren

Markus Dahms dahms at fh-brandenburg.de
Mo Okt 16 16:33:49 CEST 2006


Hallo Tobias,

> Na das ist ja spannend. Eigentlich sollte flock() bei NFS nicht
> gehen, wohl aber lockf() und fcntl() -- bei mir ist das auch so.

| struct flock lock = { .l_type = F_WRLCK };
| fcntl(fd, F_SETLK, &lock);

gleicher Effekt wie mit flock. Ich habe mal mein Testprogramm mal
angehangen, vielleicht hat's ja 'nen offensichtlichen Fehler...

Markus

-- 
Memory fault -- brain fried
-------------- nächster Teil --------------
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>

static int fd = -1;

void sighandler(int sig)
{
	close(fd);
	exit(1);
}

int main(int argc, char *argv[])
{
	struct flock lock = { .l_type = F_WRLCK };

	signal(SIGINT, sighandler);
	signal(SIGQUIT, sighandler);

	fd = open("test.lock", O_EXCL | O_WRONLY);
	/* flock(fd, LOCK_EX | LOCK_NB); */
	fcntl(fd, F_SETLK, &lock);
	if(errno == 0)
	{
		printf("successfully locked file\n");
		while(1)
		{
			sleep(1);
		}
	}
	else
	{
		printf("failed to aquire lock for %d: %s (%d)\n",
			fd, strerror(errno), errno);
	}

	return EXIT_SUCCESS;
}


Mehr Informationen über die Mailingliste Bralug