Function linux_unsafe::getpid

source ·
pub unsafe fn getpid() -> pid_t
Expand description

Get the process id (PID) of the current process.

Examples found in repository?
examples/getpid.rs (line 2)
1
2
3
4
5
6
7
8
9
10
11
12
13
fn main() {
    let pid = unsafe { linux_unsafe::getpid() };

    let msg = format!("{}\n", pid);
    let msg_raw = msg.as_bytes();
    let msg_ptr = msg_raw.as_ptr() as *const linux_unsafe::void;
    let msg_size = msg_raw.len() * core::mem::size_of::<u8>();

    let written = unsafe { linux_unsafe::write(1, msg_ptr, msg_size) }.unwrap();
    if written < 0 {
        panic!("failed to write");
    }
}