0
我在我的ubuntu机器上执行了下面的c代码...我已经阅读了关于fcntl()用来锁定文件和甚至读如果F_WRLCK opton被设置为。 ..所以我按放弃锁之前启动这个程序,进入我试图通过file1.cpp直接双击打开文件两个ways-并在一个新的终端上运行不同的C程序都...时间被打开文件...所以怎么的fcntl()允许这些文件打开时F_WRLCK设置...filelock允许打开文件
int main(int argc, char *argv[])
{
struct flock fl = {F_WRLCK, SEEK_SET, 0, 0, 0 };
int fd;
fl.l_pid = getpid();
if ((fd = open("/home/file1.cpp", O_WRONLY)) == -1)
{
perror("open");
exit(1);
}
if (fcntl(fd, F_SETLKW, &fl) == -1)
{
perror("fcntl");
exit(1);
}
printf("Press <RETURN> to release lock: ");
getchar();
fl.l_type = F_UNLCK; /* set to unlock same region */
if (fcntl(fd, F_SETLK, &fl) == -1)
{
perror("fcntl");
exit(1);
}
printf("Unlocked.\n");
close(fd);
return 0;
}
@ R..i不明白...真正的fcntl()块,直到没有获得锁,但获得了锁之后,不应该读取或写入该文件来禁止同步,因为我已经获得一个写锁....如果没有那么应该使用哪个函数 – avinash
@ R ..我知道了......只需要阅读[this](http://www.thegeekstuff.com/2012/04/linux-文件锁定类型/),你的答案变得清晰 – avinash