2011-06-30 18:43:16 +04:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <errno.h>
|
2011-06-30 19:12:23 +04:00
|
|
|
|
#include <string.h>
|
2011-06-30 18:43:16 +04:00
|
|
|
|
|
2011-06-30 19:12:23 +04:00
|
|
|
|
/* не работает с nfs */
|
|
|
|
|
void ex1()
|
2011-06-30 18:43:16 +04:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
FILE *f = fopen("fcntl_lockfile.c", "wb");
|
|
|
|
|
|
|
|
|
|
if(f != NULL && !flock(fileno(f), LOCK_EX | LOCK_NB))// !ftrylockfile(f))
|
|
|
|
|
puts("Success!");
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
puts("Failed!");
|
|
|
|
|
|
|
|
|
|
getchar();
|
2011-06-30 19:12:23 +04:00
|
|
|
|
}
|
2011-06-30 18:43:16 +04:00
|
|
|
|
|
2011-06-30 19:12:23 +04:00
|
|
|
|
/* только для 1-го процесса */
|
|
|
|
|
void ex2()
|
|
|
|
|
{
|
2011-06-30 18:43:16 +04:00
|
|
|
|
FILE *f = fopen("fcntl_lockfile.c", "wb");
|
|
|
|
|
|
|
|
|
|
if(f != NULL && !ftrylockfile(f)) {
|
|
|
|
|
flockfile(f);
|
|
|
|
|
puts("Success!");
|
|
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
puts("Failed!");
|
|
|
|
|
|
|
|
|
|
getchar();
|
2011-06-30 19:12:23 +04:00
|
|
|
|
}
|
2011-06-30 18:43:16 +04:00
|
|
|
|
|
2011-06-30 19:12:23 +04:00
|
|
|
|
/* с cifs не работает */
|
|
|
|
|
void sighandler(int signum)
|
|
|
|
|
{
|
|
|
|
|
printf("signal %d received\n", signum);
|
|
|
|
|
}
|
|
|
|
|
void ex3()
|
|
|
|
|
{
|
2011-06-30 18:43:16 +04:00
|
|
|
|
signal(SIGPOLL, sighandler);
|
|
|
|
|
|
|
|
|
|
int fd = open("fcntl_lockfile.c", O_RDWR | O_NONBLOCK);
|
2011-06-30 19:12:23 +04:00
|
|
|
|
//~ int fd = open("/mnt/smb/fs04/Users/ПРОЕКТЫ/Отчетник Климов/lock_ex", O_RDWR | O_NONBLOCK);
|
|
|
|
|
//~ int fd = open("/mnt/smb/kolan/shared/lock_ex", O_RDWR | O_NONBLOCK);
|
|
|
|
|
|
2011-06-30 18:43:16 +04:00
|
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
err(errno, "open()");
|
|
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETLEASE, F_WRLCK) == -1)
|
|
|
|
|
err(errno, "fcntl()");
|
|
|
|
|
|
|
|
|
|
puts("Success!");
|
|
|
|
|
|
|
|
|
|
getchar();
|
2011-06-30 19:12:23 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* зашибись - метод :)
|
|
|
|
|
* Консультативная (advisory) блокировка... */
|
|
|
|
|
|
|
|
|
|
void ex4()
|
|
|
|
|
{
|
|
|
|
|
signal(SIGPOLL, sighandler);
|
|
|
|
|
|
|
|
|
|
//~ int fd = open("fcntl_lockfile.c", O_RDWR | O_NONBLOCK);
|
|
|
|
|
//~ int fd = open("/mnt/smb/fs04/Users/ПРОЕКТЫ/Отчетник Климов/lock_ex", O_RDWR | O_NONBLOCK);
|
|
|
|
|
int fd = open("/mnt/smb/kolan/shared/lock_ex", O_RDWR | O_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
err(errno, "open()");
|
|
|
|
|
|
|
|
|
|
struct flock flck;
|
|
|
|
|
memset(&flck, 0, sizeof(flck));
|
|
|
|
|
flck.l_type = F_WRLCK;
|
|
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETLK, &flck) == -1)
|
|
|
|
|
err(errno, "fcntl()");
|
|
|
|
|
|
|
|
|
|
puts("Success!");
|
|
|
|
|
|
|
|
|
|
getchar();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
|
{
|
|
|
|
|
//~ ex1();
|
|
|
|
|
//~ ex2();
|
|
|
|
|
//~ ex3();
|
|
|
|
|
ex4();
|
2011-06-30 18:43:16 +04:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|