From 2ce1fe251ee30ddb7b116fe7d02d9d517931141b Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Thu, 30 Jun 2011 19:12:23 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=BB=D1=81=D1=8F,=20=D0=BA=D0=B0=D0=BA=20=D0=BB=D0=BE=D1=87?= =?UTF-8?q?=D0=B8=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=20=D0=BD=D0=B0=20?= =?UTF-8?q?CIFS.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c/fcntl_ex/fcntl_lockfile.c | 66 +++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/c/fcntl_ex/fcntl_lockfile.c b/c/fcntl_ex/fcntl_lockfile.c index 9417642..9027355 100644 --- a/c/fcntl_ex/fcntl_lockfile.c +++ b/c/fcntl_ex/fcntl_lockfile.c @@ -7,15 +7,12 @@ #include #include #include +#include -void sighandler(int signum) +/* не работает с nfs */ +void ex1() { - printf("signal %d received\n", signum); -} -int main() -{ -/* не работает с nfs FILE *f = fopen("fcntl_lockfile.c", "wb"); if(f != NULL && !flock(fileno(f), LOCK_EX | LOCK_NB))// !ftrylockfile(f)) @@ -25,11 +22,11 @@ int main() puts("Failed!"); getchar(); +} - return 0; -*/ - -/* 1 процесс +/* только для 1-го процесса */ +void ex2() +{ FILE *f = fopen("fcntl_lockfile.c", "wb"); if(f != NULL && !ftrylockfile(f)) { @@ -40,13 +37,21 @@ int main() puts("Failed!"); getchar(); +} - return 0; -*/ - +/* с cifs не работает */ +void sighandler(int signum) +{ + printf("signal %d received\n", signum); +} +void ex3() +{ 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()"); @@ -57,6 +62,41 @@ int main() puts("Success!"); getchar(); +} + +/* зашибись - метод :) + * Консультативная (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(); return 0; }