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, wherechild_pidis the PID of the newly created child.
§Errors
SyscallError::OutOfMemory- Insufficient memory to create the child process control block or initial page tables.SyscallError::PermissionDenied- The caller lacks the capability to create new processes.SyscallError::InternalError- The process table is full or an unexpected kernel error occurred.
§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
}
}