|
ginnOS
The reference: libc, the libraries and the kernel
|
#include "process.h"#include "kernel/memory/kstack.h"#include "kernel/memory/pmm.h"#include "kernel/memory/memory_layout.h"#include "arch/arch.h"#include "arch/arch_context.h"#include "kernel/scheduler/scheduler.h"#include "kernel/process/wait_status.h"#include "kernel/sync/spinlock.h"#include "kernel/memory/heap.h"#include "kernel/assert.h"#include "kernel/cpu/cpu.h"#include "kernel/sync/atomic.h"#include "common/memory.h"#include "common/string.h"#include "kernel/memory/tlb.h"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). | |
| void process_init | ( | void | ) |
initialize the process subsystem.
must be called once during kernel startup.
| 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.
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.
| regs | the caller's saved user registers (its syscall frame). |
| stack | the thread's user stack pointer. |
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.
| regs | the caller's saved user registers (its syscall frame). |
| process_t * process_current | ( | void | ) |
get the currently running process.
| void process_set_current | ( | process_t * | proc | ) |
set the currently running process.
| proc | the process to mark as current. |
| 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.
| proc | the 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.
| fs | the context, or NULL (nothing is done). |
| void process_destroy | ( | process_t * | proc | ) |
destroy a process and free its slot in the process table.
closes any open file descriptors.
| proc | the process to destroy. |
| 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.
| parent | the exiting process. |
| 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.
| 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_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.
| parent | the parent. |
| pid | a child's pid, or -1 for any child. |
| options | waitpid options (wait_status.h). |
| has_child | set to true if parent has at least one matching child. |
list the pids of a process group.
| pgid | the group. |
| pids | receives up to max pids. |
| max | capacity of pids. |
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.
| fn | the function. |
| ctx | passed to fn. |
list the pids in use.
| pids | receives up to max pids, in table order. |
| max | capacity of pids. |
| bool process_info | ( | uint32_t | pid, |
| process_info_t * | info ) |
copy what procfs shows about a process, safely against its exit.
| pid | the process. |
| info | receives the copy. |
| void process_set_name | ( | process_t * | proc, |
| const char * | path ) |
set a process's name from a program path (its last component).
| proc | the process. |
| path | the program's path. |
| 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).
| user | true if user code was running. |
give a new process the user and group ids and umask of another.
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).
| pid | the process identifier. |
send a signal to the threads of a group (signal_send to each).
| tgid | the group. |
| except | a thread that is left out, or NULL. |
| sig | the signal. |
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).
| tgid | the group. |
| mask | the signal's bit. |
| 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).
| proc | the thread whose values the others take. |
| 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).
| proc | the thread (the current one). |