Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created July 17, 2020 14:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lovasoa/412a76e9dd6e5a4af94fad55ee1a6857 to your computer and use it in GitHub Desktop.
A process that repeatedly executes itself
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