#include #include #include #include #include #include #include #include #include #include /* не работает с nfs */ void ex1() { 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(); } /* только для 1-го процесса */ void ex2() { FILE *f = fopen("fcntl_lockfile.c", "wb"); if(f != NULL && !ftrylockfile(f)) { flockfile(f); puts("Success!"); } else puts("Failed!"); getchar(); } /* с 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()"); if (fcntl(fd, F_SETLEASE, F_WRLCK) == -1) err(errno, "fcntl()"); 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; }