Skip to content

Commit fa0127b

Browse files
authored
Merge pull request #80 from yuk1ty/section-10_2
10.2: `nix::fcntl::flock()` を使って `Cargo.toml` に排他ロックを掛け、10秒sleepするプログラム
2 parents 0e89071 + fdbb0a8 commit fa0127b

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter10/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ edition = "2018"
88

99
[dependencies]
1010
lib = { path = "../lib" }
11+
nix = "0.20.0"
1112
notify = "4.0.16"
1213
memmap2 = "0.2.2"
1314

1415
[[bin]]
1516
name = "10_1"
1617
path = "src/10_1/main.rs"
1718

19+
[[bin]]
20+
name = "10_2"
21+
path = "src/10_2/main.rs"
22+
1823
[[bin]]
1924
name = "10_3"
2025
path = "src/10_3/main.rs"

chapter10/src/10_2/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::{thread, time};
2+
3+
use nix::{fcntl, sys};
4+
5+
fn main() -> nix::Result<()> {
6+
let fd = fcntl::open(
7+
"Cargo.toml",
8+
fcntl::OFlag::O_RDONLY,
9+
sys::stat::Mode::empty(),
10+
)?;
11+
12+
println!("try locking...");
13+
fcntl::flock(fd, fcntl::FlockArg::LockExclusive)?;
14+
println!("locked!");
15+
16+
thread::sleep(time::Duration::from_secs(10));
17+
18+
fcntl::flock(fd, fcntl::FlockArg::Unlock)?;
19+
println!("unlock");
20+
21+
Ok(())
22+
}

0 commit comments

Comments
 (0)