文件读写锁的小问题,求指导

小小攻城师 2011-05-25 10:25:52
我在看Beginning linux programming第三版。在第七章的时候看到自己机子上的跟书上的讲的不一样。简单描述一下:在文件的某个部分加上读锁的话可以再其上再加读锁,但不能加写锁;在某个部分加上写锁之后读锁和写锁都不能再加。但是在我的linux上(Fedora)上,我按照书上讲的加了一个写锁,但是再其上仍然可以加读锁。代码如下:file_lock3的代码:
#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的代码:

#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


最好是看过这本书的做过这个实验的帮我解答一下,谢谢
...全文
265 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
justkk 2011-05-25
  • 打赏
  • 举报
回复
代码太长了,哪个地方的行为与书上有区别?
小小攻城师 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 justkk 的回复:]

既然是验证写锁之后,能否再读锁
不要把程序搞这么复杂嘛,就一个固定区域加锁测试就行了

加锁探测成功与否不是通过fcntl的返回值来判断的吧,应该判断l_type == F_UNLCK
[/Quote]
谢谢楼上的,不过没有回答我想要的答案。ChinaUnix上有人找到我的答案,还是谢谢两位。
答案这里:http://bbs.chinaunix.net/thread-2323260-1-1.html
wylwhd 2011-05-25
  • 打赏
  • 举报
回复
关注。。。。。。。。。。
justkk 2011-05-25
  • 打赏
  • 举报
回复
既然是验证写锁之后,能否再读锁
不要把程序搞这么复杂嘛,就一个固定区域加锁测试就行了

加锁探测成功与否不是通过fcntl的返回值来判断的吧,应该判断l_type == F_UNLCK
小小攻城师 2011-05-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 justkk 的回复:]

代码太长了,哪个地方的行为与书上有区别?
[/Quote]
书上讲的是加了写锁之后就不能加读锁,但是我的程序调用fcntl这个函数的GETLK这个命令,意思是探测可以不可以加上锁,但是显示竟然成功了。书上的显示探测加锁是不可以成功的。

但是我随后真的在其写锁上加一个读锁,他又报失败。所以我想问fcntl这个函数的GETLK参数跟实际上使用SETLK参数加锁有区别?但是书上的例子是GETLK都不成功的。

23,216

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧