Skip to content

Instantly share code, notes, and snippets.

@philippkeller
Created December 17, 2016 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philippkeller/c1d9452a4aaf4c2520eb192e7ebd60d6 to your computer and use it in GitHub Desktop.
Save philippkeller/c1d9452a4aaf4c2520eb192e7ebd60d6 to your computer and use it in GitHub Desktop.
extern crate libc;
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex};
use libc::{fork, setbuf, c_char, c_int, STDOUT_FILENO, fdopen, usleep, FILE};
const N: usize = 10;
extern "C" {
pub fn putc(arg1: c_int, arg2: *mut FILE) -> c_int;
}
fn charatatime(out: *mut FILE, s: &str) {
for c in s.chars() {
unsafe {
putc(c as i32, out);
usleep(20);
}
}
}
fn main() {
let stdout = unsafe {
let stdout = fdopen(STDOUT_FILENO, &('w' as c_char));
setbuf(stdout, std::ptr::null_mut());
stdout
};
let data = Arc::new(Mutex::new(0));
match unsafe { fork() } {
0 => {
data.lock().unwrap();
charatatime(stdout, "child is writing...")
},
_ => {
data.lock().unwrap();
charatatime(stdout, "parent is writing...")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment