fcntl函数之文件锁 F_SETLK

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>/**
*
*./myfcntl contentstruct flock {...short l_type;     F_RDLCK,F_WRLCK,F_UNLCKshort l_whence;   How to interpret l_start: SEEK_SET, SEEK_CUR, SEEK_ENDoff_t l_start;    Starting offset for lockoff_t l_len;      Number of bytes to lockpid_t l_pid;      PID of process blocking our lock (set by F_GETLK and F_OFD_GETLK)...};*
*/
int lock_reg(int fd, int cmd, short lock_type, short lock_whence, off_t lock_start, off_t lock_len)
{struct flock lock;lock.l_type = lock_type;lock.l_whence = lock_whence;lock.l_start = lock_start;lock.l_len = lock_len;lock.l_pid = getpid();if (fcntl(fd, cmd, &lock) < 0) {if (errno == EACCES || errno == EAGAIN ) {printf("file lock by other processes.\n");return -1;}printf("lock file fail.\n");return -1;}if (lock_type == F_WRLCK) {printf("lock by %d.\n", getpid());} else if (lock_type == F_UNLCK) {printf("unlock by %d.\n", getpid());}return 0;
}int reg_lock(int fd)
{return lock_reg(fd, F_SETLK, F_WRLCK, 0, SEEK_SET, 0);
}
int reg_unlock(int fd)
{return lock_reg(fd, F_SETLK, F_UNLCK, 0, SEEK_SET, 0);
}/*
*
*.myfcntl AAAAAA
*/
int main(int argv, char *argc[])
{char *buf;int i;int ret;int fd;if (argv < 2) {printf("argc error!\n");return -1;}fd = open("processes.txt", O_CREAT|O_RDWR|O_APPEND, 777);ret = reg_lock(fd);if (ret !=0 ) {return -1;}sleep(5);buf = argc[1];i = 0;while (i < strlen(buf)) {if (write(fd, buf+i, sizeof(char)) < sizeof(char)) {printf("printf out error!\n");return -1;}printf(" %c:out by pid %d.\n", buf[i], getpid());i++;sleep(1);}ret = reg_unlock(fd);if (ret !=0 ) {return -1;}close(fd);return 0;
}

在不同终端下,同时运行程序。第二个进程直接提示退出。
lock by 18709.
file lock by other processes.
B:out by pid 18709.
B:out by pid 18709.
B:out by pid 18709.
B:out by pid 18709.
B:out by pid 18709.
B:out by pid 18709.
B:out by pid 18709.
unlock by 18709.

fcntl函数之文件锁 F_SETLK相关推荐

  1. fcntl函数之文件锁 F_SETLKW

    fcntl函数之文件锁 F_SETLKW F_SETLK与F_SETLKW的区别: F_SETLK设的锁遇到锁被其他进程占用时,会立刻停止进程. F_SETLKW上锁是阻塞方式.设置的锁因为其他锁而被 ...

  2. linux系统编程之文件与I/O(六):fcntl 函数与文件锁

    2013-05-14 11:26 8290人阅读 评论(2) 收藏 举报  分类: linux系统编程(19)  版权声明:本文为博主原创文章,未经博主允许不得转载. 一.fcntl函数 功能:操纵文 ...

  3. Linux 系统 文件锁 fcntl函数详解

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

  4. Linux 文件锁 fcntl 函数详解

    Linux 文件锁 fcntl 函数详解 #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); ...

  5. C/C++进程文件锁 之 fcntl函数的用法总结(非阻塞O_NONBLOCK)

    fcntl系统调用可以用来对已打开的文件描述符进行各种控制操作以改变已打开文件的的各种属性 函数原型: #include<unistd.h> #include<fcntl.h> ...

  6. linux fcntl 函数 文件描述符选项控制

    功能描述:根据文件描述词来操作文件的特性. #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); ...

  7. sigaction 函数,sigemptyset()函数,fcntl()函数,isatty()函数

    第一个函数:使用 sigaction函数: signal 函数的使用方法简单,但并不属于POSIX标准,在各类UNIX平台上的实现不尽相同,因此其用途受 到了一定的限制.而 POSIX标准定义的信号处 ...

  8. c 如何加函数锁linux,Linux下C语言中fcntl函数用法说明

    本文最后更新于2017年8月18日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢! =Start= 缘由: 学习.提高需要 正文: 参考解答: fcntl – manipulate f ...

  9. c语言中fcntl.h函数库,fcntl函数的使用详解

    (1)fcntl函数说明 前面的这5个基本函数实现了文件的打开.读写等基本操作,这一节将讨论的是,在文 件已经共享的情况下如何操作,也就是当多个用户共同使用.操作一个文件的情况,这时,Linux 通常 ...

最新文章

  1. 记录一次nginx升级,支持ipv4和ipv6访问https
  2. 〖Linux〗Bash快捷键使用
  3. 如何编码和解码base64字符串?
  4. iis8 php mysql_windows2012下 iis8+php5.2+mysql5 配置
  5. linux多少个端口,Linux允许python使用多少个网络端口?
  6. 如何使用 Linq 获取每个分组中的第一个元素?
  7. springboot 多了8小时_日本人不明白:中国的奶茶有多好喝,值得排队8小时去买?...
  8. Swift - Realm数据库的使用详解(附样例)
  9. javascript中的options.add() options.remove() options(index)或options.item(index)
  10. 【AI芯片】中国AI芯片爆发,架构创新迫在眉睫
  11. 公司网站应该外包SEO公司还是自己去做?
  12. 最近发现谷歌浏览器打开网页速度很慢,比IE都慢
  13. 广告创意设计都有哪些类型
  14. Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
  15. 《STL源码剖析》RB-tree中increment 和 decrement 的作用
  16. [开心学php100天]第六天:用php玩转页面(基础篇)
  17. macOS长按键盘重复输入
  18. 算法--01背包问题(动态规划算法) 21-01-30
  19. MachineLearning(Hsuan-Tien Lin)第一讲
  20. 谈谈程序员如何快速提升职业技能

热门文章

  1. 关于换行这个动作,win 和 mac 的实现
  2. Unity3D笔记十七 Unity3D生命周期
  3. kotlin访问控制符可见性
  4. 运维人员日常工作(转自老男孩)
  5. Ajax与CustomErrors的尴尬
  6. js中变量作用域的小理解
  7. 程序员需要谨记的九大安全编码规则
  8. matlab生成HEX文件-任意信号 大于64K长度
  9. mysql5.6与mysql5.5不同
  10. POJ3185(简单BFS,主要做测试使用)