1. 注意fcntl()参数cmd 的正确使用
F_GETFL 用于测试锁使用
F_SETFL 无阻塞设置锁 fcntl()会尝试几次后,如果失败直接返回-1
F_SETLKW 阻塞设置锁 fcntl()会尝试后,如果失败会被系统挂起来,直到收到解锁的信号再去执行
2. 测试锁的时候 struct flock lock结构体成员 中的l_stype 需要设置为F_WRLCK 其它F_UNLCK,F_RDLCK会有问题
3.struct flock lock 成员使用
测试或锁 整个文件怎样设置
l_whence = SEEK_SET
l_start = 0;
l_len = 0; 如果l_len为0 表示 从start 开始一直到文件最后EOF
struct flock {
...
short l_type; /* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock
(F_GETLK only) */
...
};
5.程序自己测试自己的锁, 测试后肯定是无锁状态
6.程序如果设置的两个区间 连续而且锁的性质一样,系统会自动地将两个锁合并成一个锁
7.更多可以参考以下牛人
http://zhuyunxiang.blog.51cto.com/653596/132548
执行结果:
在另一个终端执行
main.c
- #include"setlock.h"
- #include<stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- int main(void)
- {
- int fd = open("./src", O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IXOTH);
- check_lock(fd,0,10);
- check_lock(fd,11,20);
- set_read_lock(fd,0,10);
- set_write_lock(fd,11,20);
- sleep(15);
- unlock(fd,0,10);
- unlock(fd,11,20);
- return 0;
- }
setlock.h
- #ifndef SETLOCK_H
- #define SETLOCK_H
- void check_lock(int fd,int start,int len);
- void unlock(int fd,int start,int len);
- void set_read_lock(int fd,int start,int len);
- void set_write_lock(int fd,int start,int len);
- #endif // end SETLOCK_H
setlock.c
- #include <unistd.h>
- #include <fcntl.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include <sys/types.h>
- #include<stdbool.h>
- void check_lock(int fd,int start,int len)
- {
- struct flock lock;
- lock.l_type = F_WRLCK;
- lock.l_start = start;
- lock.l_whence = SEEK_SET;
- lock.l_len = start;
- printf("check_lock------\n");
- if((fcntl(fd, F_GETLK, &lock)) == -1)
- {
- printf("1-check_lock: fcntl error\n");
- exit(1);
- }
- switch(lock.l_type)
- {
- case F_RDLCK:
- {
- printf("[%d]:FRDLCK From %d To %d\n", lock.l_pid, start, len);
- break;
- }
- case F_WRLCK:
- {
- printf("[%d]:F_WRLCK From %d To %d\n", lock.l_pid, start, len);
- break;
- }
- case F_UNLCK:
- {
- printf("F_UNLCK\n");
- break;
- }
- default:
- {
- printf("2-check_lock: fcntl error");
- break;
- }
- }
- }
- void set_read_lock(int fd,int start,int len)
- {
- printf("set_read_lock------\n");
- struct flock lock;
- lock.l_type = F_RDLCK;
- lock.l_start = 0;
- lock.l_whence = SEEK_SET;
- lock.l_len = 0;
- if((fcntl(fd, F_SETLKW, &lock)) == -1)
- {
- printf("set_lock: fcntl error\n");
- exit(1);
- }
- else
- {
- printf("[%d] set readlock From %d To %d\n", getpid(), start, len);
- }
- }
- void set_write_lock(int fd,int start,int len)
- {
- printf("set_write_lock------\n");
- struct flock lock;
- lock.l_type = F_WRLCK;
- lock.l_start = start;
- lock.l_whence = SEEK_SET;
- lock.l_len = len;
- if((fcntl(fd, F_SETLKW, &lock)) == -1)
- {
- printf("set_lock: fcntl error\n");
- exit(1);
- }
- else
- {
- printf("[%d] set writelock From %d To %d\n", getpid(), start, len);
- }
- }
- bool unlock(int fd,int start,int len)
- {
- printf("unlock------\n");
- struct flock lock;
- lock.l_type = F_UNLCK;
- lock.l_start = start;
- lock.l_whence = SEEK_SET;
- lock.l_len = len;
- if((fcntl(fd, F_SETLK, &lock)) == -1)
- {
- printf("1-unlock: fcntl error\n");
- exit(1);
- }
- printf("unlock [%d~%d]\n",start,len);
- }
makefile
- srcs=$(wildcard *.c)
- objs =$(patsubst %c,%o,$(srcs))
- CC = gcc
- Target = main
- ####################
- .PHONY: all clean # command: make all or make clean
- clean:
- rm -f $(obj) main *~ *gch
- ###################
- all: $(Target)
- $(Target):$(objs)
- $(CC) -o $@ $^
- main.o:main.c
- $(CC) -c $<
- tool0.o:tool0.c
- $(CC) -c $<
- tool1.o:tool1.c
- $(CC) -c $<
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。