当前位置: 技术问答>linux和unix
linux程序设计 文件保护 锁文件
来源: 互联网 发布时间:2017-05-30
本文导语: 在区域锁定文件这儿 对于共享锁和独占锁的访问 写了两个main代码 针对同一个文件 一个是获取共享锁 一个是获取独占锁 均在返回前sleep(60)秒 预期的结果是一个启动后,另一个会设置文件权限失败 现实的结果:...
在区域锁定文件这儿
对于共享锁和独占锁的访问
写了两个main代码 针对同一个文件
一个是获取共享锁
一个是获取独占锁
均在返回前sleep(60)秒
预期的结果是一个启动后,另一个会设置文件权限失败
现实的结果:两个都获取成功了
系统:fedora最新版本
代码如下:
main1:
#include
#include
#include
const char *test_file = "/home/zm_test.test";
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/writen", test_file);
exit(EXIT_FAILURE);
}
region_to_lock.l_type = F_WRLCK;
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 0;
region_to_lock.l_len = 10;
printf("Process %d, trying F_WRLCK, region %d to %dn", 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) {
printf("Process %d - failed to lock regionn", getpid());
} else {
printf("Process %d - obtained lock on regionn", getpid());
}
close(file_desc);
sleep(60);
exit(EXIT_SUCCESS);
}
main2:
#include
#include
#include
#include
const char *test_file = "/home/zm_test.test";
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/writen", test_file);
exit(EXIT_FAILURE);
}
region_to_lock.l_type = F_RDLCK;
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 0;
region_to_lock.l_len = 10;
printf("Process %d, trying F_WRLCK, region %d to %dn", 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) {
printf("Process %d - failed to lock regionn", getpid());
} else {
printf("Process %d - obtained lock on regionn", getpid());
}
close(file_desc);
sleep(60);
exit(EXIT_SUCCESS);
}
对于共享锁和独占锁的访问
写了两个main代码 针对同一个文件
一个是获取共享锁
一个是获取独占锁
均在返回前sleep(60)秒
预期的结果是一个启动后,另一个会设置文件权限失败
现实的结果:两个都获取成功了
系统:fedora最新版本
代码如下:
main1:
#include
#include
#include
const char *test_file = "/home/zm_test.test";
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/writen", test_file);
exit(EXIT_FAILURE);
}
region_to_lock.l_type = F_WRLCK;
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 0;
region_to_lock.l_len = 10;
printf("Process %d, trying F_WRLCK, region %d to %dn", 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) {
printf("Process %d - failed to lock regionn", getpid());
} else {
printf("Process %d - obtained lock on regionn", getpid());
}
close(file_desc);
sleep(60);
exit(EXIT_SUCCESS);
}
main2:
#include
#include
#include
#include
const char *test_file = "/home/zm_test.test";
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/writen", test_file);
exit(EXIT_FAILURE);
}
region_to_lock.l_type = F_RDLCK;
region_to_lock.l_whence = SEEK_SET;
region_to_lock.l_start = 0;
region_to_lock.l_len = 10;
printf("Process %d, trying F_WRLCK, region %d to %dn", 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) {
printf("Process %d - failed to lock regionn", getpid());
} else {
printf("Process %d - obtained lock on regionn", getpid());
}
close(file_desc);
sleep(60);
exit(EXIT_SUCCESS);
}
|
你这个问题着实让我也困惑了一会儿。刚才不管怎么尝试,修改锁的类型,没有效果;修改为F_GETLK检测下有没有锁,没有效果。终于,最后让我猛地注意到,你的这句
close(file_desc);
竟然放到了
sleep(60);
的前面。这就是问题所在了!关闭文件会使所有的文件锁都自动消失。如果关闭了文件再开始sleep过程,另一个进程就可以毫无压力的再打开文件,放置另一种锁了
明白了吗?
close(file_desc);
竟然放到了
sleep(60);
的前面。这就是问题所在了!关闭文件会使所有的文件锁都自动消失。如果关闭了文件再开始sleep过程,另一个进程就可以毫无压力的再打开文件,放置另一种锁了
明白了吗?