-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
44 lines (37 loc) · 773 Bytes
/
main.go
File metadata and controls
44 lines (37 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
// "io"
"log"
"os"
"syscall"
"time"
)
const (
filename = "test.lock"
)
func main() {
file, err := os.Create(filename)
if err != nil {
log.Printf("error opening file: %s", err)
return
}
defer file.Close() // Closing the file will release the lock.
flockT := syscall.Flock_t{
Type: syscall.F_WRLCK,
Whence: 0,
Start: 0,
Len: 0,
}
// Passing the syscall.F_SETLKW syscall instead (mind the W) will let
// the program wait for the lock to be available (blocking) before
// proceeding.
err = syscall.FcntlFlock(file.Fd(), syscall.F_SETLK, &flockT)
if err != nil {
log.Printf("Could not acquire lock! %s", err)
return
}
log.Print("lock acquired, sleeping forever...")
for {
time.Sleep(time.Second * 2)
}
}