Created
December 17, 2016 12:07
-
-
Save philippkeller/c1d9452a4aaf4c2520eb192e7ebd60d6 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
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