Created
July 17, 2020 14:46
-
-
Save lovasoa/412a76e9dd6e5a4af94fad55ee1a6857 to your computer and use it in GitHub Desktop.
A process that repeatedly executes itself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::process::Command; | |
use std::env; | |
use std::process; | |
use std::time::SystemTime; | |
use crossbeam; // 0.7.3; | |
pub fn main() { | |
let myself = env::current_exe().expect("no current exe"); | |
let n: u64 = env::args() | |
.enumerate() | |
.find(|&(i, _)| i == 1) | |
.and_then(|(_, s)| s.parse().ok()) | |
.unwrap_or(0); | |
crossbeam::scope(|s| { | |
loop{ | |
s.spawn( |_| { | |
loop{ | |
let res = Command::new(&myself) | |
.args( &[ (n+1).to_string() ] ) | |
.spawn() | |
.expect("failed to execute process"); | |
println!("{}spawned process {} from {} (generation {}) (time: {:?})", | |
Spaces(n), | |
res.id(), | |
process::id(), | |
n, | |
SystemTime::now() | |
); | |
} | |
}); | |
} | |
}).expect("no scope"); | |
} | |
struct Spaces(u64); | |
impl std::fmt::Display for Spaces { | |
fn fmt(&self, f:&mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
for _ in 0..self.0 { | |
f.write_str(" ")? | |
} | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment