How to implement file lock in golang

青灯夜游
Release: 2022-12-19 11:08:18
Original
4999 people have browsed it

In golang, you can use the API of the sync package to implement file locking. File lock (flock) is an advisory lock for the entire file; that is, if a process places a lock on a file (inode), other processes can know it (advisory locks do not force processes to comply); file locks The calling syntax is "syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)".

How to implement file lock in golang

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

When we use Go language to develop some programs, multiple processes often operate on the same file at the same time, which can easily lead to confusion of the data in the file. At this time, we need to use some means to balance these conflicts, and file lock (flock) came into being. Let's introduce it below.

For flock, the most common example is Nginx. After the process runs, the current PID will be written to this file. Of course, if this file already exists, that is, the previous process has not exited, then Nginx It will not restart, so flock can also be used to detect whether the process exists.

flock is an advisory lock for the entire file. In other words, if a process places a lock on a file (inode), other processes can know it (advisory locks do not force processes to comply). The best part is that its first parameter is a file descriptor, and when this file descriptor is closed, the lock is automatically released. When the process terminates, all file descriptors will be closed. So often there is no need to consider things like unlocking atomic locks.

Before introducing it in detail, let’s introduce the code first

package main
import (
    "fmt"
    "os"
    "sync"
    "syscall"
    "time"
)
//文件锁
type FileLock struct {
    dir string
    f   *os.File
}
func New(dir string) *FileLock {
    return &FileLock{
        dir: dir,
    }
}
//加锁
func (l *FileLock) Lock() error {
    f, err := os.Open(l.dir)
    if err != nil {
        return err
    }
    l.f = f
    err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
    if err != nil {
        return fmt.Errorf("cannot flock directory %s - %s", l.dir, err)
    }
    return nil
}
//释放锁
func (l *FileLock) Unlock() error {
    defer l.f.Close()
    return syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN)
}
func main() {
    test_file_path, _ := os.Getwd()
    locked_file := test_file_path
    wg := sync.WaitGroup{}
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func(num int) {
            flock := New(locked_file)
            err := flock.Lock()
            if err != nil {
                wg.Done()
                fmt.Println(err.Error())
                return
            }
            fmt.Printf("output : %d\n", num)
            wg.Done()
        }(i)
    }
    wg.Wait()
    time.Sleep(2 * time.Second)
}
Copy after login

When running the above code under Windows system, the following error will appear:

How to implement file lock in golang

This is because Windows systems do not support pid locks, so we need to run the above program normally under Linux or Mac systems.

The above code demonstrates starting 10 goroutines at the same time, but during the running of the program, only one goroutine can obtain the file lock (flock). Other goroutinue will throw exception information after failing to obtain flock. This can achieve the effect of allowing only one process to access the same file within a specified period.

The specific call of the file lock in the code:

syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
Copy after login

We use syscall.LOCK_EX, syscall.LOCK_NB, what does this mean?

flock is a advisory lock and is not mandatory. One process uses flock to lock the file, and the other process can directly operate the file being locked and modify the data in the file. The reason is that flock is only used to detect whether the file is locked. If the file is already locked, another process In the case of writing data, the kernel will not block the write operation of this process, which is the kernel processing strategy of advisory locks.

flock has three main operation types:

  • LOCK_SH: Shared lock, multiple processes can use the same lock, often used as a read shared lock;

  • LOCK_EX: Exclusive lock, only allowed to be used by one process at the same time, often used as a write lock;

  • LOCK_UN: Release the lock.

When a process uses flock to try to lock a file, if the file is already locked by another process, the process will be blocked until the lock is released, or the LOCK_NB parameter is used when calling flock. When trying to lock the file, it is found that it has been locked by another service, and an error will be returned with the error code EWOULDBLOCK.

Flock lock release is very unique. You can call the LOCK_UN parameter to release the file lock, or you can release the file lock by closing fd (the first parameter of flock is fd), which means flock will It is automatically released when the process is closed.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of How to implement file lock in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!