我正在写一个应用程序和它的规范中,我需要锁定 一个文件我每次写它(该文件将被读取其他团队正在工作的其他应用程序 上):C fcntl抽象函数不起作用
我做了以下功能:
int lock_file (int fd)
{
if (fd == -1)
return -1;
struct flock file_locker;
file_locker.l_type = F_WRLCK;
file_locker.l_whence = SEEK_SET;
file_locker.l_start = 0;
file_locker.l_len = 0; //lock the entire file
int locked = fcntl(fd, F_SETLK, &file_locker);
if (locked == -1){
/*handle errors*/
return 0;
}
return 1;
}
我可以得到1回(指一切正常),但是当我做了一个试验案例 我可能会在锁定的文件乌写
the te st代码是:
char *file = "lock_test_ok";
int fd = open(file, O_RDWR);
int locked = lock_file(fd);
/* call popen and try write 'ERROR' in the file */
/* if the file contains ERROR, than fail */
+1,我同意这真的是相关的问题。 – 2011-01-10 22:36:11