flock - 应用或删除上一个打开的文件的咨询锁

内容简介

#include

int flock(intfd, intoperation);

描述

应用或删除由 fd 所指定的打开文件的咨询锁。参数操作是执行下列操作之一:

标签

描述

LOCK_SH

Place a shared lock. More than one process may hold a shared lock for a given file at a given time.

LOCK_EX

Place an exclusive lock. Only one process may hold an exclusive lock for a given file at a given time.

LOCK_UN

Remove an existing lock held by this process.

A call to flock() may block if an incompatible lock is held by another process. To make a non-blocking request, include LOCK_NB (by ORing) with any of the above operations.

A single file may not simultaneously have both shared and exclusive locks.

Locks created by flock() are associated with an open file table entry. This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using any of these descriptors. Furthermore, the lock is released either by an explicit LOCK_UN operation on any of these duplicate descriptors, or when all such descriptors have been closed.

If a process uses open(2) (or similar) to obtain more than one descriptor for the same file, these descriptors are treated independently by flock(). An attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another descriptor.

A process may only hold one type of lock (shared or exclusive) on a file. Subsequentflock() calls on an already locked file will convert an existing lock to the new lock mode.

Locks created by flock() are preserved across an execve(2).

A shared or exclusive lock can be placed on a file regardless of the mode in which the file was opened.

返回值

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

错误

Error Code

描述

EBADF

fd is not a not an open file descriptor.

EINTR

While waiting to acquire a lock, the call was interrupted by delivery of a signal caught by a handler.

EINVAL

operation is invalid.

ENOLCK

The kernel ran out of memory for allocating lock records.

EWOULDBLOCK

The file is locked and the LOCK_NB flag was selected.

遵循于

4.4BSD (theflock(2) call first appeared in 4.2BSD). A version offlock(2), possibly implemented in terms offcntl(2), appears on most Unices.

注意

flock(2) does not lock files over NFS. Use fcntl(2) instead: that does work over NFS, given a sufficiently recent version of Linux and a server which supports locking.

Since kernel 2.0, flock(2) is implemented as a system call in its own right rather than being emulated in the GNU C library as a call to fcntl(2). This yields true BSD semantics: there is no interaction between the types of lock placed by flock(2) and fcntl(2), andflock(2) does not detect deadlock.

flock(2) places advisory locks only; given suitable permissions on a file, a process is free to ignore the use of flock(2) and perform I/O on the file.

flock(2) and fcntl(2) locks have different semantics with respect to forked processes and dup(2). On systems that implement flock() using fcntl(), the semantics of flock() will be different from those described in this manual page.

Converting a lock (shared to exclusive, or vice versa) is not guaranteed to be atomic: the existing lock is first removed, and then a new lock is established. Between these two steps, a pending lock request by another process may be granted, with the result that the conversion either blocks, or fails if LOCK_NB was specified. (This is the original BSD behaviour, and occurs on many other implementations.)

另请参阅

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

linux下flock函数,flock()函数 Unix/Linux相关推荐

  1. Linux系统常用函数,浅谈linux下的一些常用函数的总结(必看篇)

    1.exit()函数 exit(int n)  其实就是直接退出程序, 因为默认的标准程序入口为int main(int argc, char** argv),返回值是int型的. 一般在shell下 ...

  2. Linux系统怎么编译sin,linux下gcc编译sin函数出错的问题

    linux下gcc编译sin函数出错的问题 收藏 Q: I keep getting errors due to library functions being undefined, but I'm ...

  3. Linux下的内存对齐函数

    在Linux下内存对齐的函数包括posix_memalign, aligned_alloc, memalign, valloc, pvalloc,其各个函数的声明如下: int posix_memal ...

  4. linux uname内核,Linux下confstr与uname函数_获取C库与内核信息

    Linux下confstr与uname函数_获取C库与内核信息 #include #include  //uname int main(int argc, char **argv[]) { struc ...

  5. linux下C语言main函数参数解析

    1. linux下常见的main函数有两种形式: int main(int argc, char **argv) int main(int argc, char * argv[]) 2. 参数介绍 第 ...

  6. Linux 下调用UEFI的函数

    Linux 下调用UEFI的函数 摘要 Linux 调用UEFI function 时候调用约定的转换 64bits calling convention Microsoft calling conv ...

  7. linux下查看进程的线程数,linux查看进程的线程数

    top -H -p $PID  #查看对应进程的那个线程占用CPU过高 1.top -H 手册中说:-H : Threads toggle 加上这个选项启动top,top一行显示一个线程.否则,它一行 ...

  8. linux下的磁盘配额简介暨 linux下加挂硬盘续

    linux下的磁盘配额简介暨 linux下加挂硬盘续 实验环境 VMware 6.5.2 +redhat Enterprise 5 as 以上是内核版本,还有方便起见加挂一块硬盘 这只是我的环境 实验 ...

  9. linux下测试ftp传输,linux下ftp命令使用详解---linux文件传输ftp命令

    linux下ftp命令使用详解---linux文件传输ftp命令 上一篇 / 下一篇  2010-12-18 09:15:35 / 个人分类:Linux ftp(file transfer proto ...

  10. 过滤Linux下不同大小的文件,linux查找当前目录下 M/G 大小的文件,删除Linux下指定大小的文件

    过滤Linux下不同大小的文件,linux查找当前目录下 M/G 大小的文件,删除Linux下指定大小的文件 find ./ -type f -size +1G| xargs rm 在清理系统日志文件 ...

最新文章

  1. Spring Cloud Zuul中使用Swagger汇总API接口文档
  2. 划重点了!网络推广网站在建设时期该如何进行优化?
  3. 爬取百度网盘资源报user is not authorized, hitcode:119
  4. android动态添加标签,android – 动态添加Textview
  5. 第八周项目实践2 建立连串算法库
  6. java面向对象编程思想_Java面向对象编程思想
  7. 大数据分析-excel常用技巧
  8. Silverlight 用户代码未处理 TypeLoadException
  9. python小结教学_Python Str内部功能-个人课堂笔记,课后总结
  10. 最小方法ZOJ 1579 Bridge
  11. 抽象实现接口,default修饰接口方法
  12. 规划过程组-项目管理
  13. win10如何删除计算机用户,在WIN10账户下怎么删除administrator账户
  14. 小程序嵌套h5页面_快速小程序开发之微信小程序内嵌 H5
  15. 日语标点符号用法解说!
  16. 管家婆软件五大实用小技巧,不会用太可惜了
  17. NVIDIA 、 显卡、显卡组成、GPU 介绍、GPU与CPU。
  18. STM32Cube_FW_F4_V1.17 F4固件包百度网盘下载
  19. SQLMAP数据库注入工具下载安装
  20. 专业实训---摩拜共享单车

热门文章

  1. 这三个移动互联网案例让你大胆追梦
  2. 如何选购计算机流程,新手小白如何选购一款适合自己的笔记本电脑?
  3. Windows 微信3.3.0内测如何申请,附报名及下载地址
  4. 面试数据库知识点总结
  5. 推荐使用Windows10企业版LTSC的理由
  6. 【阅读论文】第八章--多图像的质量增强--博-自动化眼底图像分析技术可筛查糖尿病患者的视网膜疾病
  7. 演唱会门票1秒钟就没了?没错,跟你竞争的不是人……
  8. php-emoji使用微信,微信开发中emoji表情的问题
  9. javaWeb项目跑起来吧
  10. 【CIPS 2016】(1-3章)词法、句法、语义、语篇分析(研究进展、现状趋势)