线程同步-文件锁

xiaoxiao2021-02-28  100

文件锁

         借助 fcntl函数来实现锁机制。  操作文件的进程没有获得锁时,可以打开,但无法执行read、write操作。

fcntl函数:     获取、设置文件访问控制属性。

         int fcntl(int fd, int cmd, ... /* arg */ );

         参2:

                   F_SETLK (struct flock *)   设置文件锁(trylock)

                   F_SETLKW (struct flock *) 设置文件锁(lock)W -->wait

                   F_GETLK (struct flock *)  获取文件锁

         参3:

       struct flock {

              ...

              short l_type;             锁的类型:F_RDLCK 、F_WRLCK 、F_UNLCK

              short l_whence;         偏移位置:SEEK_SET、SEEK_CUR、SEEK_END

              off_t l_start;              起始偏移:1000

              off_t l_len;              长度:0表示整个文件加锁

              pid_t l_pid;              持有该锁的进程ID:(F_GETLKonly)

              ...

        };

进程间文件锁示例

#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> void sys_err(char *str) {     perror(str);     exit(1); } int main(int argc, char *argv[]) {     int fd;     struct flock f_lock;     if (argc < 2) {         printf("./a.out filename\n");         exit(1);     }     if ((fd = open(argv[1], O_RDWR)) < 0)         sys_err("open");     f_lock.l_type = F_WRLCK;        /*选用写琐*/ //    f_lock.l_type = F_RDLCK;      /*选用读琐*/      f_lock.l_whence = SEEK_SET;     f_lock.l_start = 0;     f_lock.l_len = 0;               /* 0表示整个文件加锁 */     fcntl(fd, F_SETLKW, &f_lock);     printf("get flock\n");     sleep(10);     f_lock.l_type = F_UNLCK;     fcntl(fd, F_SETLKW, &f_lock);     printf("un flock\n");     close(fd);     return 0; }
转载请注明原文地址: https://www.6miu.com/read-65564.html

最新回复(0)