AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
fork.c File Reference

fork(): a copy of the calling process. More...

#include <core/system.h>
#include <core/string.h>
#include <core/arch.h>
#include <mm/mm.h>
#include <mm/vm.h>
#include <mm/pmap.h>
#include <sys/proc.h>
#include <ds/queue.h>
#include <aquila/errno.h>
#include <net/socket.h>

Go to the source code of this file.

Functions

static int copy_fds (struct proc *parent, struct proc *fork)
 Copies the parent's descriptors to the child.
static int fork_proc_copy (struct proc *parent, struct proc *fork)
 Copies what the child inherits from the parent's process structure: its process group, umask, IDs, signal mask and actions, and heap.
int proc_fork (struct thread *thread, struct proc **ref)
 Makes a child process, a copy of the process of thread.

Detailed Description

fork(): a copy of the calling process.

The child gets copies of the parent's process structure, descriptors (referring to the same open files) and address space. The memory isn't copied: both processes share its pages read-only, and the first one to write a page gets its own copy (copy on write, vm_space_fork()). Only the calling thread is copied.

Definition in file fork.c.

Function Documentation

◆ copy_fds()

int copy_fds ( struct proc * parent,
struct proc * fork )
static

Copies the parent's descriptors to the child.

Each refers to the same open file, so the two processes share its offset. Returns 0 or -ENOMEM.

Definition at line 27 of file fork.c.

References copy_fds(), proc::fds, FDS_COUNT, fd::file, file_ref(), fd::flags, file::flags, kmalloc(), M_ZERO, and proc_fd_file().

Referenced by copy_fds(), and proc_fork().

◆ fork_proc_copy()

int fork_proc_copy ( struct proc * parent,
struct proc * fork )
static

Copies what the child inherits from the parent's process structure: its process group, umask, IDs, signal mask and actions, and heap.

Returns 0, or -ENOMEM if there was no memory to put the child in the process group.

Definition at line 50 of file fork.c.

References proc::entry, fork_proc_copy(), proc::gid, proc::heap, proc::heap_start, proc::mask, memcpy(), proc::pgrp, proc::pgrp_node, pgroup::procs, proc::sigaction, proc::sigmask, and proc::uid.

Referenced by fork_proc_copy(), and proc_fork().

◆ proc_fork()

int proc_fork ( struct thread * thread,
struct proc ** ref )

Makes a child process, a copy of the process of thread.

The child's one thread is a copy of thread, which will return 0 from fork(); it isn't made ready to run, the caller does that.

Parameters
threadthe thread calling fork()
refif not NULL, set to the child
Returns
0, or a negative errno: -ENOMEM, or -EAGAIN if there is no free process ID

Definition at line 81 of file fork.c.

References arch_proc_fork(), arch_syscall_return(), copy_fds(), proc::cwd, proc::fds, FDS_COUNT, fork_proc_copy(), queue::head, proc::heap_vm, kfree(), proc::name, qnode::next, NULL, thread::owner, proc::parent, proc::pgrp, proc::pgrp_node, proc::pid, vm_space::pmap, pmap_decref(), proc_fd_close(), proc_fork(), proc_new(), proc_pid_alloc(), proc_pid_free(), pgroup::procs, procs, vnode::ref, thread::spawned, proc::stack_vm, proc::threads, qnode::value, vfs_close(), vm_space::vm_entries, proc::vm_space, vm_space_destroy(), and vm_space_fork().

Referenced by proc_fork(), and sys_fork().