Skip to content

Examples on mutually-locking POSIX locks between Python, Go, and Rust

Notifications You must be signed in to change notification settings

chrisglass/posix-locks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

posix-locks

This is a very simple repository demonstrating how to create mutually excluding POSIX file locks between go, python, and rust.

This ONLY works on LINUX. Possibly only on x86/libc (I've not tried on other arches or stdlib implementations).

Notes about POSIX locks vs. flocks

POSIX locks are created and held between the file and the PID holding the lock, which means that a single golang process will be considered a single actor no matter how many goroutines it spins up

Instead, using the flock (syscall.Flock(...)) syscall will create a (non-POSIX) lock between the file descriptor and the file, meaning two goroutines Open()ing the same file separately will be able to deadlock (they each are considered separate actors).

Using this repository

Building the golang program

Simply invoking go build at the root of this project should create a posix-locks binary you can then run. It will grab a hardcoded lock in the current directory forever (until killed).

go run main.go

Running the python program

As for the golang program, the python3 script will acquire a lock on a hardcoded lock in the root directory of this project, and sleep forever (until killed).

python posix_locks.py

Running the rust program

As for previous programs, the rust version will acquire a lock on a hardcoded file in the root directory of this project, and sleep forever (until killed).

cargo run

Results

Running one or the other programs will fail if the other program is currently running.

While one of the programs is running, a POSIX lock is held, as shown with e.g. the lslocks command.

About

Examples on mutually-locking POSIX locks between Python, Go, and Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published