setsid

Function setsid 

Source
pub fn setsid() -> Result<pid_t>
Expand description

Create session and set process group ID see setsid(2)

Upon successful completion, the setsid() system call returns the value of the process group ID of the new process group, which is the same as the process ID of the calling process.

§Errors

Returns an io::Error if the setsid system call fails. Common errors include:

  • The calling process is already a process group leader (EPERM)

§Example

use fork::{fork, Fork, setsid};

match fork::fork() {
    Ok(Fork::Parent(child)) => {
        println!("Parent process, child PID: {}", child);
    }
    Ok(Fork::Child) => {
        // Create new session
        match setsid() {
            Ok(sid) => {
                println!("New session ID: {}", sid);
                std::process::exit(0);
            }
            Err(e) => {
                eprintln!("Failed to create session: {}", e);
                std::process::exit(1);
            }
        }
    }
    Err(e) => eprintln!("Fork failed: {}", e),
}