【发布时间】:2012-03-02 21:10:30
【问题描述】:
是否可以在 main() 以外的函数中使用 fcntl() ?调用后文件是否解锁?我这样说是因为在这种情况下 fcntl() 和 lockfile() 中的大多数其他内容都超出了函数 returns 的范围。
int lockfile(void){
int fd;
const char *path = "path-to-lockfile";
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0
fl.l_pid = getpid();
fd = open(path, O_RDWR|O_CREAT);
fcntl(fd, F_SETLKW, &fl);
return fd;
}
【问题讨论】:
-
阅读此文并认为我们将讨论鸟类的成群行为。