Skip to content

Commit

Permalink
Rollup merge of rust-lang#45059 - tmccombs:pid, r=alexcrichton
Browse files Browse the repository at this point in the history
Add current_pid function

Fixes rust-lang#44971
  • Loading branch information
kennytm committed Oct 25, 2017
2 parents 2f5ae25 + 29b319b commit bea6136
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,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::*;
Expand Down
4 changes: 4 additions & 0 deletions src/libstd/sys/redox/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,7 @@ pub fn home_dir() -> Option<PathBuf> {
pub fn exit(code: i32) -> ! {
unsafe { libc::exit(code as c_int) }
}

pub fn getpid() -> u32 {
unsafe { libc::getpid() as u32 }
}
4 changes: 4 additions & 0 deletions src/libstd/sys/windows/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit bea6136

Please sign in to comment.