Rust examples
Exemplier (Rust version)¶
This subfolder contains the Rust version of the examples used in the operating systems course.
The examples are organized as a Cargo workspace (nix crate 0.30): cargo build at the root of
exemplier-rust/ builds them all; cargo run -p process_05 runs one of them (or cargo run
in its folder). Each example corresponds to the C file with the same name in ../exemplier/.
| Program | C equivalent | Description |
|---|---|---|
1_process/process_01 |
process_01.c |
fork(): parent and child each run their own part, the parent waits with waitpid() |
1_process/process_02 |
process_02.c |
After fork(), each one modifies its own copy of a variable |
1_process/process_03 |
process_03.c |
Parent and child blocked by sleep() |
1_process/process_04a |
process_04a.c |
The parent never waits: the child becomes a zombie (state Z in ps); the parent sleeps with pause() |
1_process/process_04b |
process_04b.c |
The parent dies before the child: the child becomes an orphan (getppid() changes) |
1_process/process_05 |
process_05.c |
The parent waits for the child and decodes its status: exit code or signal (WaitStatus::Exited / Signaled) |
1_process/process_06a |
process_06a.c |
The child is replaced by ls -al (CommandExt::exec()) |
1_process/process_06b |
process_06b.c |
The child is replaced by gnuplot, which plots command.gp (run it in this folder) |
1_process/process_07 |
process_07.c |
Process groups: setpgid(0, 0) in the child and the parent, then kill(-pgid, SIGTERM) |
3_files/file_copy |
file_copy.c |
Copies keyboard input to file.out with read()/write(), partial writes handled |
3_files/fs_block |
fs_block.c |
statvfs(): block size and number of blocks, counted in f_frsize |
Using fork() is considered unsafe in Rust. Each program includes a comment (// SAFETY:) explaining why the call is safe in this context.
These code examples are provided under the Apache 2.0 License.
Sources¶
1_process/process_01/src/main.rs
1_process/process_02/src/main.rs
1_process/process_03/src/main.rs
1_process/process_04a/src/main.rs
1_process/process_04b/src/main.rs
1_process/process_05/src/main.rs
1_process/process_06a/src/main.rs
1_process/process_06b/src/main.rs
1_process/process_07/src/main.rs
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |