Last active
February 6, 2020 03:18
-
-
Save dulimarta/31a66cf439e27ecc679255aab07ad874 to your computer and use it in GitHub Desktop.
CS452 Lab05 - Sample 1 (Rust)
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
use core::ffi::c_void; // FFI: Foreign Function Interface | |
use libc::{c_int, shmid_ds}; | |
use libc::{shmat, shmctl, shmdt, shmget}; | |
use libc::{IPC_PRIVATE, IPC_RMID, S_IRUSR, S_IWUSR}; | |
const FOO: usize = 4096; | |
fn main() { | |
println!("Hello, world!"); | |
let shm_ptr: *mut c_void; | |
unsafe { | |
let shm_id = shmget(IPC_PRIVATE, FOO, (S_IRUSR | S_IWUSR) as c_int); | |
if shm_id < 0 { | |
panic!("I can't get no.."); | |
} | |
println!("Shared memory id {}", shm_id); | |
shm_ptr = shmat(shm_id, 0 as *const c_void, 0); | |
if shm_ptr == 0xFFFFFFFFFFFFFFFF as *mut c_void { | |
panic!("Can't attach"); | |
} | |
println!("Value a = {:x}", shm_ptr as usize); | |
println!("Value b = {:x}", shm_ptr as usize + FOO); | |
if shmdt(shm_ptr) < 0 { | |
panic!("Just can't let go") | |
} | |
if shmctl(shm_id, IPC_RMID, 0 as *mut shmid_ds) < 0 { | |
panic!("Can't deallocate"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment