Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

上一節用 open()、read()、write()、lseek()、close() 完成了基本的文件操作。這一節討論文件已被多個用戶共享時如何避免競爭——Linux 的通用做法就是上鎖。

建議性鎖與強制性鎖

建議性鎖要求每個訪問文件的進程主動檢查並尊重既有的鎖,內核與系統本身並不強制。強制性鎖則由內核執行:文件被鎖並寫入時,內核會阻止其他進程讀寫。強制性鎖對性能影響很大,因為每次讀寫都要檢查鎖狀態。

Linux 中提供 lockf() 與 fcntl()。lockf() 只能施加建議性鎖;fcntl() 既能施加建議性鎖,也能施加強制性鎖,還支援對文件的某一記錄上鎖,也就是記錄鎖。

記錄鎖分兩類:讀取鎖又稱共享鎖,多個進程可在同一區域同時持有;寫入鎖又稱排斥鎖,同一時刻只允許一個進程持有。同一區域不能同時存在讀鎖與寫鎖。

fcntl() 功能很豐富,除了管理文件鎖,還能取得與設置文件描述符標志、複製描述符等,這裡先聚焦上鎖。

函數與結構

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

lock 參數是 flock 結構體:

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

其中 off_t 即 long int,pid_t 即 int。各字段含義如下:

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

基礎實驗:封裝上鎖函數

先給 flock 各字段賦值,然後用 F_GETLK 判斷能否上鎖:可以則 l_type 被置為 F_UNLCK,否則 l_pid 會填寫持鎖進程號。設定鎖則用 F_SETLK 或阻塞版的 F_SETLKW。

/*lock_set.c*/
int lock_set(int fd, int type)
{
    struct flock old_lock, lock;

    lock.l_whence = SEEK_SET;   /* 鎖定整個文件 */
    lock.l_start = 0;
    lock.l_len = 0;
    lock.l_type = type;
    lock.l_pid = -1;

    fcntl(fd, F_GETLK, &lock);
    if (lock.l_type != F_UNLCK) {
        if (lock.l_type == F_RDLCK)
            printf("Read lock already set by %d\n", lock.l_pid);
        else if (lock.l_type == F_WRLCK)
            printf("Write lock already set by %d\n", lock.l_pid);
    }

    lock.l_type = type;
    if ((fcntl(fd, F_SETLKW, &lock)) < 0) {
        printf("Lock failed:type=%d\n", lock.l_type);
        return 1;
    }
    switch (lock.l_type) {
    case F_RDLCK:
        printf("Read lock set by %d\n", getpid());
        break;
    case F_WRLCK:
        printf("Write lock set by %d\n", getpid());
        break;
    case F_UNLCK:
        printf("Release lock set by %d\n", getpid());
        return 1;
    default: break;
    }
    return 0;
}

寫入鎖測試

/*write_lock.c*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/file.h>
#include"lock_set.c"

int main(void)
{
    int fd;
    fd = open("/home/song/hello.c", O_RDWR|O_CREAT, 0644);
    if (fd < 0) {
        printf("Open file error!\n");
        exit(1);
    }
    lock_set(fd, F_WRLCK);   /* 加寫入鎖 */
    getchar();
    lock_set(fd, F_UNLCK);   /* 解鎖 */
    getchar();
    close(fd);
    exit(0);
}

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

為了便於觀察,開兩個終端同時運行同一程序,模擬多進程操作同一文件。先啟動終端 1,再看終端 2 的提示:

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

可見寫入鎖是互斥的,同一時刻只能存在一個。

讀取鎖測試

/*read_lock.c*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/file.h>
#include"lock_set.c"

int main(void)
{
    int fd;
    fd = open("/home/song/hello.c", O_RDWR|O_CREAT, 0644);
    if (fd < 0) {
        printf("Open file error!\n");
        exit(1);
    }
    lock_set(fd, F_RDLCK);
    getchar();
    lock_set(fd, F_UNLCK);
    getchar();
    close(fd);
    exit(0);
}

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

Linux 文件 I/O 編程(二):fcntl 文件鎖與記錄鎖實驗

對比就很清楚:讀取鎖是共享鎖,進程 7170 已加讀鎖後,進程 7294 依然能成功加上讀鎖。

總結一句:文件鎖存在的意義,就是在多進程共享同一文件時避免資源競爭。

Copyright © Linux教程網 All Rights Reserved