thread 2
parent
9727eb9c14
commit
43a670c9ce
@ -1,23 +1,27 @@
|
||||
extern crate nix;
|
||||
use nix::unistd::{fork};
|
||||
use std::{thread,time};
|
||||
use std::sync::{Mutex, Arc};
|
||||
|
||||
fn main() {
|
||||
let mut big_data: Vec<u8> = Vec::with_capacity(200000000);
|
||||
big_data.push(1);
|
||||
big_data.push(2);
|
||||
big_data.push(3);
|
||||
let big_data = Arc::new(Mutex::new(big_data));
|
||||
|
||||
//Both sides of the fork, will continue to fork
|
||||
//This is called a fork bomb
|
||||
for _ in 0..9 {
|
||||
fork().expect("fork failed");
|
||||
for _ in 0..512 {
|
||||
let big_data = Arc::clone(&big_data);
|
||||
thread::spawn(move || {
|
||||
let t = time::Duration::from_millis(1000);
|
||||
loop {
|
||||
let d = big_data.lock().unwrap();
|
||||
(*d)[2];
|
||||
thread::sleep(t);
|
||||
}
|
||||
});
|
||||
}
|
||||
//2^9 = 512
|
||||
|
||||
let t = time::Duration::from_millis(1000);
|
||||
loop {
|
||||
big_data[2];
|
||||
thread::sleep(t);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue