【Linux】Linux文件锁

编程入门 行业动态 更新时间:2024-10-23 05:37:32

【Linux】Linux<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件锁"/>

【Linux】Linux文件锁

文件锁

前言

/proc是一个特殊的文件系统。

该目录下文件用来表示与启动、内核相关的特殊信息。

  1. /proc/cpuinfo——CPU详细信息

  2. /proc/meminfo——内存相关信息

  3. /proc/version——版本信息

  4. /proc/sys/fs/file-max——系统中能同时打开的文件总数

​ 可修改该文件

  1. 进程的相关信息——/proc/32689/ 表示指定进程(进程号为32689)的相关信息

  2. /proc/devices——已分配的字符设备、块设备的设备号


文件锁

  • 用于并发对文件I/O进行操作

用法

#include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );

参数

  • cmd——取值F_GETLK,F_SETLK和F_SETLKW,分别表示获取锁、设置锁、和同步设置锁。

struct flock {

short l_type; /*F_RDLCK, F_WRLCK, or F_UNLCK */

off_t l_start; /*offset in bytes, relative to l_whence */

short l_whence; /*SEEK_SET, SEEK_CUR, or SEEK_END */

off_t l_len; /*length, in bytes; 0 means lock to EOF */

pid_t l_pid; /*returned with F_GETLK */

};

  • l_type: 第一个成员是加锁的类型:只读锁,读写锁,或是解锁。

  • l_start和l_whence: 用来指明加锁部分的开始位置。

  • l_len: 是加锁的长度。

  • l_pid: 是加锁进程的进程id。

示例:

#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define FILE_NAME "flock_demo.txt"int flock_set(int fd, int type) {printf("pid=%d come in.\n",getpid());struct flock fflock;memset(&fflock, 0, sizeof(fflock));fcntl(fd,F_GETLK,&fflock);if (fflock.l_type != F_UNLCK) {if (fflock.l_type == F_RDLCK) {//有锁,判断是读锁还是写锁printf("flock has been set to read lock by %d\n",fflock.l_pid);} else if (fflock.l_type == F_WRLCK) {printf("flock has been set to write lock by %d\n", fflock.l_pid);}}//锁定文件fflock.l_type = type;fflock.l_whence = SEEK_SET;fflock.l_start = 0;fflock.l_len = 0;fflock.l_pid = -1;//阻塞式的if (fcntl(fd,F_SETLKW,&fflock) < 0) {printf("set lock failed!\n");return -1;}switch (fflock.l_type) {case F_RDLCK:printf("read lock is set by %d\n", getpid());break;case F_WRLCK:printf("write lock is set by %d\n", getpid());break;case F_UNLCK:printf("lock is released by %d\n", getpid());break;default:break;}printf("Process pid = %d out.\n",getpid());return 0;}int main(void) {int fd = 0;fd = open(FILE_NAME, O_RDWR | O_CREAT, 0666);if (fd < 0) {printf("open file %s failed!\n",FILE_NAME);exit(-1);}//flock_set(fd, F_RDLCK); //读锁flock_set(fd, F_WRLCK);	   //写锁getchar();flock_set(fd, F_UNLCK); //解锁getchar();close(fd);return 0;
}
  • 写锁是排他性的,文件上了写锁,就会阻止其他程序的写锁与读锁
  • 读锁可以多个程序对同一文件上读锁,除此之外其他情况也会失败(阻止其他程序的读锁与写锁)。

更多推荐

【Linux】Linux文件锁

本文发布于:2023-07-01 07:36:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/972702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!