PatchworkOS  da8a090
A non-POSIX operating system.
Loading...
Searching...
No Matches

Processes. More...

Collaboration diagram for Processes:

Detailed Description

Processes.

Processes store the shared resources for threads of execution, for example the address space and open files.

Process Filesystem

Each process has a directory located at /proc/[pid], which contains various files that can be used to interact with the process. Additionally, there is a /proc/self bound mount point that points to the /proc/[pid] directory of the current process.

Included below is a list of all entries found in the /proc/[pid] directory along with their formats.

prio

A readable and writable file that contains the scheduling priority of the process.

Format:

%llu

cwd

A readable and writable file that contains the current working directory of the process.

Format:

%s

cmdline

A readable file that contains the command line arguments of the process (argv).

Format:

%s\0%s\0...%s\0

note

A writable file that can be used to send notes to the process. Writing data to this file will enqueue that data as a note in the note queue of one of the process's threads.

wait

A readable and pollable file that can be used to wait for the process to exit and retrieve its exit status. Reading from this file will block until the process has exited.

Format:

%lld

perf

A readable file that contains performance statistics for the process.

Format:

user_clocks kernel_clocks start_clocks user_pages thread_count
%llu %llu %llu %llu %llu

ctl

A writable file that can be used to control certain aspects of the process, such as closing file descriptors.

Included is a list of all supported commands.

close <fd>

Closes the specified file descriptor in the process.

close <minfd> <maxfd>

Closes the range [minfd, maxfd) of file descriptors in the process.

Note that specifying -1 as maxfd will close all file descriptors from minfd to the maximum allowed file descriptor.

dup2 <oldfd> <newfd>

Duplicates the specified old file descriptor to the new file descriptor in the process.

start

Starts the process if it was previously suspended.

kill

Sends a kill note to all threads in the process, effectively terminating it.

fd

Todo:
Implement the /proc/[pid]/fd directory.

env

A directory that contains the environment variables of the process. Each environment variable is represented as a readable and writable file whose name is the name of the variable and whose content is the value of the variable.

To add or modify an environment variable, create or write to a file with the name of the variable. To remove an environment variable, delete the corresponding file.

Data Structures

struct  process_threads_t
 Represents the threads in a process. More...
 
struct  process_t
 Process structure. More...
 

Enumerations

enum  process_flags_t { PROCESS_NONE = 0 , PROCESS_DYING = 1 << 0 , PROCESS_SUSPENDED = 1 << 1 }
 Process flags enum. More...
 

Functions

process_tprocess_new (priority_t priority)
 Allocates and initializes a new process.
 
void process_kill (process_t *process, int32_t status)
 Kills a process.
 
uint64_t process_copy_env (process_t *dest, process_t *src)
 Copies the environment variables from one process to another.
 
uint64_t process_set_cmdline (process_t *process, char **argv, uint64_t argc)
 Sets the command line arguments for a process.
 
bool process_has_thread (process_t *process, tid_t tid)
 Checks if a process has a thread with the specified thread ID.
 
process_tprocess_get_kernel (void)
 Gets the kernel process.
 
void process_procfs_init (void)
 Initializes the /proc directory.
 
void process_reaper_init (void)
 Initializes the process reaper.
 

Enumeration Type Documentation

◆ process_flags_t

Process flags enum.

Enumerator
PROCESS_NONE 
PROCESS_DYING 
PROCESS_SUSPENDED 

Definition at line 146 of file process.h.

Function Documentation

◆ process_new()

process_t * process_new ( priority_t  priority)

Allocates and initializes a new process.

There is no process_free(), instead use UNREF(), UNREF_DEFER() or process_kill() to free a process.

Parameters
priorityThe priority of the new process.
Returns
On success, the newly created process. On failure, NULL and errno is set.

Definition at line 718 of file process.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_kill()

void process_kill ( process_t process,
int32_t  status 
)

Kills a process.

Sends a kill note to all threads in the process and sets its exit status. Will also close all files opened by the process and deinitialize its /proc directory.

When all threads have exited and all entires in its /proc directory have been closed, the process will be freed.

Parameters
processThe process to kill.
statusThe exit status of the process.

Definition at line 776 of file process.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_copy_env()

uint64_t process_copy_env ( process_t dest,
process_t src 
)

Copies the environment variables from one process to another.

Parameters
destThe destination process, must have an empty environment.
srcThe source process.
Returns
On success, 0. On failure, ERR and errno is set to:
  • EINVAL: Invalid parameters.
  • EBUSY: The destination process already has environment variables.

Definition at line 817 of file process.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_set_cmdline()

uint64_t process_set_cmdline ( process_t process,
char **  argv,
uint64_t  argc 
)

Sets the command line arguments for a process.

This value is only used for the /proc/[pid]/cmdline file.

Parameters
processThe process to set the cmdline for.
argvThe array of argument strings.
argcThe number of arguments.
Returns
On success, 0. On failure, ERR and errno is set to:
  • EINVAL: Invalid parameters.
  • ENOMEM: Out of memory.

Definition at line 869 of file process.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_has_thread()

bool process_has_thread ( process_t process,
tid_t  tid 
)

Checks if a process has a thread with the specified thread ID.

Parameters
processThe process to check.
tidThe thread ID to look for.
Returns
true if the process has a thread with the specified ID, false otherwise.

Definition at line 927 of file process.c.

Here is the caller graph for this function:

◆ process_get_kernel()

process_t * process_get_kernel ( void  )

Gets the kernel process.

The kernel process will be initalized lazily on the first call to this function, which should happen during early boot.

Will never return NULL.

Will not increment the reference count of the returned process as it should never be freed either way.

Returns
The kernel process.

Definition at line 943 of file process.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_procfs_init()

void process_procfs_init ( void  )

Initializes the /proc directory.

Definition at line 958 of file process.c.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_reaper_init()

void process_reaper_init ( void  )

Initializes the process reaper.

The process reaper allows us to delay the freeing of processes, this is useful if, for example, another process wanted that process's exit status.

Definition at line 1032 of file process.c.

Here is the call graph for this function:
Here is the caller graph for this function: