当前位置: 技术问答>linux和unix
文件读写锁的小问题,求指导
来源: 互联网 发布时间:2016-12-02
本文导语: 我在看Beginning linux programming第三版。在第七章的时候看到自己机子上的跟书上的讲的不一样。简单描述一下:在文件的某个部分加上读锁的话可以再其上再加读锁,但不能加写锁;在某个部分加上写锁之后读锁和写...
我在看Beginning linux programming第三版。在第七章的时候看到自己机子上的跟书上的讲的不一样。简单描述一下:在文件的某个部分加上读锁的话可以再其上再加读锁,但不能加写锁;在某个部分加上写锁之后读锁和写锁都不能再加。但是在我的linux上(Fedora)上,我按照书上讲的加了一个写锁,但是再其上仍然可以加读锁。代码如下:file_lock3的代码:
然后我用这样的命令运行 ./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
最好是看过这本书的做过这个实验的帮我解答一下,谢谢
#include
#include
#include
#include
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 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
最好是看过这本书的做过这个实验的帮我解答一下,谢谢
|
既然是验证写锁之后,能否再读锁
不要把程序搞这么复杂嘛,就一个固定区域加锁测试就行了
加锁探测成功与否不是通过fcntl的返回值来判断的吧,应该判断l_type == F_UNLCK
不要把程序搞这么复杂嘛,就一个固定区域加锁测试就行了
加锁探测成功与否不是通过fcntl的返回值来判断的吧,应该判断l_type == F_UNLCK
|
代码太长了,哪个地方的行为与书上有区别?
|
关注。。。。。。。。。。