### Get Process Affinity Mask (Rust) Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves the process affinity mask and system affinity mask for the current process. The process mask specifies allowed processors, while the system mask shows available processors. It also demonstrates counting available CPUs. ```rust use win32job::utils::{get_current_process, get_process_affinity_mask}; fn main() -> Result<(), std::io::Error> { let handle = get_current_process(); let (process_affinity, system_affinity) = get_process_affinity_mask(handle)?; println!("Process affinity mask: {:b}", process_affinity); println!("System affinity mask: {:b}", system_affinity); // Count available CPUs let cpu_count = system_affinity.count_ones(); println!("Available CPUs: {}", cpu_count); Ok(()) } ``` -------------------------------- ### Get Process Memory Info (Rust) Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves detailed memory usage statistics for the current process, including working set size, peak working set, page fault count, page file usage, and private usage. This function requires the `win32job` crate. ```rust use win32job::utils::{get_current_process, get_process_memory_info}; fn main() -> Result<(), std::io::Error> { let handle = get_current_process(); let memory_info = get_process_memory_info(handle)?; println!("Working set size: {} bytes", memory_info.working_set_size); println!("Peak working set: {} bytes", memory_info.peak_working_set_size); println!("Page fault count: {}", memory_info.page_fault_count); println!("Pagefile usage: {} bytes", memory_info.pagefile_usage); println!("Private usage: {} bytes", memory_info.private_usage); Ok(()) } ``` -------------------------------- ### GET utils::get_process_affinity_mask Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves the processor affinity masks for a process and the system. ```APIDOC ## GET utils::get_process_affinity_mask ### Description Retrieves both the process affinity mask and system affinity mask. The process mask indicates which processors the process can run on, and the system mask indicates which processors are available. ### Method GET ### Endpoint utils::get_process_affinity_mask(handle: HANDLE) ### Parameters #### Path Parameters - **handle** (HANDLE) - Required - The Windows process handle to query. ### Response #### Success Response (200) - **process_affinity** (usize) - Bitmask of processors the process is allowed to run on. - **system_affinity** (usize) - Bitmask of processors available on the system. ``` -------------------------------- ### GET utils::get_process_memory_info Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves detailed memory usage statistics for a specified Windows process handle. ```APIDOC ## GET utils::get_process_memory_info ### Description Retrieves detailed memory usage information for a process including working set size, page file usage, and pool usage. ### Method GET ### Endpoint utils::get_process_memory_info(handle: HANDLE) ### Parameters #### Path Parameters - **handle** (HANDLE) - Required - The Windows process handle to query. ### Response #### Success Response (200) - **working_set_size** (u64) - Current working set size in bytes. - **peak_working_set_size** (u64) - Peak working set size in bytes. - **page_fault_count** (u32) - Total number of page faults. - **pagefile_usage** (u64) - Current pagefile usage in bytes. - **private_usage** (u64) - Current private memory usage in bytes. ``` -------------------------------- ### Create a job object with predefined limits Source: https://context7.com/ohadravid/win32job-rs/llms.txt Shows how to configure memory and priority limits using ExtendedLimitInfo before creating the job. This approach ensures the process is constrained immediately upon assignment. ```rust use win32job::{Job, ExtendedLimitInfo, PriorityClass, JobError}; fn main() -> Result<(), JobError> { let mut info = ExtendedLimitInfo::new(); info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024) .limit_priority_class(PriorityClass::BelowNormal); let job = Job::create_with_limit_info(&info)?; job.assign_current_process()?; Ok(()) ``` -------------------------------- ### Create an anonymous Windows job object Source: https://context7.com/ohadravid/win32job-rs/llms.txt Demonstrates how to initialize a new job object using the Job::create method. The handle is automatically managed and closed when the object goes out of scope. ```rust use win32job::{Job, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; println!("Job created successfully with handle: {}", job.handle()); Ok(()) } ``` -------------------------------- ### Job::create Source: https://context7.com/ohadravid/win32job-rs/llms.txt Creates a new, unconfigured Windows job object. ```APIDOC ## Job::create ### Description Creates a new anonymous Windows job object without any initial limits. ### Method Rust Method (Constructor) ### Endpoint win32job::Job::create() ### Parameters None ### Response - **Result** - Returns a Job instance on success or a JobError on failure. ``` -------------------------------- ### Job::create_with_limit_info Source: https://context7.com/ohadravid/win32job-rs/llms.txt Creates a job object with pre-defined resource limits. ```APIDOC ## Job::create_with_limit_info ### Description Creates a new job object and immediately applies the specified ExtendedLimitInfo configuration. ### Method Rust Method (Constructor) ### Parameters - **info** (ExtendedLimitInfo) - Required - The configuration object containing memory and priority limits. ### Response - **Result** - Returns a configured Job instance. ``` -------------------------------- ### Configure Job Extended Limits Source: https://context7.com/ohadravid/win32job-rs/llms.txt Demonstrates how to query, modify, and apply extended limit information to a job object, including memory and scheduling class constraints. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let mut info = job.query_extended_limit_info()?; info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024) .limit_scheduling_class(3); job.set_extended_limit_info(&info)?; job.assign_current_process()?; info.clear_limits(); job.set_extended_limit_info(&info)?; Ok(()) } ``` -------------------------------- ### Access Windows Job Handle Source: https://context7.com/ohadravid/win32job-rs/llms.txt Shows how to access the underlying Windows HANDLE for a job object, either by borrowing or by consuming the job object. ```rust use win32job::{Job, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let handle = job.handle(); let another_job = Job::create()?; let owned_handle = another_job.into_handle(); Ok(()) } ``` -------------------------------- ### Assign a process to a job object Source: https://context7.com/ohadravid/win32job-rs/llms.txt Explains how to associate a process with a job object. Once assigned, the process is subject to the resource constraints defined by the job. ```rust use win32job::{Job, JobError}; use std::process::Command; fn main() -> Result<(), JobError> { let job = Job::create()?; let child = Command::new("cmd.exe").arg("/C").arg("ping -n 5 127.0.0.1").spawn().expect("Failed to spawn process"); job.assign_current_process()?; Ok(()) ``` -------------------------------- ### Assign current process to a job Source: https://context7.com/ohadravid/win32job-rs/llms.txt Demonstrates the convenience method to assign the current running process to a job, often used after updating limit information. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let mut info = job.query_extended_limit_info()?; info.limit_working_memory(2 * 1024 * 1024, 8 * 1024 * 1024); job.set_extended_limit_info(&info)?; job.assign_current_process()?; println!("Current process is now part of the job"); Ok(()) ``` -------------------------------- ### utils::get_current_process Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves a pseudo-handle for the currently executing process. ```APIDOC ## GET /utils/get_current_process ### Description Returns a pseudo-handle to the current process, which can be used for job assignment operations. ### Method GET ### Response #### Success Response (200) - **handle** (isize) - The pseudo-handle value. ``` -------------------------------- ### Job Object Error Handling (Rust) Source: https://context7.com/ohadravid/win32job-rs/llms.txt Demonstrates how to handle `JobError` which is returned by job operations. It shows matching specific errors like `CreateFailed` and `AssignFailed`, and how `JobError` can be converted into `std::io::Error` for seamless integration with Rust's standard error handling. ```rust use win32job::{Job, JobError}; fn main() { match Job::create() { Ok(job) => { if let Err(e) = job.assign_process(0) { // Invalid handle match e { JobError::AssignFailed(io_err) => { println!("Failed to assign: {}", io_err); } _ => println!("Other error: {}", e), } } } Err(JobError::CreateFailed(io_err)) => { println!("Failed to create job: {}", io_err); } Err(e) => println!("Unexpected error: {}", e), } // JobError can be converted to std::io::Error fn example() -> Result<(), std::io::Error> { let job = Job::create()?; // JobError converts to io::Error Ok(()) } } ``` -------------------------------- ### Set Priority Class for Job Processes Source: https://context7.com/ohadravid/win32job-rs/llms.txt Demonstrates how to restrict the priority class of all processes within a job. Once set, processes cannot modify their own priority. ```rust use win32job::{Job, ExtendedLimitInfo, PriorityClass, JobError}; fn main() -> Result<(), JobError> { let mut info = ExtendedLimitInfo::new(); info.limit_priority_class(PriorityClass::BelowNormal); let job = Job::create_with_limit_info(&info)?; job.assign_current_process()?; Ok(()) } ``` -------------------------------- ### Retrieve Current Process Pseudo-Handle Source: https://context7.com/ohadravid/win32job-rs/llms.txt Obtains a pseudo-handle for the current process, which is useful for assigning the current process to a job object. ```rust use win32job::utils::get_current_process; fn main() { let handle = get_current_process(); println!("Current process pseudo-handle: {}", handle); } ``` -------------------------------- ### Query Job Process List Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves a list of all process identifiers currently associated with a job object, including nested child jobs. ```rust use win32job::{Job, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let pids = job.query_process_id_list()?; job.assign_current_process()?; let pids = job.query_process_id_list()?; let current_pid = std::process::id() as usize; assert!(pids.contains(¤t_pid)); Ok(()) } ``` -------------------------------- ### Query job limit information Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves the current limit configuration of a job object, allowing inspection of the underlying Windows JOBOBJECT_EXTENDED_LIMIT_INFORMATION structure. ```rust use win32job::{Job, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let info = job.query_extended_limit_info()?; let scheduling_class = info.0.BasicLimitInformation.SchedulingClass; println!("Default scheduling class: {}", scheduling_class); Ok(()) ``` -------------------------------- ### Terminate Sub-processes on Main Process Exit using win32job Source: https://github.com/ohadravid/win32job-rs/blob/main/README.md This Rust code snippet shows how to configure a job object to automatically terminate all associated child processes when the main process exits. It creates a job, queries and modifies its extended limit information to enable `limit_kill_on_job_close`, and then assigns the current process to this job. A child process is spawned to demonstrate the functionality. ```rust use win32job::Job; use std::process::Command; fn main() -> Result<(), Box> { let job = Job::create()?; let mut info = job.query_extended_limit_info()?; info.limit_kill_on_job_close(); job.set_extended_limit_info(&mut info)?; job.assign_current_process()?; Command::new("cmd.exe") .arg("/C") .arg("ping -n 9999 127.0.0.1") .spawn()?; // The cmd will be killed once we exit, or `job` is dropped. Ok(()) } ``` -------------------------------- ### Job::handle and Job::into_handle Source: https://context7.com/ohadravid/win32job-rs/llms.txt Access the underlying Windows HANDLE for the job object. `handle()` returns the handle while keeping ownership, `into_handle()` consumes the job and returns the handle (caller must close it manually). ```APIDOC ## Job::handle and Job::into_handle ### Description Access the underlying Windows HANDLE for the job object. `handle()` returns the handle while keeping ownership, `into_handle()` consumes the job and returns the handle (caller must close it manually). ### Method (Implicitly called via `Job` methods) ### Endpoint N/A (Rust function) ### Parameters N/A (Rust method parameters) ### Request Example ```rust use win32job::{Job, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let handle = job.handle(); println!("Job handle: {}", handle); let another_job = Job::create()?; let owned_handle = another_job.into_handle(); Ok(()) } ``` ### Response N/A (Rust function return) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Enable Kill-on-Close Behavior Source: https://context7.com/ohadravid/win32job-rs/llms.txt Configures a job to automatically terminate all associated processes when the job handle is closed, preventing orphaned child processes. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; use std::process::Command; fn main() -> Result<(), JobError> { let job = Job::create()?; let mut info = job.query_extended_limit_info()?; info.limit_kill_on_job_close(); job.set_extended_limit_info(&info)?; job.assign_current_process()?; Command::new("cmd.exe").arg("/C").arg("ping -n 9999 127.0.0.1").spawn()?; Ok(()) } ``` -------------------------------- ### Set Working Memory Limits Source: https://context7.com/ohadravid/win32job-rs/llms.txt Configures the minimum and maximum working set sizes for processes within a job, affecting how memory is paged. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let mut info = ExtendedLimitInfo::new(); info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024); let job = Job::create_with_limit_info(&info)?; job.assign_current_process()?; Ok(()) } ``` -------------------------------- ### Limit Working Memory for a Process using win32job Source: https://github.com/ohadravid/win32job-rs/blob/main/README.md This Rust code snippet demonstrates how to limit the working memory available to a process using the win32job crate. It creates a job object with extended limit information and assigns the current process to it. The `limit_working_memory` function sets the hard and soft limits for memory allocation. ```rust use win32job::{Job, ExtendedLimitInfo}; fn main() -> Result<(), Box> { let mut info = ExtendedLimitInfo::new(); info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024); let job = Job::create_with_limit_info(&mut info)?; job.assign_current_process()?; Ok(()) } ``` -------------------------------- ### Job::query_extended_limit_info Source: https://context7.com/ohadravid/win32job-rs/llms.txt Retrieves current limit information from a job object. ```APIDOC ## Job::query_extended_limit_info ### Description Queries the job object to retrieve the current JOBOBJECT_EXTENDED_LIMIT_INFORMATION structure. ### Method Rust Method ### Parameters None ### Response - **Result** - Returns the current limit configuration. ``` -------------------------------- ### Configure Job Breakaway Permissions Source: https://context7.com/ohadravid/win32job-rs/llms.txt Controls whether child processes can escape the job constraints. Supports explicit breakaway via flags or silent automatic breakaway. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let mut info = ExtendedLimitInfo::new(); info.limit_breakaway_ok(); let job = Job::create_with_limit_info(&info)?; job.assign_current_process()?; Ok(()) } ``` -------------------------------- ### Job::query_process_id_list Source: https://context7.com/ohadravid/win32job-rs/llms.txt Returns all process identifiers for processes assigned to the job. If the job is nested, the list includes all processes from the job and its child jobs. ```APIDOC ## Job::query_process_id_list ### Description Returns all process identifiers for processes assigned to the job. If the job is nested, the list includes all processes from the job and its child jobs. ### Method (Implicitly called via `Job` methods) ### Endpoint N/A (Rust function) ### Parameters N/A (Rust method parameters) ### Request Example ```rust use win32job::{Job, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let pids = job.query_process_id_list()?; println!("Processes before assignment: {:?}", pids); // [] job.assign_current_process()?; let pids = job.query_process_id_list()?; let current_pid = std::process::id() as usize; println!("Processes after assignment: {:?}", pids); assert!(pids.contains(¤t_pid)); Ok(()) } ``` ### Response N/A (Rust function return) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Set Scheduling Class for Job Processes Source: https://context7.com/ohadravid/win32job-rs/llms.txt Configures the scheduling class for a job, which influences CPU scheduling favorability. Values range from 0 to 9, where higher values are more favorable. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let mut info = ExtendedLimitInfo::new(); info.limit_scheduling_class(3); let job = Job::create_with_limit_info(&info)?; job.assign_current_process()?; Ok(()) } ``` -------------------------------- ### Job::set_extended_limit_info Source: https://context7.com/ohadravid/win32job-rs/llms.txt Sets the basic and extended limit information for a job object. This applies the configured limits to all processes currently assigned to the job and any future processes that will be assigned. ```APIDOC ## Job::set_extended_limit_info ### Description Sets the basic and extended limit information for a job object. This applies the configured limits to all processes currently assigned to the job and any future processes that will be assigned. ### Method (Implicitly called via `Job` methods) ### Endpoint N/A (Rust function) ### Parameters N/A (Rust method parameters) ### Request Example ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let mut info = job.query_extended_limit_info()?; info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024) .limit_scheduling_class(3); job.set_extended_limit_info(&info)?; job.assign_current_process()?; info.clear_limits(); job.set_extended_limit_info(&info)?; Ok(()) } ``` ### Response N/A (Rust function return) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Job::assign_current_process Source: https://context7.com/ohadravid/win32job-rs/llms.txt Assigns the calling process to the specified job object. ```APIDOC ## Job::assign_current_process ### Description Convenience method that assigns the current process to the job, subjecting it to the job's defined limits. ### Method Rust Method ### Parameters None ### Response - **Result<(), JobError>** - Returns Ok(()) on success. ``` -------------------------------- ### ExtendedLimitInfo::limit_scheduling_class Source: https://context7.com/ohadravid/win32job-rs/llms.txt Configures the scheduling class for processes in the job, affecting CPU time allocation. ```APIDOC ## POST /ExtendedLimitInfo/limit_scheduling_class ### Description Sets the scheduling class for all processes in the job. Valid values are 0-9, with 5 being the default. ### Method POST ### Parameters #### Request Body - **class** (u8) - Required - Scheduling class value (0-9). ### Request Example { "class": 3 } ``` -------------------------------- ### ExtendedLimitInfo::limit_working_memory Source: https://context7.com/ohadravid/win32job-rs/llms.txt Sets the minimum and maximum working set sizes for all processes in the job. Working set is the portion of a process's virtual address space that is resident in physical memory. ```APIDOC ## ExtendedLimitInfo::limit_working_memory ### Description Sets the minimum and maximum working set sizes for all processes in the job. Working set is the portion of a process's virtual address space that is resident in physical memory. ### Method (Implicitly called via `ExtendedLimitInfo` methods) ### Endpoint N/A (Rust function) ### Parameters N/A (Rust method parameters) ### Request Example ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let mut info = ExtendedLimitInfo::new(); info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024); let job = Job::create_with_limit_info(&info)?; job.assign_current_process()?; let mut big_vec: Vec = Vec::with_capacity(16 * 1024 * 1024); big_vec.resize(16 * 1024 * 1024, 1); Ok(()) } ``` ### Response N/A (Rust function return) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### ExtendedLimitInfo::limit_kill_on_job_close Source: https://context7.com/ohadravid/win32job-rs/llms.txt Configures the job to terminate all associated processes when the last handle to the job is closed. This is useful for ensuring child processes don't outlive the parent. ```APIDOC ## ExtendedLimitInfo::limit_kill_on_job_close ### Description Configures the job to terminate all associated processes when the last handle to the job is closed. This is useful for ensuring child processes don't outlive the parent. ### Method (Implicitly called via `ExtendedLimitInfo` methods) ### Endpoint N/A (Rust function) ### Parameters N/A (Rust method parameters) ### Request Example ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; use std::process::Command; fn main() -> Result<(), JobError> { let job = Job::create()?; let mut info = job.query_extended_limit_info()?; info.limit_kill_on_job_close(); job.set_extended_limit_info(&info)?; job.assign_current_process()?; Command::new("cmd.exe") .arg("/C") .arg("ping -n 9999 127.0.0.1") .spawn()?; Ok(()) } ``` ### Response N/A (Rust function return) #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### ExtendedLimitInfo::limit_priority_class Source: https://context7.com/ohadravid/win32job-rs/llms.txt Sets the priority class for all processes within a job, preventing them from modifying their own priority. ```APIDOC ## POST /ExtendedLimitInfo/limit_priority_class ### Description Sets the priority class for all processes in the job. Processes cannot modify their own priority class after being assigned to a job with this limit. ### Method POST ### Parameters #### Request Body - **priority_class** (PriorityClass) - Required - The desired priority level (e.g., Idle, Normal, High, Realtime). ### Request Example { "priority_class": "PriorityClass::BelowNormal" } ``` -------------------------------- ### ExtendedLimitInfo::limit_affinity Source: https://context7.com/ohadravid/win32job-rs/llms.txt Restricts the CPU cores that processes within the job are allowed to execute on. ```APIDOC ## POST /ExtendedLimitInfo/limit_affinity ### Description Sets the processor affinity mask for all processes in the job to restrict execution to specific CPUs. ### Method POST ### Parameters #### Request Body - **mask** (usize) - Required - Bitmask representing allowed CPUs. ### Request Example { "mask": 1 } ``` -------------------------------- ### ExtendedLimitInfo::clear_limits Source: https://context7.com/ohadravid/win32job-rs/llms.txt Resets all previously applied limits on the ExtendedLimitInfo object. ```APIDOC ## POST /ExtendedLimitInfo/clear_limits ### Description Removes all limits from the ExtendedLimitInfo structure, effectively resetting the job's constraints. ### Method POST ``` -------------------------------- ### Set Processor Affinity for Job Processes Source: https://context7.com/ohadravid/win32job-rs/llms.txt Restricts the CPU cores that processes within a job are permitted to run on using an affinity mask. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; use win32job::utils::get_process_affinity_mask; fn main() -> Result<(), JobError> { let mut info = ExtendedLimitInfo::new(); info.limit_affinity(1); let job = Job::create_with_limit_info(&info)?; job.assign_current_process()?; let current_proc = win32job::utils::get_current_process(); let (proc_affinity, system_affinity) = get_process_affinity_mask(current_proc)?; println!("Process affinity: {}", proc_affinity); Ok(()) } ``` -------------------------------- ### Clear Job Limits Source: https://context7.com/ohadravid/win32job-rs/llms.txt Resets all previously applied limits within an ExtendedLimitInfo structure, effectively removing constraints from the associated job. ```rust use win32job::{Job, ExtendedLimitInfo, JobError}; fn main() -> Result<(), JobError> { let job = Job::create()?; let mut info = job.query_extended_limit_info()?; info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024).limit_kill_on_job_close(); job.set_extended_limit_info(&info)?; info.clear_limits(); job.set_extended_limit_info(&info)?; Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.