十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1. 注意fcntl()参数cmd 的正确使用
创新互联为企业级客户提高一站式互联网+设计服务,主要包括成都做网站、成都网站制作、成都app软件开发公司、小程序开发、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。
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
- #include
- #include
- #include
- 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
- #include
- #include
- #include
- #include
- #include
- 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 $<