fcntl函数之文件锁 F_SETLK
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相关推荐
- fcntl函数之文件锁 F_SETLKW
fcntl函数之文件锁 F_SETLKW F_SETLK与F_SETLKW的区别: F_SETLK设的锁遇到锁被其他进程占用时,会立刻停止进程. F_SETLKW上锁是阻塞方式.设置的锁因为其他锁而被 ...
- linux系统编程之文件与I/O(六):fcntl 函数与文件锁
2013-05-14 11:26 8290人阅读 评论(2) 收藏 举报 分类: linux系统编程(19) 版权声明:本文为博主原创文章,未经博主允许不得转载. 一.fcntl函数 功能:操纵文 ...
- Linux 系统 文件锁 fcntl函数详解
#include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int ...
- Linux 文件锁 fcntl 函数详解
Linux 文件锁 fcntl 函数详解 #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); ...
- C/C++进程文件锁 之 fcntl函数的用法总结(非阻塞O_NONBLOCK)
fcntl系统调用可以用来对已打开的文件描述符进行各种控制操作以改变已打开文件的的各种属性 函数原型: #include<unistd.h> #include<fcntl.h> ...
- linux fcntl 函数 文件描述符选项控制
功能描述:根据文件描述词来操作文件的特性. #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); ...
- sigaction 函数,sigemptyset()函数,fcntl()函数,isatty()函数
第一个函数:使用 sigaction函数: signal 函数的使用方法简单,但并不属于POSIX标准,在各类UNIX平台上的实现不尽相同,因此其用途受 到了一定的限制.而 POSIX标准定义的信号处 ...
- c 如何加函数锁linux,Linux下C语言中fcntl函数用法说明
本文最后更新于2017年8月18日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢! =Start= 缘由: 学习.提高需要 正文: 参考解答: fcntl – manipulate f ...
- c语言中fcntl.h函数库,fcntl函数的使用详解
(1)fcntl函数说明 前面的这5个基本函数实现了文件的打开.读写等基本操作,这一节将讨论的是,在文 件已经共享的情况下如何操作,也就是当多个用户共同使用.操作一个文件的情况,这时,Linux 通常 ...
最新文章
- 记录一次nginx升级,支持ipv4和ipv6访问https
- 〖Linux〗Bash快捷键使用
- 如何编码和解码base64字符串?
- iis8 php mysql_windows2012下 iis8+php5.2+mysql5 配置
- linux多少个端口,Linux允许python使用多少个网络端口?
- 如何使用 Linq 获取每个分组中的第一个元素?
- springboot 多了8小时_日本人不明白:中国的奶茶有多好喝,值得排队8小时去买?...
- Swift - Realm数据库的使用详解(附样例)
- javascript中的options.add() options.remove() options(index)或options.item(index)
- 【AI芯片】中国AI芯片爆发,架构创新迫在眉睫
- 公司网站应该外包SEO公司还是自己去做?
- 最近发现谷歌浏览器打开网页速度很慢,比IE都慢
- 广告创意设计都有哪些类型
- Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000004
- 《STL源码剖析》RB-tree中increment 和 decrement 的作用
- [开心学php100天]第六天:用php玩转页面(基础篇)
- macOS长按键盘重复输入
- 算法--01背包问题(动态规划算法)	21-01-30
- MachineLearning(Hsuan-Tien Lin)第一讲
- 谈谈程序员如何快速提升职业技能