当前位置:网站首页>The use of file lock in golang

The use of file lock in golang

2020-12-06 09:07:16 osc_ 60zeaho1

Preface

The title is golang The use of file locks , But the purpose of this paper is actually through golang How to use the file lock under , Take a look at the mechanism behind the file lock .

Why file locks are needed

Only multithreading / Read and write files in the concurrent scenario of multiple processes , You need a lock ,

scene 1- Read write concurrency

In the concurrent read-write scenario , If not locked , You're going to read dirty data . Imagine , The process of reading files , Read No 500 byte , There are other processes writing to the file in the form of overlay write 1000 byte , After the reading process reads 500 Bytes are dirty data .

scene 2- Write concurrent

In the concurrent write scenario , If not locked , hypothesis A Process write first 0-1000 byte ,B Process write 0-900 byte , And so on , The last process writes 0-100 byte , The final file content is before each process 100 It's a jumble of bits .

Several concepts of file lock

Shared lock

Shared lock , It's also called read lock . After a process acquires a shared lock for the first time , Will generate a lock type variable L, Type marked as shared lock . When other processes acquire a read lock ,L Add to the counter in 1, Indicates that another process has acquired the shared lock . At this time, if there is a process to acquire the exclusive lock , Will get failed .

Exclusive lock

Exclusive lock , Also called write locks . The first time a process acquires an exclusive lock , Will generate a lock type variable L, Type marked as exclusive lock . When other processes acquire any type of lock , Will gain failure .

Blocking

Blocking means to say , The new process finds the current file ( data ) After being locked , Will always be in a waiting state , Until the lock is released , Will continue the next step of behavior .

Non blocking

Non blocking means to say , The new process finds the current file ( data ) After being locked , Return exception immediately . Business needs to handle the exception according to the specific business scenarios .

Blocking and non blocking are actually two processing modes when a process encounters a lock .

golang How to use file lock

Basic use


package main

import (
    "log"
    "os"
    "syscall"
)

func main() {
    f, err := os.Create("example.txt")
    if err != nil {
        log.Println("create file example.txt failed", err)
    }
    defer f.Close()
    //  In non blocking mode , Add shared lock 
    if err := syscall.Flock(int(f.Fd()), syscall.LOCK_SH|syscall.LOCK_NB); err != nil {
        log.Println("add share lock in no block failed", err)
    }
    //  Business logic here 
    // TODO

    //  Unlock 
    if err := syscall.Flock(int(f.Fd()), syscall.LOCK_UN); err != nil {
        log.Println("unlock share lock failed", err)
    }

    return
}

Example LOCK_SH Indicates that the shared lock is currently acquired , If it is LOCK_EX, It means that the lock is exclusive . and LOCK_NB Indicates that the current lock acquisition mode is non blocking mode , If blocking mode is needed , Without this parameter .LOCK_UN It means unlock , Release the lock .

golang The way to use this file lock is actually Linux System level call under , It uses Linux Related capabilities of native file locks .

Use flock A few points to pay attention to

1、 as long as fd It points to the same file pointer , Then the locking and unlocking behavior is inherited and overridden ( This can be seen in the final explanation ).

2、flock In this way, a suggestion lock is added , In other words, the new process of "three seven" is no matter , Don't go through flock Get the lock , All kinds of operations on files , It can also work normally .

say Linux Below flock and fcntl

and flock equally ,fcntl It's also a system level call , But in the specific use, there is a great need for , And the two locks don't interfere with each other , use flock Lock ,fcntl Can't perceive , And vice versa .

Suggestion lock and force lock

flock It's a suggestive lock , and fcntl The lock is mandatory .

Suggestion lock , It's an agreement in essence , Check whether the file is locked by other processes before reading and writing . If you don't comply with this Agreement , As soon as you come up, you can operate the file , Don't check for locks , There is no problem in program execution , Can execute successfully .

Mandatory lock , It's more like a real lock . Just lock it up , Other processes cannot perform non allowed operations .

In fact, some use redis Distributed locks , It's all suggestive locks . The lock mechanism should work , We need to abide by this agreement .

Global lock and local lock

For a file ,flock The scope of locking is the entire file content , and fcntl Can lock any part of a file .

The lock holder problem

flock Think , The holder of the lock is the file table ( Can be understood as a file pointer ), So for fork and dup operation , They all correspond to the same file pointer , All operations will be applied to this file . Specific performance :

  • A Process locking ,A The child process of can be unlocked , The new operation will override the previous operation
  • A Process locking ,A Process replication fd, It's still possible to pass the new fd Operation file lock , The new operation will override the previous operation

fcntl Think , The holder of the lock is the process . Locking and unlocking actions follow the process , Specific performance: :

  • A Process locking ,B The process has to wait A Only when the process dies or unlocks can it be locked

Reference resources

[1] The forgotten paradise ——flock File lock

[2] Linux File lock learning -flock, lockf, fcntl

Originality is not easy. , Welcome to my official account. : Manon's road to freedom

版权声明
本文为[osc_ 60zeaho1]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201206090411131o.html