2 stable releases

1.0.1 Feb 5, 2020

#68 in Windows APIs

Download history 38/week @ 2021-10-01 22/week @ 2021-10-08 6/week @ 2021-10-15 46/week @ 2021-10-22 55/week @ 2021-10-29 104/week @ 2021-11-05 27/week @ 2021-11-12 98/week @ 2021-11-19 93/week @ 2021-11-26 72/week @ 2021-12-03 36/week @ 2021-12-10 37/week @ 2021-12-17 27/week @ 2021-12-24 70/week @ 2021-12-31 67/week @ 2022-01-07 35/week @ 2022-01-14

211 downloads per month
Used in tp-note

MIT/Apache

23KB
356 lines

win32job

crates.io

Documentation

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

# Cargo.toml
[dependencies]
win32job = "1"

Examples

Limit the amount of memory that 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

~295–750KB
~18K SLoC

:^