5 stable releases

2.0.0 Jan 26, 2024
1.0.4 Jan 26, 2024
1.0.3 Jan 24, 2024
1.0.2 Aug 12, 2022
1.0.1 Feb 5, 2020

#48 in Windows APIs

Download history 438/week @ 2023-12-18 217/week @ 2023-12-25 632/week @ 2024-01-01 773/week @ 2024-01-08 1117/week @ 2024-01-15 1254/week @ 2024-01-22 1169/week @ 2024-01-29 2050/week @ 2024-02-05 1851/week @ 2024-02-12 1527/week @ 2024-02-19 2267/week @ 2024-02-26 2906/week @ 2024-03-04 2257/week @ 2024-03-11 1793/week @ 2024-03-18 1632/week @ 2024-03-25 2120/week @ 2024-04-01

7,962 downloads per month
Used in 8 crates (3 directly)

MIT/Apache

24KB
371 lines

win32job

docs.rs crates.io

Documentation

A safe API for Windows' job objects, which can be used to set various limits to processes associated with them.

[dependencies]
win32job = "2"

Examples

Limit the amount of memory that will be available for this process (allocating more memory is still possible, but it will be paged):

use win32job::{Job, ExtendedLimitInfo};

fn main() -> Result<(), Box<dyn std::error::Error>>  {
    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(())
}

Force any created sub processes to exit when the main process exits:

use win32job::Job;
use std::process::Command;

fn main() -> Result<(), Box<dyn std::error::Error>>  {
    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(())
}

License

The win32job crate is licensed under either of

Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Dependencies

~152MB
~2.5M SLoC