您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。