文件读写锁的小问题,求指导
我在看Beginning linux programming第三版。在第七章的时候看到自己机子上的跟书上的讲的不一样。简单描述一下:在文件的某个部分加上读锁的话可以再其上再加读锁,但不能加写锁;在某个部分加上写锁之后读锁和写锁都不能再加。但是在我的linux上(Fedora)上,我按照书上讲的加了一个写锁,但是再其上仍然可以加读锁。代码如下:file_lock3的代码:
C/C++ code
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
int main()
{
//some variable
int file_desc;
const char *test_file = "/tmp/test_lock";
int byte_count;
char *byte_to_write = "A";
struct flock region_1;
struct flock region_2;
int res;
//open a file
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);
}
// write somen data
for(byte_count = 0; byte_count < 100; byte_count++)
{
write(file_desc, byte_to_write, 1);
}
// add the share lock to the 10-30 byte region
region_1.l_type = F_RDLCK;
region_1.l_whence = SEEK_SET;
region_1.l_start = 10;
region_1.l_len = 20;
// add the write lock to the 40-50 byte region
region_2.l_type = F_WRLCK;
region_2.l_whence = SEEK_SET;
region_2.l_start = 40;
region_2.l_len = 10;
// lock the file
printf("Process %d locking file\n", getpid());
res = fcntl(file_desc, F_SETLK, ®ion_1);
if(res == -1)
{
fprintf(stderr, "Failed to lock the region 1\n");
}
res = fcntl(file_desc, F_SETLK, ®ion_2);
if(res == -1)
{
fprintf(stderr, "Failed to lock region 2\n");
}
// wait 60 seconds
sleep(60);
printf("Process %d release the lock\n", getpid());
close(file_desc);
exit(EXIT_SUCCESS);
}
file_lock4的代码:
C/C++ code
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#define SIZE_TO_TRY 5
void show_lock_info(struct flock *);
int main()
{
int file_desc;
const char *test_file = "/tmp/test_lock";
int res;
struct flock region_to_test;
int start_byte;
//open the test_file
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);
}
//test region by SIZE_TO_TRY byte
for(start_byte = 0; start_byte < 99; start_byte += SIZE_TO_TRY)
{
//test use the write lock
region_to_test.l_type = F_WRLCK;
region_to_test.l_whence = SEEK_SET;
region_to_test.l_start = start_byte;
region_to_test.l_len = SIZE_TO_TRY;
region_to_test.l_type = F_RDLCK;
region_to_test.l_whence = SEEK_SET;
region_to_test.l_start = start_byte;
region_to_test.l_len = SIZE_TO_TRY;
region_to_test.l_pid = -1;
printf("Test F_RDLCK on region from %d to %d\n", start_byte, start_byte+SIZE_TO_TRY);
res = fcntl(file_desc, F_GETLK, ®ion_to_test);
if(res == -1)
{
fprintf(stderr, "F_GETLK Failed\n");
exit(EXIT_FAILURE);
}
else
{
printf("F_RDLCK - Lock would succeed\n");
}
}
close(file_desc);
exit(EXIT_SUCCESS);
}
void show_lock_info(struct flock *to_show)
{
printf("l_type %d, ", to_show->l_type);
printf("l_whence %d, ", to_show->l_whence);
printf("l_start %d, ", to_show->l_start);
printf("l_len %d, ", to_show->l_len);
printf("l_pid %d, ", to_show->l_pid);
}
然后我用这样的命令运行 ./file_lock3 & ; ./file_lock4
运行的结果和书上不一样
Testing F_WRLCK on region from 0 to 5
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 0 to 5
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 5 to 10
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 5 to 10
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 10 to 15
Lock would fail. F_GETLK returned:
l_type 0, l_whence 0, l_start 10, l_len 20, l_pid 2289, Test F_RDLCK on region from 10 to 15
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 15 to 20
Lock would fail. F_GETLK returned:
l_type 0, l_whence 0, l_start 10, l_len 20, l_pid 2289, Test F_RDLCK on region from 15 to 20
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 20 to 25
Lock would fail. F_GETLK returned:
l_type 0, l_whence 0, l_start 10, l_len 20, l_pid 2289, Test F_RDLCK on region from 20 to 25
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 25 to 30
Lock would fail. F_GETLK returned:
l_type 0, l_whence 0, l_start 10, l_len 20, l_pid 2289, Test F_RDLCK on region from 25 to 30
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 30 to 35
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 30 to 35
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 35 to 40
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 35 to 40
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 40 to 45
Lock would fail. F_GETLK returned:
l_type 1, l_whence 0, l_start 40, l_len 10, l_pid 2289, Test F_RDLCK on region from 40 to 45
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 45 to 50
Lock would fail. F_GETLK returned:
l_type 1, l_whence 0, l_start 40, l_len 10, l_pid 2289, Test F_RDLCK on region from 45 to 50
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 50 to 55
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 50 to 55
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 55 to 60
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 55 to 60
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 60 to 65
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 60 to 65
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 65 to 70
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 65 to 70
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 70 to 75
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 70 to 75
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 75 to 80
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 75 to 80
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 80 to 85
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 80 to 85
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 85 to 90
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 85 to 90
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 90 to 95
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 90 to 95
F_RDLCK - Lock would succeed
Testing F_WRLCK on region from 95 to 100
F_WRLCK - Lock would succeed
Test F_RDLCK on region from 95 to 100
F_RDLCK - Lock would succeed
最好是看过这本书的做过这个实验的帮我解答一下,谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
本帖最后由 cokeboL 于 2011-05-25 11:17 编辑
回复 1# wsw908
没看过这书,这个不是书上代码吧。
第一个程序在不同区域分别加了读、写锁,没啥问题。
第二个程序你用的是F_GETLK:
复制代码而对于fcntl的返回值,只有出错的时候返回-1,出错情况包括被信号中断之类的,但F_GETLK这种参数,
不管是按flockptr设置了还是被拒绝设置返回了,都应该算是执行成功,所以都不是返回-1,if else判断条件
就用得不恰当了(这块我没具体试验,楼主打印下返回值看看然后把结果说下哈)。
另外这段代码:
复制代码自己赋值马上又改了没必要,一次就行了,如果读、写都想试那就赋一次F_GETLK一次。
不知道对不对~
非常感谢楼上的回答。
第一、是书上的源代码没有错,不过是我不小心写代码的时候抄错了。
第二、第二个自己赋值确实没必要改那么多,只需要改一个l_type和l_pid就可以了,照书上抄的,偷懒没有多思考,该打。
听了你的提示我后面自己查看了我自己的代码,终于找到失误的地方了,就是判断条件不对。真的是,写代码不懂脑子,灰常感谢楼上的回答。
复制代码应该为
复制代码
回复 3# wsw908
不客气~
APUE还是蛮好的~linux编程接口蛮好的~