Linux 文件鎖

系統(tǒng)調(diào)用fcntl

#include <unistd.h>
#include <fcntl.h>

int fcntl(int fd, int cmd, struct flock *lock );

參數(shù)
fd:文件描述符
cmd:F_GETLK, F_SETLK or F_SETLKW
lock:按照如下規(guī)則填寫(xiě),可以達(dá)到相應(yīng)效果

F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test for the existence of record locks (also known as file-segment  or  file-region
       locks).  The third argument, lock, is a pointer to a structure that has at least the following fields (in unspecified order).

           struct flock {
               ...
               short l_type;    /* Type of lock: F_RDLCK,
                                   F_WRLCK, F_UNLCK */
               short l_whence;  /* How to interpret l_start:
                                   SEEK_SET, SEEK_CUR, SEEK_END */
               off_t l_start;   /* Starting offset for lock */
               off_t l_len;     /* Number of bytes to lock */
               pid_t l_pid;     /* PID of process blocking our lock
                                   (F_GETLK only) */
               ...
           };

文件鎖的封裝

filelock.h

#ifndef _FILE_LOCK_H
#define _FILE_LOCK_H

int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len);

int File_ReadLck(int fd, off_t offset, int whence, off_t len);
int File_WriteLck(int fd, off_t offset, int whence, off_t len);

int File_ReadLckW(int fd, off_t offset, int whence, off_t len);
int File_WriteLckW(int fd, off_t offset, int whence, off_t len);


int File_Unlock(int fd, off_t offset, int whence, off_t len);

#endif

filelock.c

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>

#include "filelock.h"

int lock_reg(int fd, int cmd, int type, off_t offset, int whence, off_t len)
{
    struct flock lock;

    lock.l_type = type;
    lock.l_start = offset;
    lock.l_whence = whence;
    lock.l_len = len;

    return (fcntl(fd, cmd, &lock));
}


int File_ReadLck(int fd, off_t offset, int whence, off_t len)
{
    return lock_reg(fd, F_SETLK, F_RDLCK, offset, whence, len);
}


int File_WriteLck(int fd, off_t offset, int whence, off_t len)
{
    return lock_reg(fd, F_SETLK, F_WRLCK, offset, whence, len);
}


int File_ReadLckW(int fd, off_t offset, int whence, off_t len)
{
    return lock_reg(fd, F_SETLKW, F_RDLCK, offset, whence, len);
}


int File_WriteLckW(int fd, off_t offset, int whence, off_t len)
{
    return lock_reg(fd, F_SETLKW, F_WRLCK, offset, whence, len);    
}


int File_Unlock(int fd, off_t offset, int whence, off_t len)
{
    return lock_reg(fd, F_SETLK, F_UNLCK, offset, whence, len);
}

例程

#include <stdio.h>
#include <fcntl.h>

#include "filelock.h"

int main(void)
{
    int fd = 0;
    pid_t pid = 0;

    fd = open("tmp", O_RDWR|O_CREAT|O_TRUNC, 0666);
    unlink("tmp");

    pid = fork();

    if(0 == pid)
    {
        while(1)
        {
            File_WriteLckW(fd, 0, SEEK_SET, 1);
            printf("====> child process\n\n");
            usleep(5000000);
            File_Unlock(fd, 0, SEEK_SET, 1);
            usleep(500000);
        }
    }

    while(1)
    {
        File_WriteLckW(fd, 0, SEEK_SET, 1);
        printf("#### parent process\n\n");
        usleep(5000000);
        File_Unlock(fd, 0, SEEK_SET, 1);
        usleep(500000);
    }
    
    return 0;
}

文件鎖的用法

  • 不同的對(duì)象訪問(wèn)同一文件,可以使用文件鎖達(dá)到讀寫(xiě)同步,不會(huì)導(dǎo)致文件寫(xiě)入混亂。
  • 進(jìn)程間訪問(wèn)同一資源,使用文件鎖作為進(jìn)程間的鎖達(dá)到訪問(wèn)同步;用法:創(chuàng)建一個(gè)空文件,并使用文件的第一字節(jié)(無(wú)需存在)作為鎖字節(jié),然后unlink文件;獲取資源之前,先獲取該字節(jié)的寫(xiě)鎖;釋放資源時(shí),對(duì)該字節(jié)解鎖。記錄鎖的一個(gè)優(yōu)點(diǎn)是:持有鎖的進(jìn)程終止退出時(shí),內(nèi)核會(huì)自動(dòng)釋放該鎖,所以進(jìn)程間就不會(huì)因?yàn)檫M(jìn)程崩潰導(dǎo)致死鎖。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,552評(píng)論 25 708
  • 我們家草莓有點(diǎn)嘛怪 而且好假哇。。。
    愛(ài)吃的小胖紙閱讀 126評(píng)論 0 0
  • 不思終生昨日夢(mèng) 回憶今生事事情 雙鬢霜至無(wú)問(wèn)津 只得冷心心痛心
    SmileToLin閱讀 172評(píng)論 0 3
  • 上月去到宜家,發(fā)現(xiàn)宜家樣本間所有電視由原來(lái)的“海信”換成了“小米”,再加上最近這一年小米在全國(guó)開(kāi)設(shè)了40多家“小米...
    張臭臭826閱讀 1,945評(píng)論 1 1
  • 機(jī)緣巧合之下,我今天參加了一場(chǎng)由友邦保險(xiǎn)舉辦的,關(guān)于保險(xiǎn)從業(yè)者的培訓(xùn)。 并不是因?yàn)橄氤蔀樗^的保險(xiǎn)從業(yè)者,而是純粹...
    188c97774176閱讀 2,471評(píng)論 1 0