⚠️ VeridianOS Kernel Documentation - This is low-level kernel code. All functions are unsafe unless explicitly marked otherwise. no_std

sys_fork

Function sys_fork 

Source
pub fn sys_fork() -> SyscallResult<u64>
Expand description

Fork the current process, creating a new child process.

Creates a near-identical copy of the calling process. The child process receives a copy of the parent’s address space (using copy-on-write pages for efficiency), file descriptor table, and capability set. The child inherits all capabilities that have the INHERIT flag set on them.

Both the parent and child return from this call, but with different return values so they can distinguish themselves. The child process receives a new unique PID and has its parent PID set to the calling process.

§Returns

  • Ok(0) in the child process.
  • Ok(child_pid) in the parent process, where child_pid is the PID of the newly created child.

§Errors

§Examples

use veridian_kernel::pkg::sdk::syscall_api::{sys_exit, sys_fork};

let result = sys_fork();
match result {
    Ok(0) => {
        // Child process: execute child-specific logic
        sys_exit(0);
    }
    Ok(child_pid) => {
        // Parent process: child_pid contains the new child's PID
        // Optionally wait for the child with sys_wait(child_pid)
    }
    Err(e) => {
        // Fork failed, handle the error
    }
}