Skip to content

Commit bea6136

Browse files
authored
Rollup merge of rust-lang#45059 - tmccombs:pid, r=alexcrichton
Add current_pid function Fixes rust-lang#44971
2 parents 2f5ae25 + 29b319b commit bea6136

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/libstd/process.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,25 @@ pub fn abort() -> ! {
13101310
unsafe { ::sys::abort_internal() };
13111311
}
13121312

1313+
/// Returns the OS-assigned process identifier associated with this process.
1314+
///
1315+
/// # Examples
1316+
///
1317+
/// Basic usage:
1318+
///
1319+
/// ```no_run
1320+
/// #![feature(getpid)]
1321+
/// use std::process;
1322+
///
1323+
/// println!("My pid is {}", process::id());
1324+
/// ```
1325+
///
1326+
///
1327+
#[unstable(feature = "getpid", issue = "44971", reason = "recently added")]
1328+
pub fn id() -> u32 {
1329+
::sys::os::getpid()
1330+
}
1331+
13131332
#[cfg(all(test, not(target_os = "emscripten")))]
13141333
mod tests {
13151334
use io::prelude::*;

src/libstd/sys/redox/os.rs

+4
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,7 @@ pub fn exit(code: i32) -> ! {
209209
let _ = syscall::exit(code as usize);
210210
unreachable!();
211211
}
212+
213+
pub fn getpid() -> u32 {
214+
syscall::getpid().unwrap() as u32
215+
}

src/libstd/sys/unix/os.rs

+4
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,7 @@ pub fn home_dir() -> Option<PathBuf> {
511511
pub fn exit(code: i32) -> ! {
512512
unsafe { libc::exit(code as c_int) }
513513
}
514+
515+
pub fn getpid() -> u32 {
516+
unsafe { libc::getpid() as u32 }
517+
}

src/libstd/sys/windows/os.rs

+4
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ pub fn exit(code: i32) -> ! {
318318
unsafe { c::ExitProcess(code as c::UINT) }
319319
}
320320

321+
pub fn getpid() -> u32 {
322+
unsafe { c::GetCurrentProcessId() as u32 }
323+
}
324+
321325
#[cfg(test)]
322326
mod tests {
323327
use io::Error;

0 commit comments

Comments
 (0)