Skip to content

Commit f73e772

Browse files
committed
10.2: nix::fcntl::flock() を使って Cargo.toml に排他ロックを掛け、10秒sleepするプログラム
1 parent 73f72cc commit f73e772

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

Cargo.lock

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter10/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,9 @@ edition = "2018"
77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
lib = { path = "../lib" }
10+
lib = { path = "../lib" }
11+
nix = "0.20.0"
12+
13+
[[bin]]
14+
name = "10_2"
15+
path = "src/10_2/main.rs"

chapter10/src/10_2/main.rs

+22
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+
}

chapter10/src/main.rs

-3
This file was deleted.

0 commit comments

Comments
 (0)