Created
December 16, 2016 20:26
-
-
Save philippkeller/38ff188ddb5aea01ce91ba5e00aefae2 to your computer and use it in GitHub Desktop.
This file contains 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
#[macro_use] | |
extern crate chan; | |
extern crate chan_signal; | |
extern crate libc; | |
use chan_signal::Signal; | |
use std::thread; | |
use libc::fork; | |
fn main() { | |
// Signal gets a value when the OS sent a INT or TERM signal. | |
let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]); | |
// When our work is complete, send a sentinel value on `sdone`. | |
let (sdone, rdone) = chan::sync(0); | |
match unsafe { fork() } { | |
0 => { | |
run(sdone); | |
}, | |
_ => { | |
// Wait for a signal or for work to be done. | |
chan_select! { | |
signal.recv() -> signal => { | |
println!("received signal: {:?}", signal) | |
}, | |
rdone.recv() => { | |
println!("Program completed normally."); | |
} | |
} | |
} | |
} | |
} | |
fn run(_sdone: chan::Sender<()>) { | |
println!("Running work for 5 seconds."); | |
println!("Can you send a signal quickly enough?"); | |
// Do some work. | |
thread::sleep_ms(5000); | |
// _sdone gets dropped which closes the channel and causes `rdone` | |
// to unblock. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment