-
-
Save MerlinDMC/3197f4d13f8145c457e4 to your computer and use it in GitHub Desktop.
syscall.Flock() vs syscall.FcntlFlock()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flock_fcntl | |
import ( | |
"os" | |
"path/filepath" | |
"syscall" | |
"testing" | |
) | |
var ( | |
lock_filename string | |
) | |
func init() { | |
lock_filename = filepath.Join(os.TempDir(), "lock_test") | |
} | |
func TestFlock(t *testing.T) { | |
t.Logf("operating on file: %s\n", lock_filename) | |
fd, err := syscall.Open(lock_filename, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0666) | |
if err != nil { | |
t.Fatalf("error opening file: %s\n", err) | |
} | |
defer syscall.Unlink(lock_filename) | |
defer syscall.Close(fd) | |
// this one should succeed as we're not holding a lock already | |
err = syscall.Flock(fd, syscall.LOCK_EX|syscall.LOCK_NB) | |
if err != nil && err != syscall.EAGAIN { | |
t.Fatalf("error getting lock: %s\n", err) | |
} else if err == syscall.EAGAIN { | |
t.Fatalf("already locked: %s\n", err) | |
} | |
// get new fd | |
fdup, err := syscall.Open(lock_filename, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0666) | |
if err != nil { | |
t.Fatalf("error opening file: %s\n", err) | |
} | |
defer syscall.Close(fd) | |
// this should fail as we're holding a lock | |
err = syscall.Flock(fdup, syscall.LOCK_EX|syscall.LOCK_NB) | |
if err == nil { | |
t.Fatalf("getting lock for already locked resource.\n") | |
} else if err == syscall.EAGAIN { | |
t.Logf("already locked: %s\n", err) | |
} | |
} | |
func TestFcntlFlock(t *testing.T) { | |
t.Logf("operating on file: %s\n", lock_filename) | |
fd, err := syscall.Open(lock_filename, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0666) | |
if err != nil { | |
t.Fatalf("error opening file: %s\n", err) | |
} | |
defer syscall.Unlink(lock_filename) | |
defer syscall.Close(fd) | |
lk := syscall.Flock_t{ | |
Type: syscall.F_WRLCK, | |
Start: 0, | |
Len: 0, | |
Whence: 0, | |
} | |
// this one should succeed as we're not holding a lock already | |
err = syscall.FcntlFlock(uintptr(fd), syscall.F_SETLK, &lk) | |
if err != nil && err != syscall.EAGAIN { | |
t.Fatalf("error getting lock: %s\n", err) | |
} else if err == syscall.EAGAIN { | |
t.Fatalf("already locked: %s\n", err) | |
} | |
// get new fd | |
fdup, err := syscall.Open(lock_filename, syscall.O_CREAT|syscall.O_RDWR|syscall.O_CLOEXEC, 0666) | |
if err != nil { | |
t.Fatalf("error opening file: %s\n", err) | |
} | |
defer syscall.Close(fd) | |
// this should fail as we're holding a lock | |
err = syscall.FcntlFlock(uintptr(fdup), syscall.F_SETLK, &lk) | |
if err == nil { | |
t.Fatalf("getting lock for already locked resource.\n") | |
} else if err == syscall.EAGAIN { | |
t.Logf("already locked: %s\n", err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== RUN TestFlock | |
--- PASS: TestFlock (0.00s) | |
flock_test.go:19: operating on file: /var/folders/g8/dwjzl4_134143kw7yjzs_w5r0000gn/T/lock_test | |
flock_test.go:48: already locked: resource temporarily unavailable | |
=== RUN TestFcntlFlock | |
--- FAIL: TestFcntlFlock (0.00s) | |
flock_test.go:53: operating on file: /var/folders/g8/dwjzl4_134143kw7yjzs_w5r0000gn/T/lock_test | |
flock_test.go:87: getting lock for already locked resource. | |
FAIL | |
exit status 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In one window (on Solaris):
./locks
... sleeps...
In another window:
./locks
Can not grab lock: resource temporarily unavailable