文件锁的竞争
来源:互联网 发布:音频剪辑软件mac版 编辑:程序博客网 时间:2024/05/16 19:07
//fcntl锁定文件#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<fcntl.h>const char *test_file = "/home/ndj/filelock/fcntl/test_lock.in";int main(){ int file_desc;//文件描述符 int byte_count; char *byte_to_write = "A"; struct flock region1; struct flock region2; int res; //打开一个文件描述符 file_desc = open(test_file, O_RDWR | O_CREAT, 0666); if(!file_desc) { fprintf(stderr, "Unable to open %s for read/write\n", test_file); exit(EXIT_FAILURE); } //给文件添加一些数据 for(byte_count=0; byte_count<100; byte_count++) (void)write(file_desc, byte_to_write, 1); //把文件10~30字节设为区域1, 并在其上设置共享锁 region1.l_type = F_RDLCK;//共享锁 region1.l_whence = SEEK_SET;//绝对位置 region1.l_start = 10; region1.l_len = 20; //把文件40~50字节设置为区域2,并在其上设置独占锁 region2.l_type = F_WRLCK;//独占锁 region2.l_whence = SEEK_SET; region2.l_start = 40; region2.l_len = 10; //现在锁定文件 printf("Process %d locking file\n", getpid()); res = fcntl(file_desc, F_SETLK, ®ion1); if(res == -1) fprintf(stderr, "Failed to lock region1\n"); res = fcntl(file_desc, F_SETLK, ®ion2); if(res == -1) fprintf(stderr, "Failed to lock region2\n"); //然后等一会 sleep(60);//等待其他进程访问 printf("Princess %d closing file\n", getpid()); close(file_desc); printf("SUCCESS=%d", EXIT_SUCCESS); exit(EXIT_SUCCESS);}
//文件锁的竞争#include<unistd.h>#include<stdlib.h>#include<stdio.h>#include<fcntl.h>const char *test_file="/home/ndj/filelock/fcntl/test_lock.in";//文件对象//const char *test_file = "/home/ndj/filelock/fcntl/test_lock.in";int main(){ int file_desc; struct flock region_to_lock; int res; //打开一个文件描述符 file_desc = open(test_file, O_RDWR | O_CREAT, 0666); if(!file_desc) { fprintf(stderr, "Unable to open %s for read/write\n", test_file); exit(EXIT_FAILURE); } //指定文件区域,并尝试在他们之上执行不同的锁定操作 region_to_lock.l_type = F_RDLCK;//共享锁 region_to_lock.l_whence = SEEK_SET; region_to_lock.l_start = 10; region_to_lock.l_len = 5; printf("Process %d, trying F_RDLCK, region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len)); res = fcntl(file_desc, F_SETLK, ®ion_to_lock); if(res == -1) { fprintf(stderr, "Process %d - failed to lock region\n", getpid()); // exit(EXIT_FAILURE); } else { printf("Process %d - obtained lock region\n", getpid()); } region_to_lock.l_type = F_UNLCK;//解锁 region_to_lock.l_whence = SEEK_SET; region_to_lock.l_start = 10; region_to_lock.l_len = 5; printf("Process %d, trying F_UNLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len)); res = fcntl(file_desc, F_SETLK, ®ion_to_lock); if(res == -1) { fprintf(stderr, "Process %d - fail to unlock region\n", getpid()); } else { printf("Process %d - unlocked region\n", getpid()); } region_to_lock.l_type = F_UNLCK;//解锁 region_to_lock.l_whence = SEEK_SET; region_to_lock.l_start = 0; region_to_lock.l_len = 50; printf("Process %d, trying F_UNLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len)); res = fcntl(file_desc, F_SETLK, ®ion_to_lock); if(res == -1) { fprintf(stderr, "Process %d - fail to unlock region\n", getpid()); } else { printf("Process %d - unlocked region\n", getpid()); } region_to_lock.l_type = F_WRLCK;//独占锁 region_to_lock.l_whence = SEEK_SET; region_to_lock.l_start = 16; region_to_lock.l_len = 5; printf("Process %d, trying F_WRLCK ,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len)); res = fcntl(file_desc, F_SETLK, ®ion_to_lock); if(res == -1) { fprintf(stderr, "Process %d - fail to lock region\n", getpid()); } else { printf("Process %d - locked region\n", getpid()); } region_to_lock.l_type = F_RDLCK;//共享锁 region_to_lock.l_whence = SEEK_SET; region_to_lock.l_start = 40; region_to_lock.l_len = 10; printf("Process %d, trying F_RDLCK,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len)); res = fcntl(file_desc, F_SETLK, ®ion_to_lock); if(res == -1) { fprintf(stderr, "Process %d - fail to lock region\n", getpid()); } else { printf("Process %d - locked region\n", getpid()); } region_to_lock.l_type = F_WRLCK;//独占锁 region_to_lock.l_whence = SEEK_SET; region_to_lock.l_start = 16; region_to_lock.l_len = 5; printf("Process %d, trying F_WELCK ,region %d to %d\n", getpid(), (int)region_to_lock.l_start, (int)(region_to_lock.l_start+region_to_lock.l_len)); res = fcntl(file_desc, F_SETLK, ®ion_to_lock); if(res == -1) { fprintf(stderr, "Process %d - fail to lock region\n", getpid()); } else { printf("Process %d - locked region\n", getpid()); } printf("Process %d ending\n", getpid()); close(file_desc); exit(EXIT_SUCCESS);}
阅读全文
0 0
- 文件锁的竞争
- 减少锁的竞争
- 多线程锁竞争造成的开销
- JAVA并发-减少锁的竞争
- JAVA并发-减少锁的竞争
- java-并发-解决锁竞争的问题
- JAVA并发-减少锁的竞争
- 软件的竞争就是服务的竞争
- 竞争:繁荣的硬道理
- 管理软件的竞争突破口
- 支付的竞争
- 非同寻常的竞争心
- 关于竞争的思考
- IT竞争的环境
- 多线程之间的竞争
- 竞争的策略
- 残酷的竞争
- 资源竞争和锁
- 泛函编程(13)-无穷数据流-Infinite Streamvvv
- JAVA-1036. 跟奥巴马一起编程(15)
- HTTP请求与响应基础
- 欢迎使用CSDN-markdown编辑器
- Optimal Number Permutation CodeForces
- 文件锁的竞争
- HTTP POST和GET区别
- 一次随Tomcat启动一个线程所映射的run()start()区别
- CodeForces
- JAVA-1043. 输出PATest(20)
- HDU—1596find the safest road(floyd算法模板题)
- c++实验6
- apt-get upgrade前,指定某些软件不更新
- leetcode 581: Shortest Unsorted Continuous Subarray