Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active September 23, 2020 20:12
Show Gist options
  • Save dulimarta/a63dec8bc29c7b61e17287acabc4c1ee to your computer and use it in GitHub Desktop.
Save dulimarta/a63dec8bc29c7b61e17287acabc4c1ee to your computer and use it in GitHub Desktop.
CS452 Lab03 - Sample1 (Rust)
extern crate signal_hook;
use std::error::Error;
use std::time::Duration;
use nix::unistd::{pause, sleep};
use signal_hook::{register, SIGINT};
static mut I_CAN_RUN: bool = true;
fn main() -> Result<(), Box<dyn Error>> {
unsafe {
register(SIGINT, sig_handler)?;
}
let mut extra_life = 5;
while unsafe { I_CAN_RUN} || extra_life > 0 {
println!("Waiting...");
std::thread::sleep(Duration::from_millis(300));
if unsafe {!I_CAN_RUN} {
extra_life = extra_life - 1;
}
}
// match pause() {
// Err(e) => println!("Error due to {}", e),
// _ => {}
// }
println!("Out of the loop");
Ok(())
}
fn sig_handler() {
println!("received an interrupt");
sleep(5);
unsafe { I_CAN_RUN = false; }
println!("outta here.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment