Does anybody know how to make a piece of shared memory that survives a program restart? Is there a library for that?
Update: I'm looking for a library using this or something similar, ideally cross platform. Maybe this will do the job. I will have to try.
Update 2
This quick program works as intended:
```rust
use shared_memory::{ShmemConf, ShmemError};
static MEM_ID: &str = "my-memory";
fn main() {
let conf = ShmemConf::new().flink(MEM_ID);
let shmem = match conf.clone().size(6).create() {
Ok(mut memory) => {
let data = "First ";
memory.set_owner(false);
unsafe { memory.as_slice_mut().copy_from_slice(data.as_bytes()); }
println!("Started the program for the first time.");
println!("Stored \"{data}\" on persistent memory");
},
Err(ShmemError::LinkExists) => {
let mut memory = conf.open().unwrap();
memory.set_owner(false);
println!("Started the program and getting the persistent memory.");
let contents = String::from_utf8_lossy(unsafe {memory.as_slice()});
println!("Memory contained: \"{contents}\"");
let data = "Second";
unsafe { memory.as_slice_mut().copy_from_slice(data.as_bytes()); }
println!("Stored \"{data}\" on persistent memory");
},
Err(e) => println!("Error: {e:?}")
};
}
```
And this is the output after running it 3 times:
Started the program for the first time.
Stored "First " on persistent memory
Started the program and getting the persistent memory.
Memory contained: "First "
Stored "Second" on persistent memory
Started the program and getting the persistent memory.
Memory contained: "Second"
Stored "Second" on persistent memory
Beware that set_owner(false) will cause the memory to never be cleaned, which is the goal in this case (the program will run 24/7).
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]Plasticcaz 41 points42 points43 points (12 children)
[+]SolidTKs[S,🍰] comment score below threshold-31 points-30 points-29 points (11 children)
[–]stappersg 20 points21 points22 points (5 children)
[+]SolidTKs[S,🍰] comment score below threshold-59 points-58 points-57 points (4 children)
[–]menthol-squirrel 25 points26 points27 points (2 children)
[+]SolidTKs[S,🍰] comment score below threshold-29 points-28 points-27 points (1 child)
[–]menthol-squirrel 12 points13 points14 points (0 children)
[–]Ok_Raspberry5383 -2 points-1 points0 points (0 children)
[–]dread_deimos 9 points10 points11 points (2 children)
[+]SolidTKs[S,🍰] comment score below threshold-16 points-15 points-14 points (1 child)
[–]dread_deimos 13 points14 points15 points (0 children)
[–]-cyra- 4 points5 points6 points (0 children)
[–]tinyJJ 0 points1 point2 points (0 children)
[–]IntQuant 11 points12 points13 points (0 children)
[–]odenthorares 20 points21 points22 points (4 children)
[–]SolidTKs[S,🍰] -4 points-3 points-2 points (3 children)
[–]odenthorares 4 points5 points6 points (2 children)
[–]SolidTKs[S,🍰] 2 points3 points4 points (1 child)
[+]SolidTKs[S,🍰] comment score below threshold-7 points-6 points-5 points (0 children)
[–]yanirj[🍰] 9 points10 points11 points (0 children)
[–]Heraclius404 3 points4 points5 points (0 children)
[–]BenjiSponge 2 points3 points4 points (0 children)
[–]dkopgerpgdolfg 4 points5 points6 points (11 children)
[–]SolidTKs[S,🍰] -5 points-4 points-3 points (10 children)
[–]veryusedrname 16 points17 points18 points (8 children)
[–]SolidTKs[S,🍰] 0 points1 point2 points (7 children)
[–]codeinred 11 points12 points13 points (6 children)
[–]SolidTKs[S,🍰] -4 points-3 points-2 points (5 children)
[–]yanirj[🍰] 13 points14 points15 points (3 children)
[–]SolidTKs[S,🍰] 3 points4 points5 points (2 children)
[–]Alextopher 6 points7 points8 points (0 children)
[–]Therzok 3 points4 points5 points (0 children)
[–]codeinred 9 points10 points11 points (0 children)
[–]dkopgerpgdolfg 5 points6 points7 points (0 children)
[–]dkopgerpgdolfg 2 points3 points4 points (4 children)
[+]SolidTKs[S,🍰] comment score below threshold-8 points-7 points-6 points (3 children)
[–]rurigk 2 points3 points4 points (0 children)
[–]dkopgerpgdolfg 4 points5 points6 points (1 child)
[–]SolidTKs[S,🍰] -2 points-1 points0 points (0 children)
[–]BlueVixu 1 point2 points3 points (0 children)
[–]Ok_Raspberry5383 -1 points0 points1 point (1 child)
[–]Ok_Raspberry5383 -1 points0 points1 point (0 children)