Skip to content

Commit 6ff6b93

Browse files
committed
Add current_pid function
Fixes #44971
1 parent a4af930 commit 6ff6b93

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

src/libstd/process.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,24 @@ pub fn abort() -> ! {
11671167
unsafe { ::sys::abort_internal() };
11681168
}
11691169

1170+
/// Returns the OS-assigned process identifier associated with this process.
1171+
///
1172+
/// # Examples
1173+
///
1174+
/// Basic usage:
1175+
///
1176+
/// ```no_run
1177+
/// use std::process:current_pid;
1178+
///
1179+
/// println!("My pid is {}", current_pid());
1180+
/// ```
1181+
///
1182+
///
1183+
#[unstable(feature = "getpid", issue = "44971", reason = "recently added")]
1184+
pub fn current_pid() -> u32 {
1185+
::sys::os::getpid()
1186+
}
1187+
11701188
#[cfg(all(test, not(target_os = "emscripten")))]
11711189
mod tests {
11721190
use io::prelude::*;

src/libstd/sys/redox/os.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,7 @@ pub fn home_dir() -> Option<PathBuf> {
513513
pub fn exit(code: i32) -> ! {
514514
unsafe { libc::exit(code as c_int) }
515515
}
516+
517+
pub fn getpid() -> u32 {
518+
unsafe { libc::getpid() as u32 }
519+
}

src/libstd/sys/windows/os.rs

Lines changed: 4 additions & 0 deletions
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)