From 53a30721cdbde67def6e4c0d595c8b8acd945e66 Mon Sep 17 00:00:00 2001 From: Shinichi Morimoto Date: Sat, 29 May 2021 18:24:37 +0900 Subject: [PATCH 01/12] add 4_5 --- chapter4/Cargo.toml | 4 ++++ chapter4/src/4_5/main.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 chapter4/src/4_5/main.rs diff --git a/chapter4/Cargo.toml b/chapter4/Cargo.toml index 7899466..ff046f8 100644 --- a/chapter4/Cargo.toml +++ b/chapter4/Cargo.toml @@ -26,6 +26,10 @@ path = "src/4_2_5/main.rs" name = "4_3_1" path = "src/4_3_1/main.rs" +[[bin]] +name = "4_5" +path = "src/4_5/main.rs" + [dependencies] lib = { path = "../lib" } tokio = { version = "1.5.0", features = ["full"] } diff --git a/chapter4/src/4_5/main.rs b/chapter4/src/4_5/main.rs new file mode 100644 index 0000000..304250a --- /dev/null +++ b/chapter4/src/4_5/main.rs @@ -0,0 +1,24 @@ +use tokio::time::{self, Duration}; + +// tokio::time::sleepを使って、決まった時間を図るタイマーを作る +#[tokio::main] +async fn main() { + let wait_sec = 5; + let (_, mut wait) = tokio::sync::oneshot::channel::<()>(); + let (sleep_tx, mut sleep_rx) = tokio::sync::oneshot::channel(); + + tokio::spawn(async move { + time::sleep(Duration::from_secs(wait_sec)).await; + sleep_tx.send("done").unwrap(); + }); + + loop { + tokio::select! { + _ = &mut sleep_rx => { + println!("{} secondes elapsed", wait_sec); + break; + } + _ = &mut wait => {} + } + } +} From 2bd85dd93f01178486c630236a72b1c871255fb9 Mon Sep 17 00:00:00 2001 From: Shinichi Morimoto Date: Sun, 30 May 2021 01:36:08 +0900 Subject: [PATCH 02/12] delete channel for sleep --- chapter4/src/4_5/main.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/chapter4/src/4_5/main.rs b/chapter4/src/4_5/main.rs index 304250a..f249f91 100644 --- a/chapter4/src/4_5/main.rs +++ b/chapter4/src/4_5/main.rs @@ -5,16 +5,12 @@ use tokio::time::{self, Duration}; async fn main() { let wait_sec = 5; let (_, mut wait) = tokio::sync::oneshot::channel::<()>(); - let (sleep_tx, mut sleep_rx) = tokio::sync::oneshot::channel(); - - tokio::spawn(async move { - time::sleep(Duration::from_secs(wait_sec)).await; - sleep_tx.send("done").unwrap(); - }); + let sleep = time::sleep(Duration::from_secs(wait_sec)); + tokio::pin!(sleep); loop { tokio::select! { - _ = &mut sleep_rx => { + _ = &mut sleep => { println!("{} secondes elapsed", wait_sec); break; } From fd1a639fe70b965f4e29c737eaaa6a2686e92a55 Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Mon, 31 May 2021 15:40:38 +0900 Subject: [PATCH 03/12] add chapter6 directories --- chapter6/Cargo.toml | 22 +++++++++++++++++++++- chapter6/src/6_5/main.rs | 3 +++ chapter6/src/6_6/main.rs | 3 +++ chapter6/src/6_7/main.rs | 3 +++ chapter6/src/6_8/main.rs | 3 +++ chapter6/src/6_9/main.rs | 3 +++ chapter6/src/main.rs | 3 --- 7 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 chapter6/src/6_5/main.rs create mode 100644 chapter6/src/6_6/main.rs create mode 100644 chapter6/src/6_7/main.rs create mode 100644 chapter6/src/6_8/main.rs create mode 100644 chapter6/src/6_9/main.rs delete mode 100644 chapter6/src/main.rs diff --git a/chapter6/Cargo.toml b/chapter6/Cargo.toml index 3eb34de..65601c8 100644 --- a/chapter6/Cargo.toml +++ b/chapter6/Cargo.toml @@ -7,4 +7,24 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lib = { path = "../lib" } \ No newline at end of file +lib = { path = "../lib" } + +[[bin]] +name = "6_5" +path = "src/6_5/main.rs" + +[[bin]] +name = "6_6" +path = "src/6_6/main.rs" + +[[bin]] +name = "6_7" +path = "src/6_7/main.rs" + +[[bin]] +name = "6_8" +path = "src/6_8/main.rs" + +[[bin]] +name = "6_9" +path = "src/6_9/main.rs" \ No newline at end of file diff --git a/chapter6/src/6_5/main.rs b/chapter6/src/6_5/main.rs new file mode 100644 index 0000000..0672e51 --- /dev/null +++ b/chapter6/src/6_5/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, World!"); +} diff --git a/chapter6/src/6_6/main.rs b/chapter6/src/6_6/main.rs new file mode 100644 index 0000000..0672e51 --- /dev/null +++ b/chapter6/src/6_6/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, World!"); +} diff --git a/chapter6/src/6_7/main.rs b/chapter6/src/6_7/main.rs new file mode 100644 index 0000000..0672e51 --- /dev/null +++ b/chapter6/src/6_7/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, World!"); +} diff --git a/chapter6/src/6_8/main.rs b/chapter6/src/6_8/main.rs new file mode 100644 index 0000000..0672e51 --- /dev/null +++ b/chapter6/src/6_8/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, World!"); +} diff --git a/chapter6/src/6_9/main.rs b/chapter6/src/6_9/main.rs new file mode 100644 index 0000000..0672e51 --- /dev/null +++ b/chapter6/src/6_9/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, World!"); +} diff --git a/chapter6/src/main.rs b/chapter6/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/chapter6/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} From f07aee098353868ced595c212b8d82e08ef46151 Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sat, 5 Jun 2021 16:16:14 +0900 Subject: [PATCH 04/12] add 11_1_2 --- Cargo.lock | 29 ++++++++++++++++++++++++++--- chapter11/Cargo.toml | 7 ++++++- chapter11/src/11_1_2/main.rs | 9 +++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 chapter11/src/11_1_2/main.rs diff --git a/Cargo.lock b/Cargo.lock index 646e4cd..fbac903 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,7 +104,7 @@ version = "0.1.0" dependencies = [ "lib", "memmap2", - "nix", + "nix 0.20.0", "notify", ] @@ -113,6 +113,7 @@ name = "chapter11" version = "0.1.0" dependencies = [ "lib", + "nix 0.21.0", ] [[package]] @@ -408,9 +409,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.94" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e" +checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" [[package]] name = "lock_api" @@ -445,6 +446,15 @@ dependencies = [ "libc", ] +[[package]] +name = "memoffset" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" +dependencies = [ + "autocfg", +] + [[package]] name = "miniz_oxide" version = "0.4.4" @@ -543,6 +553,19 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c3728fec49d363a50a8828a190b379a446cc5cf085c06259bbbeb34447e4ec7" +dependencies = [ + "bitflags", + "cc", + "cfg-if 1.0.0", + "libc", + "memoffset", +] + [[package]] name = "notify" version = "4.0.17" diff --git a/chapter11/Cargo.toml b/chapter11/Cargo.toml index 086d198..b262381 100644 --- a/chapter11/Cargo.toml +++ b/chapter11/Cargo.toml @@ -7,4 +7,9 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lib = { path = "../lib" } \ No newline at end of file +lib = { path = "../lib" } +nix = "0.21.0" + +[[bin]] +name = "11_1_2" +path = "src/11_1_2/main.rs" diff --git a/chapter11/src/11_1_2/main.rs b/chapter11/src/11_1_2/main.rs new file mode 100644 index 0000000..cab3a97 --- /dev/null +++ b/chapter11/src/11_1_2/main.rs @@ -0,0 +1,9 @@ +use nix::unistd::{getpid, getppid}; + +fn main() { + // nix を使用してもよい。 + println!("プロセス ID: {}", getpid()); + // あるいは、Rust の std::process::id を使用しても取得できる。 + // println!("プロセス ID: {}", std::process::id()); + println!("親プロセスID: {}", getppid()); +} From bf336a5296680086a451a41c6dbc0069b595254f Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sat, 5 Jun 2021 16:27:59 +0900 Subject: [PATCH 05/12] set up directories for chapter 11 --- chapter11/Cargo.toml | 16 ++++++++++++++++ chapter11/src/11_1_3/main.rs | 3 +++ chapter11/src/11_1_4/main.rs | 3 +++ chapter11/src/11_1_5/main.rs | 3 +++ chapter11/src/11_1_6/main.rs | 3 +++ chapter11/src/main.rs | 3 --- 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 chapter11/src/11_1_3/main.rs create mode 100644 chapter11/src/11_1_4/main.rs create mode 100644 chapter11/src/11_1_5/main.rs create mode 100644 chapter11/src/11_1_6/main.rs delete mode 100644 chapter11/src/main.rs diff --git a/chapter11/Cargo.toml b/chapter11/Cargo.toml index b262381..70ab15e 100644 --- a/chapter11/Cargo.toml +++ b/chapter11/Cargo.toml @@ -13,3 +13,19 @@ nix = "0.21.0" [[bin]] name = "11_1_2" path = "src/11_1_2/main.rs" + +[[bin]] +name = "11_1_3" +path = "src/11_1_3/main.rs" + +[[bin]] +name = "11_1_4" +path = "src/11_1_4/main.rs" + +[[bin]] +name = "11_1_5" +path = "src/11_1_5/main.rs" + +[[bin]] +name = "11_1_6" +path = "src/11_1_6/main.rs" \ No newline at end of file diff --git a/chapter11/src/11_1_3/main.rs b/chapter11/src/11_1_3/main.rs new file mode 100644 index 0000000..c9f29e8 --- /dev/null +++ b/chapter11/src/11_1_3/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("11_1_3"); +} diff --git a/chapter11/src/11_1_4/main.rs b/chapter11/src/11_1_4/main.rs new file mode 100644 index 0000000..1b7cdd6 --- /dev/null +++ b/chapter11/src/11_1_4/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("11_1_4"); +} diff --git a/chapter11/src/11_1_5/main.rs b/chapter11/src/11_1_5/main.rs new file mode 100644 index 0000000..1b78e65 --- /dev/null +++ b/chapter11/src/11_1_5/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("11_1_5"); +} diff --git a/chapter11/src/11_1_6/main.rs b/chapter11/src/11_1_6/main.rs new file mode 100644 index 0000000..ad2be8e --- /dev/null +++ b/chapter11/src/11_1_6/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("11_1_6"); +} diff --git a/chapter11/src/main.rs b/chapter11/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/chapter11/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} From cd45c9fedeee4be69d2e7a416ab23ff0fab4188c Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sat, 5 Jun 2021 16:41:33 +0900 Subject: [PATCH 06/12] add 11_1_3 & 11_1_4 --- chapter11/src/11_1_3/main.rs | 5 ++++- chapter11/src/11_1_4/main.rs | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/chapter11/src/11_1_3/main.rs b/chapter11/src/11_1_3/main.rs index c9f29e8..027fc56 100644 --- a/chapter11/src/11_1_3/main.rs +++ b/chapter11/src/11_1_3/main.rs @@ -1,3 +1,6 @@ +use nix::unistd::{getpgrp, getpid, getsid}; + fn main() { - println!("11_1_3"); + let sid = getsid(Some(getpid())).unwrap(); + println!("グループID: {}, セッションID: {}", getpgrp(), sid); } diff --git a/chapter11/src/11_1_4/main.rs b/chapter11/src/11_1_4/main.rs index 1b7cdd6..715a5dd 100644 --- a/chapter11/src/11_1_4/main.rs +++ b/chapter11/src/11_1_4/main.rs @@ -1,3 +1,11 @@ +use nix::unistd::getgroups; +use nix::unistd::{getgid, getuid}; + fn main() { - println!("11_1_4"); + println!("ユーザーID: {}", getuid()); + println!("グループID: {}", getgid()); + + // nix クレートにおける getgroups は Apple のプラットフォームでは実行できない。 + let groups = getgroups().unwrap(); + println!("サブグループID: {:?}", groups); } From 558ee887b8b82508e1d9ef9787bd61155eb6b12a Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sat, 5 Jun 2021 16:46:35 +0900 Subject: [PATCH 07/12] add annotation not compiling on apple platform --- chapter11/src/11_1_4/main.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/chapter11/src/11_1_4/main.rs b/chapter11/src/11_1_4/main.rs index 715a5dd..d48ff7c 100644 --- a/chapter11/src/11_1_4/main.rs +++ b/chapter11/src/11_1_4/main.rs @@ -1,3 +1,4 @@ +#[cfg(not(any(target_os = "ios", target_os = "macos")))] use nix::unistd::getgroups; use nix::unistd::{getgid, getuid}; @@ -6,6 +7,12 @@ fn main() { println!("グループID: {}", getgid()); // nix クレートにおける getgroups は Apple のプラットフォームでは実行できない。 + #[cfg(not(any(target_os = "ios", target_os = "macos")))] + get_groups(); +} + +#[cfg(not(any(target_os = "ios", target_os = "macos")))] +fn get_groups() { let groups = getgroups().unwrap(); println!("サブグループID: {:?}", groups); } From ec65d42b65bf6a73fa8069b33dfd7a722c36f797 Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sat, 5 Jun 2021 16:48:21 +0900 Subject: [PATCH 08/12] add 11_1_5 --- chapter11/src/11_1_5/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/chapter11/src/11_1_5/main.rs b/chapter11/src/11_1_5/main.rs index 1b78e65..e8982c2 100644 --- a/chapter11/src/11_1_5/main.rs +++ b/chapter11/src/11_1_5/main.rs @@ -1,3 +1,8 @@ +use nix::unistd::{getegid, geteuid, getgid, getuid}; + fn main() { - println!("11_1_5"); + println!("ユーザー ID: {}", getuid()); + println!("グループID: {}", getgid()); + println!("実効ユーザーID: {}", geteuid()); + println!("実効グループID: {}", getegid()); } From b14306f64f7c9333fcb89ed371fab84775655f74 Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sat, 5 Jun 2021 16:53:10 +0900 Subject: [PATCH 09/12] add 11_1_6 --- chapter11/src/11_1_6/main.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/chapter11/src/11_1_6/main.rs b/chapter11/src/11_1_6/main.rs index ad2be8e..7d3e855 100644 --- a/chapter11/src/11_1_6/main.rs +++ b/chapter11/src/11_1_6/main.rs @@ -1,3 +1,9 @@ +use nix::unistd::getcwd; +use std::env::current_dir; + fn main() { - println!("11_1_6"); + // nix クレートを使用する場合は getcwd が使える。 + println!("{}", getcwd().unwrap().to_str().unwrap()); + // あるいは、Rust の std::env にある current_dir を使ってもよい。 + println!("{}", current_dir().unwrap().to_str().unwrap()); } From c1521da0dcb56b9e6e0df1c2ca06e2d64ed3c14a Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sat, 5 Jun 2021 17:17:56 +0900 Subject: [PATCH 10/12] add 11_2 --- chapter11/Cargo.toml | 10 +++++++++- chapter11/src/11_2_1/main.rs | 7 +++++++ chapter11/src/11_2_3/main.rs | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 chapter11/src/11_2_1/main.rs create mode 100644 chapter11/src/11_2_3/main.rs diff --git a/chapter11/Cargo.toml b/chapter11/Cargo.toml index 70ab15e..d561116 100644 --- a/chapter11/Cargo.toml +++ b/chapter11/Cargo.toml @@ -28,4 +28,12 @@ path = "src/11_1_5/main.rs" [[bin]] name = "11_1_6" -path = "src/11_1_6/main.rs" \ No newline at end of file +path = "src/11_1_6/main.rs" + +[[bin]] +name = "11_2_1" +path = "src/11_2_1/main.rs" + +[[bin]] +name = "11_2_3" +path = "src/11_2_3/main.rs" \ No newline at end of file diff --git a/chapter11/src/11_2_1/main.rs b/chapter11/src/11_2_1/main.rs new file mode 100644 index 0000000..a34b3b2 --- /dev/null +++ b/chapter11/src/11_2_1/main.rs @@ -0,0 +1,7 @@ +fn main() { + let envs = std::env::vars(); + for (key, value) in envs { + println!("{}={}", key, value); + } + // os.ExpandEnv 関数は Rust には存在しない。 +} diff --git a/chapter11/src/11_2_3/main.rs b/chapter11/src/11_2_3/main.rs new file mode 100644 index 0000000..081ca86 --- /dev/null +++ b/chapter11/src/11_2_3/main.rs @@ -0,0 +1,3 @@ +fn main() { + std::process::exit(1); +} From e5d66a5ebd27d6818acedc849bd36409210f2512 Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sun, 6 Jun 2021 00:17:33 +0900 Subject: [PATCH 11/12] add 11_3 --- Cargo.lock | 110 +++++++++++++++++++++++++++++++++++++ chapter11/Cargo.toml | 7 ++- chapter11/src/11_3/main.rs | 18 ++++++ 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 chapter11/src/11_3/main.rs diff --git a/Cargo.lock b/Cargo.lock index fbac903..d25923a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -114,6 +114,7 @@ version = "0.1.0" dependencies = [ "lib", "nix 0.21.0", + "sysinfo", ] [[package]] @@ -238,6 +239,12 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "core-foundation-sys" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" + [[package]] name = "crc32fast" version = "1.2.1" @@ -247,6 +254,62 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd" +dependencies = [ + "cfg-if 1.0.0", + "crossbeam-utils", + "lazy_static", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db" +dependencies = [ + "cfg-if 1.0.0", + "lazy_static", +] + +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + +[[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + [[package]] name = "filetime" version = "0.2.14" @@ -392,6 +455,12 @@ dependencies = [ "winapi-build", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "lazycell" version = "1.3.0" @@ -729,6 +798,31 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rayon" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" +dependencies = [ + "autocfg", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" +dependencies = [ + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.2.8" @@ -825,6 +919,22 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "sysinfo" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3899fa43ce1ab71db79fda2a3fdb119a07e25a006677d46033c9d9051c82f019" +dependencies = [ + "cfg-if 1.0.0", + "core-foundation-sys", + "doc-comment", + "libc", + "ntapi", + "once_cell", + "rayon", + "winapi 0.3.9", +] + [[package]] name = "thiserror" version = "1.0.24" diff --git a/chapter11/Cargo.toml b/chapter11/Cargo.toml index d561116..a540383 100644 --- a/chapter11/Cargo.toml +++ b/chapter11/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] lib = { path = "../lib" } nix = "0.21.0" +sysinfo = "0.18.1" [[bin]] name = "11_1_2" @@ -36,4 +37,8 @@ path = "src/11_2_1/main.rs" [[bin]] name = "11_2_3" -path = "src/11_2_3/main.rs" \ No newline at end of file +path = "src/11_2_3/main.rs" + +[[bin]] +name = "11_3" +path = "src/11_3/main.rs" diff --git a/chapter11/src/11_3/main.rs b/chapter11/src/11_3/main.rs new file mode 100644 index 0000000..2d9e8ca --- /dev/null +++ b/chapter11/src/11_3/main.rs @@ -0,0 +1,18 @@ +use nix::libc::getppid; +// gopsutil の Rust バージョンとして rust-psutil というクレートを使用できそうだったが、 +// 実装がかなり不完全そうだった。 +// そこで、sysinfo というクレートを使っている。 +use sysinfo::{ProcessExt, System, SystemExt}; + +fn main() { + // get_process に libc の pid_t を要求するので、それを取得できるように nix::libc 経由で getppid を使っている。 + let pid = unsafe { getppid() }; + if let Some(process) = System::new_all().get_process(pid) { + println!( + "parent pid: {}, name: {}, cmd: {:?}", + process.pid(), + process.name(), + process.cmd() + ) + } +} From 80285913c2d8e6fd8941418ca9538f7e95c45cca Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sun, 6 Jun 2021 00:51:59 +0900 Subject: [PATCH 12/12] add sample program for 11_8 --- chapter11/Cargo.toml | 4 ++++ chapter11/src/11_8/main.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 chapter11/src/11_8/main.rs diff --git a/chapter11/Cargo.toml b/chapter11/Cargo.toml index a540383..bc05717 100644 --- a/chapter11/Cargo.toml +++ b/chapter11/Cargo.toml @@ -42,3 +42,7 @@ path = "src/11_2_3/main.rs" [[bin]] name = "11_3" path = "src/11_3/main.rs" + +[[bin]] +name = "11_8" +path = "src/11_8/main.rs" \ No newline at end of file diff --git a/chapter11/src/11_8/main.rs b/chapter11/src/11_8/main.rs new file mode 100644 index 0000000..20f9bd1 --- /dev/null +++ b/chapter11/src/11_8/main.rs @@ -0,0 +1,35 @@ +use nix::sys::wait::*; +use nix::unistd::{execve, fork, ForkResult}; +use std::env; +use std::ffi::CString; + +// Go のプログラミングでは触れることのない世界として言及されているが、Rust では触れることができる。 +// 本書の中で fork(2) と execve(3) が言及されていたので、それを使用したサンプルプログラムを掲載しておく。 +fn main() { + // fork + match unsafe { fork() }.expect("fork failed") { + ForkResult::Parent { child } => { + // waitpid + match waitpid(child, None).expect("wait_pid failed") { + WaitStatus::Exited(pid, status) => { + println!("exit!: pid={:?}, status={:?}", pid, status) + } + WaitStatus::Signaled(pid, status, _) => { + println!("signal!: pid={:?}, status={:?}", pid, status) + } + _ => println!("abnormal exit!"), + } + } + ForkResult::Child => { + // 引数の値を取得する。 + let args: Vec = env::args().collect(); + let dir = CString::new(args[1].to_string()).unwrap(); + let arg = CString::new(args[2].to_string()).unwrap(); + // env は仮で入れておく。 + let env = CString::new("ENV=prd".to_string()).unwrap(); + + // execv + execve(&dir, &[dir.clone(), arg], &[env]).expect("execution failed."); + } + } +}