-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add current_pid function #45059
Add current_pid function #45059
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1167,6 +1167,24 @@ pub fn abort() -> ! { | |
unsafe { ::sys::abort_internal() }; | ||
} | ||
|
||
/// Returns the OS-assigned process identifier associated with this process. | ||
/// | ||
/// # Examples | ||
/// | ||
/// Basic usage: | ||
/// | ||
/// ```no_run | ||
/// use std::process:current_pid; | ||
/// | ||
/// println!("My pid is {}", current_pid()); | ||
/// ``` | ||
/// | ||
/// | ||
#[unstable(feature = "getpid", issue = "44971", reason = "recently added")] | ||
pub fn current_pid() -> u32 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already use u32 elsewhere: https://doc.rust-lang.org/std/process/struct.Child.html#method.id There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that's why I chose u32. Although I think usize or u64 would probably make more sense for both. |
||
::sys::os::getpid() | ||
} | ||
|
||
#[cfg(all(test, not(target_os = "emscripten")))] | ||
mod tests { | ||
use io::prelude::*; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does Redox really allow PIDs greater than 4 billion-ish though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it does, we'll need to reconsider the current There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made it a u32 to be consistent with |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -513,3 +513,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 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing a colon in
process::current_pid
.