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

相关文章

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

OpenHarmony鸿蒙开发( Beta5.0)无感配网详解

1、简介 无感配网是指在设备联网过程中无需输入热点相关账号信息,即可快速实现设备配网,是一种兼顾高效性、可靠性和安全性的配网方式。 2、配网原理 2.1 通信原理 手机和智能设备之间的信息传递,利用特有的NAN协议实现。利用手机和智能设备之间的WiFi 感知订阅、发布能力,实现了数字管家应用和设备之间的发现。在完成设备间的认证和响应后,即可发送相关配网数据。同时还支持与常规Sof

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

6.1.数据结构-c/c++堆详解下篇(堆排序,TopK问题)

上篇:6.1.数据结构-c/c++模拟实现堆上篇(向下,上调整算法,建堆,增删数据)-CSDN博客 本章重点 1.使用堆来完成堆排序 2.使用堆解决TopK问题 目录 一.堆排序 1.1 思路 1.2 代码 1.3 简单测试 二.TopK问题 2.1 思路(求最小): 2.2 C语言代码(手写堆) 2.3 C++代码(使用优先级队列 priority_queue)

K8S(Kubernetes)开源的容器编排平台安装步骤详解

K8S(Kubernetes)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。以下是K8S容器编排平台的安装步骤、使用方式及特点的概述: 安装步骤: 安装Docker:K8S需要基于Docker来运行容器化应用程序。首先要在所有节点上安装Docker引擎。 安装Kubernetes Master:在集群中选择一台主机作为Master节点,安装K8S的控制平面组件,如AP

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

嵌入式Openharmony系统构建与启动详解

大家好,今天主要给大家分享一下,如何构建Openharmony子系统以及系统的启动过程分解。 第一:OpenHarmony系统构建      首先熟悉一下,构建系统是一种自动化处理工具的集合,通过将源代码文件进行一系列处理,最终生成和用户可以使用的目标文件。这里的目标文件包括静态链接库文件、动态链接库文件、可执行文件、脚本文件、配置文件等。      我们在编写hellowor

LabVIEW FIFO详解

在LabVIEW的FPGA开发中,FIFO(先入先出队列)是常用的数据传输机制。通过配置FIFO的属性,工程师可以在FPGA和主机之间,或不同FPGA VIs之间进行高效的数据传输。根据具体需求,FIFO有多种类型与实现方式,包括目标范围内FIFO(Target-Scoped)、DMA FIFO以及点对点流(Peer-to-Peer)。 FIFO类型 **目标范围FIFO(Target-Sc

019、JOptionPane类的常用静态方法详解

目录 JOptionPane类的常用静态方法详解 1. showInputDialog()方法 1.1基本用法 1.2带有默认值的输入框 1.3带有选项的输入对话框 1.4自定义图标的输入对话框 2. showConfirmDialog()方法 2.1基本用法 2.2自定义按钮和图标 2.3带有自定义组件的确认对话框 3. showMessageDialog()方法 3.1