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

Go to the source code of this file.

Data Structures

struct  fs_context
 where paths start, and the mode bits new files lose. More...
struct  process
 process control block. More...
struct  process_info_t
 a copy of what procfs shows about a process. More...

Macros

#define PROCESS_MAX   64
#define PID_NONE   0
#define PID_INIT   1
#define KERNEL_STACK_SIZE   (2 * PAGE_SIZE)
#define PATH_MAX   256
#define PROCESS_NAME_MAX   32

Typedefs

typedef struct fs_context fs_context_t
 where paths start, and the mode bits new files lose.
typedef struct process process_t
 process control block.

Enumerations

enum  process_state_t {
  PROC_STATE_UNUSED = 0 , PROC_STATE_NEW , PROC_STATE_RUNNING , PROC_STATE_READY ,
  PROC_STATE_BLOCKED , PROC_STATE_ZOMBIE , PROC_STATE_STOPPED
}
 process states. More...
enum  wait_report_t { WAIT_REPORT_NONE = 0 , WAIT_REPORT_STOPPED , WAIT_REPORT_CONTINUED }
 a change of a child that waitpid has not reported yet. More...

Functions

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_init (void)
 initialize the process subsystem.
process_t * process_create (void)
 create a new process and return its PCB.
process_t * process_fork (const struct registers *regs)
 fork the current process.
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.
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).
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).
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).
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).
process_t * process_current (void)
 get the currently running process.
void process_set_current (process_t *proc)
 set the currently running process.
void process_destroy (process_t *proc)
 destroy a process and free its slot in the process table.
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).
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.

Macro Definition Documentation

◆ PROCESS_MAX

#define PROCESS_MAX   64

◆ PID_NONE

#define PID_NONE   0

◆ PID_INIT

#define PID_INIT   1

◆ KERNEL_STACK_SIZE

#define KERNEL_STACK_SIZE   (2 * PAGE_SIZE)

◆ PATH_MAX

#define PATH_MAX   256

◆ PROCESS_NAME_MAX

#define PROCESS_NAME_MAX   32

Typedef Documentation

◆ fs_context_t

typedef struct fs_context fs_context_t

where paths start, and the mode bits new files lose.

the threads of a process use one together; it is freed when the last lets it go.

◆ process_t

typedef struct process process_t

process control block.

Enumeration Type Documentation

◆ process_state_t

process states.

Enumerator
PROC_STATE_UNUSED 
PROC_STATE_NEW 
PROC_STATE_RUNNING 
PROC_STATE_READY 
PROC_STATE_BLOCKED 
PROC_STATE_ZOMBIE 
PROC_STATE_STOPPED 

◆ wait_report_t

a change of a child that waitpid has not reported yet.

Enumerator
WAIT_REPORT_NONE 
WAIT_REPORT_STOPPED 
WAIT_REPORT_CONTINUED 

Function Documentation

◆ 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_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_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_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_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_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).

◆ 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_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_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_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_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.

◆ 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.