diff --git a/src/libstd/process.rs b/src/libstd/process.rs index 1869ad3ed707a..dee01df651d11 100644 --- a/src/libstd/process.rs +++ b/src/libstd/process.rs @@ -1167,6 +1167,25 @@ pub fn abort() -> ! { unsafe { ::sys::abort_internal() }; } +/// Returns the OS-assigned process identifier associated with this process. +/// +/// # Examples +/// +/// Basic usage: +/// +/// ```no_run +/// #![feature(getpid)] +/// use std::process; +/// +/// println!("My pid is {}", process::id()); +/// ``` +/// +/// +#[unstable(feature = "getpid", issue = "44971", reason = "recently added")] +pub fn id() -> u32 { + ::sys::os::getpid() +} + #[cfg(all(test, not(target_os = "emscripten")))] mod tests { use io::prelude::*; diff --git a/src/libstd/sys/redox/os.rs b/src/libstd/sys/redox/os.rs index efddd5f029484..c27e2ee172c6b 100644 --- a/src/libstd/sys/redox/os.rs +++ b/src/libstd/sys/redox/os.rs @@ -209,3 +209,7 @@ pub fn exit(code: i32) -> ! { let _ = syscall::exit(code as usize); unreachable!(); } + +pub fn getpid() -> u32 { + syscall::getpid().unwrap() as u32 +} diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 5ef98d247105e..132f59b999d4f 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -513,3 +513,7 @@ pub fn home_dir() -> Option { pub fn exit(code: i32) -> ! { unsafe { libc::exit(code as c_int) } } + +pub fn getpid() -> u32 { + unsafe { libc::getpid() as u32 } +} diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index a51b458451e86..b94482435597e 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -318,6 +318,10 @@ pub fn exit(code: i32) -> ! { unsafe { c::ExitProcess(code as c::UINT) } } +pub fn getpid() -> u32 { + unsafe { c::GetCurrentProcessId() as u32 } +} + #[cfg(test)] mod tests { use io::Error;