Skip to content
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

Merged
merged 3 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

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.

///
/// println!("My pid is {}", current_pid());
/// ```
///
///
#[unstable(feature = "getpid", issue = "44971", reason = "recently added")]
pub fn current_pid() -> u32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i64 or u64 would be more prudent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@tmccombs tmccombs Oct 7, 2017

Choose a reason for hiding this comment

The 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::*;
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pid_t is a usize on redox, so this may lose bits.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Redox really allow PIDs greater than 4 billion-ish though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it does, we'll need to reconsider the current Child::id method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it a u32 to be consistent with Child::id.

}
4 changes: 4 additions & 0 deletions src/libstd/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pid_t is technically signed by standard, defined as i32 on all of libc's unix platforms.

Copy link
Contributor

@clarfonthey clarfonthey Oct 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but getpid() is guaranteed to return a non-negative number, and so u32 is valid here. The signed-ness represents process groups, not IDs.

}
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