Process and Exit
Process and Exit
In Rust, the std::process module provides functionality for working with the current process and spawning new processes. This challenge focuses on working with the current process - getting its ID, handling exit codes, and managing process information.
Key Concepts
Process ID
Every running process has a unique identifier (PID) assigned by the operating system. You can get the current process ID using std::process::id().
Exit Codes
When a program terminates, it returns an exit code to the operating system:
- 0 typically indicates success
- Non-zero values indicate various types of failures
The std::process::ExitCode type provides a type-safe way to represent exit codes. You can also use std::process::exit() to terminate a program immediately with a specific exit code.
Abort
In extreme cases, you can use std::process::abort() to terminate the process abnormally. This is typically used when the program is in an unrecoverable state.
Your Task
Implement the following functions to work with process information and exit codes:
-
get_process_id- Returns the current process ID as au32 -
exit_code_success- Returns anExitCoderepresenting success (0) -
exit_code_failure- Returns anExitCoderepresenting failure (1) -
exit_code_from_u8- Converts au8value to anExitCode -
is_success_code- Checks if a givenu8exit code represents success (0) -
is_failure_code- Checks if a givenu8exit code represents failure (non-zero) -
describe_exit_code- Returns a description string for common exit codes:- 0 -> "success"
- 1 -> "general error"
- 2 -> "misuse of command"
- 126 -> "command not executable"
- 127 -> "command not found"
- 128 -> "invalid exit argument"
- 130 -> "script terminated by ctrl-c"
- Any other value -> "unknown exit code: N" (where N is the code)
-
validate_exit_code- Takes anOption<u8>and returns:Ok(code)if Some and code is 0Err("process failed with code N")if Some and code is non-zeroErr("process did not return an exit code")if None
Examples
use process_exit::*;
// Get current process ID
let pid = get_process_id();
assert!(pid > 0); // PID is always positive
// Create exit codes
let success = exit_code_success();
let failure = exit_code_failure();
let custom = exit_code_from_u8(42);
// Check exit codes
assert!(is_success_code(0));
assert!(!is_success_code(1));
assert!(is_failure_code(1));
assert!(!is_failure_code(0));
// Describe exit codes
assert_eq!(describe_exit_code(0), "success");
assert_eq!(describe_exit_code(127), "command not found");
assert_eq!(describe_exit_code(42), "unknown exit code: 42");
// Validate exit codes
assert_eq!(validate_exit_code(Some(0)), Ok(0));
assert_eq!(
validate_exit_code(Some(1)),
Err("process failed with code 1".to_string())
);
assert_eq!(
validate_exit_code(None),
Err("process did not return an exit code".to_string())
);Hints
Click here to reveal hints
- Use
std::process::id()to get the current process ID ExitCode::SUCCESSandExitCode::FAILUREare predefined constants- Use
ExitCode::from(u8)to create an exit code from a number - For
describe_exit_code, use amatchexpression with the common Unix exit codes - Remember that exit code 0 is the only "success" code; all others indicate failure
Run to execute your code and see test results here.