嵌入式Linux基础学习笔记-文件IO编程-文件锁(1)
生活随笔
收集整理的這篇文章主要介紹了
嵌入式Linux基础学习笔记-文件IO编程-文件锁(1)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文件鎖-文件共享
- 共享資源競(jìng)爭(zhēng)問題的解決方法:文件鎖。
- 文件鎖包括
- 建議性鎖 要求每個(gè)上鎖文件的進(jìn)程都要檢查是否有鎖存在,并且尊重 已有的鎖
- 強(qiáng)制性鎖 由內(nèi)核執(zhí)行的鎖,當(dāng)一個(gè)文件被上鎖進(jìn)行寫入操作的時(shí)候, 內(nèi)核將阻止其他任何文件對(duì)其進(jìn)行讀寫操作。
- 文件上鎖的函數(shù)
- lockf()
用于對(duì)文件施加建議性鎖 - fcntl()
不僅可以施加建議性鎖,還可以施加強(qiáng)制鎖。同時(shí)還能對(duì)文 件的某一記錄上鎖,也就是記錄鎖。
- lockf()
- 記錄鎖可分為
- 讀取鎖: 又稱為共享鎖,能夠使多個(gè)進(jìn)程都能在文件的同一部分建立 讀取鎖。
- 寫入鎖: 又稱為排斥鎖,在任何時(shí)刻只能有一個(gè)進(jìn)程在文件的某個(gè)部 分上建立寫入鎖。
- 注:在文件的同一部分不能同時(shí)建立讀取鎖和寫入鎖。
fcntl()函數(shù)格式
fcntl函數(shù)原型:
int fcntl(int fd, int cmd); int fcntl(int fd, int cmd, long arg); int fcntl(int fd, int cmd ,struct flock* lock);flock結(jié)構(gòu)
struct flock {short l_type; /*鎖的類型*/ off_t l_start; /*偏移量的起始位置:SEEK_SET,SEEK_CUR,SEEK_END*/ short l_whence; /*加鎖的起始偏移*/ off_t l_len; /*上鎖字節(jié)*/ pid_t l_pid; /*鎖的屬主進(jìn)程ID */ }-
flock結(jié)構(gòu)變量取值
-
上手代碼:
1. lock_set.c實(shí)現(xiàn)文件記錄鎖功能:
/* lock_set.c */ int lock_set(int fd, int type) {struct flock old_lock, lock;lock.l_whence = SEEK_SET;lock.l_start = 0;lock.l_len = 0;lock.l_type = type;lock.l_pid = -1;/* 判斷文件是否可以上鎖 */fcntl(fd, F_GETLK, &lock);//文件已有鎖if (lock.l_type != F_UNLCK){/* 判斷文件不能上鎖的原因 */if (lock.l_type == F_RDLCK) /* 該文件已有讀取鎖 */{printf("Read lock already set by %d\n", lock.l_pid);}else if (lock.l_type == F_WRLCK) /* 該文件已有寫入鎖 */{printf("Write lock already set by %d\n", lock.l_pid);} }/* l_type 可能已被F_GETLK修改過(guò) */lock.l_type = type;/* 根據(jù)不同的type值進(jìn)行阻塞式上鎖或解鎖 */if ((fcntl(fd, F_SETLKW, &lock)) < 0){printf("Lock failed:type = %d\n", lock.l_type);return 1;}switch(lock.l_type){case F_RDLCK:{printf("Read lock set by %d\n", getpid());}break;case F_WRLCK:{printf("Write lock set by %d\n", getpid());}break;case F_UNLCK:{printf("Release lock by %d\n", getpid());return 1;}break;default:break;}/* end of switch */return 0; }2. 文件寫入鎖的測(cè)試用例 write_lock.c:
創(chuàng)建一個(gè)hello文件,之后對(duì)其上寫入鎖,鍵盤輸入任意一個(gè)字符后解除寫入鎖。
/* write_lock.c */ #include <unistd.h> #include <sys/file.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include "lock_set.c"int main(void) {int fd;/* 首先打開文件 */fd = open("hello",O_RDWR | O_CREAT, 0644);if(fd < 0){printf("Open file error\n");exit(1);}/* 給文件上寫入鎖 *///調(diào)用lock_set函數(shù)lock_set(fd,F_WRLCK);getchar(); //輸入任意字符/* 給文件解鎖 */lock_set(fd,F_UNLCK);getchar(); //close(fd); exit(0); } 補(bǔ)充筆記:關(guān)于Linux權(quán)限 Linux 系統(tǒng)中采用三位十進(jìn)制數(shù)表示權(quán)限,如0755, 0644. ABCD A- 0, 表示十進(jìn)制 B-用戶 C-組用戶 D-其他用戶--- -> 0 (no excute , no write ,no read) --x -> 1 excute, (no write, no read) -w- -> 2 write -wx -> 3 write, excute r-- -> 4 read r-x -> 5 read, excute rw- -> 6 read, write , rwx -> 7 read, write , excute0755->即用戶具有讀/寫/執(zhí)行權(quán)限,組用戶和其它用戶具有讀寫權(quán)限; 0644->即用戶具有讀寫權(quán)限,組用戶和其它用戶具有只讀權(quán)限;3. 在兩個(gè)終端上運(yùn)行./write_lock,查看運(yùn)行結(jié)果
4. 編寫文件讀取鎖的測(cè)試用例read_lock.c:創(chuàng)建一個(gè)hello文件,之后對(duì)其上讀取鎖,鍵盤輸入任意一個(gè)字符后解除讀取鎖。
/* read_lock.c */ #include <unistd.h> #include <sys/file.h> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <stdlib.h> #include "lock_set.c" int main(void) {int fd;fd = open("hello",O_RDWR | O_CREAT, 0644);if(fd < 0){printf("Open file error\n");exit(1);}/* 給文件上讀取鎖 */lock_set(fd,F_RDLCK);getchar(); //輸入任意字符/* 給文件解鎖 */lock_set(fd,F_UNLCK);getchar(); //輸入任意字符close(fd);exit(0); }實(shí)現(xiàn)效果:
5. 在兩個(gè)終端上運(yùn)行./read_lock,查看運(yùn)行結(jié)果。
6. 驗(yàn)證: 如果在一個(gè)終端上運(yùn)行讀取鎖程序,在另一個(gè)終端上運(yùn)行寫入鎖程序,會(huì)有什么結(jié)果?
總結(jié)
以上是生活随笔為你收集整理的嵌入式Linux基础学习笔记-文件IO编程-文件锁(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [BUUCTF-pwn]——ciscn_
- 下一篇: 嵌入式Linux基础学习笔记-文件IO编