The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
process.c File Reference

Functions

void process_init (void)
 initialize the process subsystem.
process_t * process_create (void)
 create a new process and return its PCB.
process_t * process_clone_thread (const struct registers *regs, uintptr_t stack)
 make a thread of the current process: a new task in its group that shares its memory, descriptors, fs context and handlers.
process_t * process_fork (const struct registers *regs)
 fork the current process.
process_t * process_current (void)
 get the currently running process.
void process_set_current (process_t *proc)
 set the currently running process.
void process_release_resources (process_t *proc)
 release what a process owns apart from its kernel stack and table slot: close its fds, free its argv copy and destroy its address space (after switching away from it, when it is the current process).
fs_context_t * fs_context_get (fs_context_t *fs)
 add a user to an fs context (a new thread that shares it).
void fs_context_put (fs_context_t *fs)
 remove a user from an fs context; the last user frees it.
void process_destroy (process_t *proc)
 destroy a process and free its slot in the process table.
void process_orphan_children (process_t *parent)
 detach the children of an exiting group (call when its last thread ends; parent is that thread): zombie children are destroyed now (nobody wants their status), running ones get init (PID 1) as their parent, which reaps them.
void process_note_orphan_zombie (void)
 record that an orphan (no parent) has become a zombie and must be reaped by the kernel.
void process_reap_orphans (void)
 destroy orphan zombies.
process_t * process_find_waitable_child (process_t *parent, int32_t pid, uint32_t options, bool *has_child)
 find a child of a process that has something for waitpid: it exited (a zombie), or, with WUNTRACED / WCONTINUED in options, it stopped or continued and that was not reported yet.
uint32_t process_list_group (uint32_t pgid, uint32_t *pids, uint32_t max)
 list the pids of a process group.
bool process_group_exists (uint32_t pgid, uint32_t sid)
 true if some live process is in the group and the session.
void process_for_each (void(*fn)(process_t *proc, void *ctx), void *ctx)
 call fn for every process in the table, with the table locked and interrupts masked: fn must not sleep.
uint32_t process_list_pids (uint32_t *pids, uint32_t max)
 list the pids in use.
bool process_info (uint32_t pid, process_info_t *info)
 copy what procfs shows about a process, safely against its exit.
void process_set_name (process_t *proc, const char *path)
 set a process's name from a program path (its last component).
uint64_t process_idle_ticks (void)
 the timer ticks that the cores spent in their idle loops since boot, added over all cores.
void process_account_tick (bool user)
 charge one timer tick to the current process (timer interrupt).
void process_copy_credentials (process_t *child, const process_t *parent)
 give a new process the user and group ids and umask of another.
process_t * process_get (uint32_t pid)
 look up a process by PID.
void process_signal_threads (uint32_t tgid, process_t *except, int sig)
 send a signal to the threads of a group (signal_send to each).
process_t * process_pick_thread (uint32_t tgid, uint32_t mask)
 choose the thread of a group that takes a signal sent to the process: the leader if it does not block mask, else a thread that does not, else any of them (the leader first).
void process_sync_threads (process_t *proc)
 give every other thread of a process's group its user and group ids, its process group and its session (they belong to the process).
void process_take_leader (process_t *proc)
 make a thread the leader of its group in place of the old leader, which has ended (a zombie) and is the only other member: the thread takes the leader's pid and its place for the parent, and the old slot is reaped by the kernel (execve from a thread that is not the leader).

Function Documentation

◆ process_init()

void process_init ( void )

initialize the process subsystem.

must be called once during kernel startup.

◆ process_create()

process_t * process_create ( void )

create a new process and return its PCB.

allocates a PID and initializes the fd table with stdin/stdout/stderr. the process starts in PROC_STATE_NEW; scheduler_ready() makes it runnable.

Returns
pointer to the new process, or NULL if the table is full or its kernel stack or address space could not be allocated.

◆ process_clone_thread()

process_t * process_clone_thread ( const struct registers * regs,
uintptr_t stack )

make a thread of the current process: a new task in its group that shares its memory, descriptors, fs context and handlers.

it resumes from the same syscall, which returns 0 there, with stack as its user stack pointer. the caller makes it runnable.

Parameters
regsthe caller's saved user registers (its syscall frame).
stackthe thread's user stack pointer.
Returns
the thread, or NULL if the table or the memory ran out.

◆ process_fork()

process_t * process_fork ( const struct registers * regs)

fork the current process.

the child is a copy of the caller: the same memory (shared copy-on-write), areas, fds, cwd and break. it is made runnable at once and resumes from the same syscall, which returns 0 in the child.

Parameters
regsthe caller's saved user registers (its syscall frame).
Returns
the child, or NULL if the process table, a kernel stack or memory for the page tables ran out.

◆ process_current()

process_t * process_current ( void )

get the currently running process.

Returns
pointer to the current process, or NULL if none is running.

◆ process_set_current()

void process_set_current ( process_t * proc)

set the currently running process.

Parameters
procthe process to mark as current.

◆ process_release_resources()

void process_release_resources ( process_t * proc)

release what a process owns apart from its kernel stack and table slot: close its fds, free its argv copy and destroy its address space (after switching away from it, when it is the current process).

run by an exiting process in its own context, since closing a file may sleep. safe to call more than once.

Parameters
procthe process.

◆ fs_context_get()

fs_context_t * fs_context_get ( fs_context_t * fs)

add a user to an fs context (a new thread that shares it).

Returns
fs.

◆ fs_context_put()

void fs_context_put ( fs_context_t * fs)

remove a user from an fs context; the last user frees it.

Parameters
fsthe context, or NULL (nothing is done).

◆ process_destroy()

void process_destroy ( process_t * proc)

destroy a process and free its slot in the process table.

closes any open file descriptors.

Parameters
procthe process to destroy.

◆ process_orphan_children()

void process_orphan_children ( process_t * parent)

detach the children of an exiting group (call when its last thread ends; parent is that thread): zombie children are destroyed now (nobody wants their status), running ones get init (PID 1) as their parent, which reaps them.

without init they lose their parent (PID_NONE) and the kernel reaps them when they exit.

Parameters
parentthe exiting process.

◆ process_note_orphan_zombie()

void process_note_orphan_zombie ( void )

record that an orphan (no parent) has become a zombie and must be reaped by the kernel.

the reap waits until the zombie has left its core.

◆ process_reap_orphans()

void process_reap_orphans ( void )

destroy orphan zombies.

called by the scheduler right after a context switch, on the next process's stack, so a dead process's own stack is never freed while in use. runs with interrupts masked.

◆ process_find_waitable_child()

process_t * process_find_waitable_child ( process_t * parent,
int32_t pid,
uint32_t options,
bool * has_child )

find a child of a process that has something for waitpid: it exited (a zombie), or, with WUNTRACED / WCONTINUED in options, it stopped or continued and that was not reported yet.

a zombie that it returns is marked reaped, so no other waitpid takes it. call it with the signal state lock held (signal_state_lock), which guards the reports.

Parameters
parentthe parent.
pida child's pid, or -1 for any child.
optionswaitpid options (wait_status.h).
has_childset to true if parent has at least one matching child.
Returns
the child, or NULL if none has anything to report yet.

◆ process_list_group()

uint32_t process_list_group ( uint32_t pgid,
uint32_t * pids,
uint32_t max )

list the pids of a process group.

Parameters
pgidthe group.
pidsreceives up to max pids.
maxcapacity of pids.
Returns
the number stored.

◆ process_group_exists()

bool process_group_exists ( uint32_t pgid,
uint32_t sid )

true if some live process is in the group and the session.

◆ process_for_each()

void process_for_each ( void(* fn )(process_t *proc, void *ctx),
void * ctx )

call fn for every process in the table, with the table locked and interrupts masked: fn must not sleep.

Parameters
fnthe function.
ctxpassed to fn.

◆ process_list_pids()

uint32_t process_list_pids ( uint32_t * pids,
uint32_t max )

list the pids in use.

Parameters
pidsreceives up to max pids, in table order.
maxcapacity of pids.
Returns
the number stored.

◆ process_info()

bool process_info ( uint32_t pid,
process_info_t * info )

copy what procfs shows about a process, safely against its exit.

Parameters
pidthe process.
inforeceives the copy.
Returns
false if no such process exists.

◆ process_set_name()

void process_set_name ( process_t * proc,
const char * path )

set a process's name from a program path (its last component).

Parameters
procthe process.
paththe program's path.

◆ process_idle_ticks()

uint64_t process_idle_ticks ( void )

the timer ticks that the cores spent in their idle loops since boot, added over all cores.

◆ process_account_tick()

void process_account_tick ( bool user)

charge one timer tick to the current process (timer interrupt).

Parameters
usertrue if user code was running.

◆ process_copy_credentials()

void process_copy_credentials ( process_t * child,
const process_t * parent )

give a new process the user and group ids and umask of another.

◆ process_get()

process_t * process_get ( uint32_t pid)

look up a process by PID.

the pointer stays valid only while the process cannot be destroyed (it is the caller, or the caller's unreaped child).

Parameters
pidthe process identifier.
Returns
pointer to the process, or NULL if not found.

◆ process_signal_threads()

void process_signal_threads ( uint32_t tgid,
process_t * except,
int sig )

send a signal to the threads of a group (signal_send to each).

Parameters
tgidthe group.
excepta thread that is left out, or NULL.
sigthe signal.

◆ process_pick_thread()

process_t * process_pick_thread ( uint32_t tgid,
uint32_t mask )

choose the thread of a group that takes a signal sent to the process: the leader if it does not block mask, else a thread that does not, else any of them (the leader first).

Parameters
tgidthe group.
maskthe signal's bit.
Returns
the thread, or NULL if the group has none left.

◆ process_sync_threads()

void process_sync_threads ( process_t * proc)

give every other thread of a process's group its user and group ids, its process group and its session (they belong to the process).

Parameters
procthe thread whose values the others take.

◆ process_take_leader()

void process_take_leader ( process_t * proc)

make a thread the leader of its group in place of the old leader, which has ended (a zombie) and is the only other member: the thread takes the leader's pid and its place for the parent, and the old slot is reaped by the kernel (execve from a thread that is not the leader).

Parameters
procthe thread (the current one).