2012-03-02 51 views
3

是否可以在main()以外的其他功能中使用fcntl()?調用文件後文件是否解鎖?我這樣說是因爲在這種情況下,fcntl()lockfile()內的大部分內容都在功能returns以外的範圍內。fcntl()範圍可見性行爲?

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; 
} 
+0

請仔細閱讀並認爲我們將討論鳥類植絨行爲。 – 2012-03-02 22:38:55

回答

2

fcntl電話放置在文件上的鎖。它一直保留到文件關閉或鎖定被釋放。這些結構僅用於告知fcntl要做什麼。