struct flock 结构体详解及用法

2023-12-17 18:58

本文主要是介绍struct flock 结构体详解及用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//该部分内容摘抄至网络

功能 :定义一些文件的锁的选项

Description

The flock structure in the /usr/include/sys/flock.h file, which describes a lock, contains the following fields:

 

l_type Describes the type of lock. If the value of the Command parameter to the fcntl subroutine is F_SETLK orF_SETLKW, the l_type field indicates the type of lock to be created. Possible values are:
F_RDLCK
A read lock is requested.
F_WRLCK
A write lock is requested.
F_UNLCK
Unlock. An existing lock is to be removed.

If the value of the Command parameter to the fcntl subroutine is F_GETLK, the l_typefield describes an existing lock. Possible values are:

F_RDLCK
A conflicting read lock exists.
F_WRLCK
A conflicting write lock exists.
F_UNLCK
No conflicting lock exists.
l_whence Defines the starting offset. The value of this field indicates the point from which the relative offset, the l_startfield, is measured. Possible values are:
SEEK_SET
The relative offset is measured from the start of the file.
SEEK_CUR
The relative offset is measured from the current position.
SEEK_END
The relative offset is measured from the end of the file.

These values are defined in the unistd.h file.

l_start Defines the relative offset in bytes, measured from the starting point in the l_whence field.
l_len Specifies the number of consecutive bytes to be locked.
l_sysid Contains the ID of the node that already has a lock placed on the area defined by the fcntlsubroutine. This field is returned only when the value of the Command parameter isF_GETLK.
l_pid Contains the ID of a process that already has a lock placed on the area defined by thefcntl subroutine. This field is returned only when the value of the Command parameter isF_GETLK.

l_vfs

Specifies the file system type of the node identified in the l_sysid field.

 举例说明:在嵌入式编程中经常会遇到写配置文件的问题,这个时候由于多进程操作就需要跟配置文件加写锁操作。

int cfg_save()
{int fd = -1;struct flock lock;struct stat stat_buf;char *p = NULL;char description[200] = {0};fd = open(cfg_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);if (fd == -1){sprintf(description,"open error %s.", cfg_file);write_log(time(NULL), SER_ERR, CFG_LOG, "cfg_func", "cfg_save",description);return -1;}/* Set a write lock */lock.l_type = F_WRLCK;lock.l_start = 0;lock.l_whence = SEEK_SET;lock.l_len = 0;if (fcntl(fd, F_SETLKW, &lock) == -1 || fstat(fd, &stat_buf) == -1) {close(fd);fprintf(stderr,"lock cfg_file error\n");return -1;}save_to_tree();if(tree == NULL){fprintf(stderr,"cfg_save  error\n");write_log(time(NULL), SER_ERR, CFG_LOG, "cfg_func", "cfg_save","cfg_save  error.");close(fd);return -1;}mxmlSaveFd(tree, fd, whitespace_cb);//把值写入配置文件if (tree!=NULL) {mxmlDelete(tree);tree = NULL;}close(fd);return 0;
}



这篇关于struct flock 结构体详解及用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/505468

相关文章

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

Spring Cloud LoadBalancer 负载均衡详解

《SpringCloudLoadBalancer负载均衡详解》本文介绍了如何在SpringCloud中使用SpringCloudLoadBalancer实现客户端负载均衡,并详细讲解了轮询策略和... 目录1. 在 idea 上运行多个服务2. 问题引入3. 负载均衡4. Spring Cloud Load

Springboot中分析SQL性能的两种方式详解

《Springboot中分析SQL性能的两种方式详解》文章介绍了SQL性能分析的两种方式:MyBatis-Plus性能分析插件和p6spy框架,MyBatis-Plus插件配置简单,适用于开发和测试环... 目录SQL性能分析的两种方式:功能介绍实现方式:实现步骤:SQL性能分析的两种方式:功能介绍记录

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.