【问题标题】:fcntl() scope visibility behavior?fcntl() 范围可见性行为?
【发布时间】: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;
}

【问题讨论】:

  • 阅读此文并认为我们将讨论鸟类的成群行为。

标签: c linux unix gcc posix


【解决方案1】:

fcntl 调用会锁定文件。它一直存在,直到文件关闭或锁被释放。这些结构只需要告诉fcntl做什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2018-11-14
    • 1970-01-01
    • 2015-08-30
    相关资源
    最近更新 更多