Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active February 6, 2020 03:18
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 dulimarta/31a66cf439e27ecc679255aab07ad874 to your computer and use it in GitHub Desktop.
Save dulimarta/31a66cf439e27ecc679255aab07ad874 to your computer and use it in GitHub Desktop.
CS452 Lab05 - Sample 1 (Rust)
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