您好,登錄后才能下訂單哦!
1. 注意fcntl()參數(shù)cmd 的正確使用
F_GETFL 用于測(cè)試鎖使用
F_SETFL 無(wú)阻塞設(shè)置鎖 fcntl()會(huì)嘗試幾次后,如果失敗直接返回-1
F_SETLKW 阻塞設(shè)置鎖 fcntl()會(huì)嘗試后,如果失敗會(huì)被系統(tǒng)掛起來(lái),直到收到解鎖的信號(hào)再去執(zhí)行
2. 測(cè)試鎖的時(shí)候 struct flock lock結(jié)構(gòu)體成員 中的l_stype 需要設(shè)置為F_WRLCK 其它F_UNLCK,F_RDLCK會(huì)有問(wèn)題
3.struct flock lock 成員使用
測(cè)試或鎖 整個(gè)文件怎樣設(shè)置
l_whence = SEEK_SET
l_start = 0;
l_len = 0; 如果l_len為0 表示 從start 開(kāi)始一直到文件最后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.程序自己測(cè)試自己的鎖, 測(cè)試后肯定是無(wú)鎖狀態(tài)
6.程序如果設(shè)置的兩個(gè)區(qū)間 連續(xù)而且鎖的性質(zhì)一樣,系統(tǒng)會(huì)自動(dòng)地將兩個(gè)鎖合并成一個(gè)鎖
7.更多可以參考以下牛人
http://zhuyunxiang.blog.51cto.com/653596/132548
執(zhí)行結(jié)果:
在另一個(gè)終端執(zhí)行
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 $<
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。