Processes
WARNING: This document is far from being complete and may lack a good description of what processes are, and/or the features they offer.
NightOS processes are implemented a bit like Linux' ones, with additional features. There are several types of processes:
- System processes, which are created by the system ;
- Application processes, in which applications run ;
- Worker processes, in which applications' workers run
The base and system processes are called low-level processes, while application and worker ones are called userland processes.
You can find the implementation details of processes in the kernel document.
User privileges
Each process is ran as a specific user, which determines the maximum allowed scope for controller requests, and with a list of initial privileges (the ones given to the application).
Child processes
A process can create child processes (it's called a fork). The child process will roughly be a 1:1 copy of the parent process, but with its own unique PID.
To avoid copying the whole memory, copy-on-write is used, which means that pages are identical and require no allocation until they are modified, in which case a new page is created and attached to the process in place of the original one.
Child processes automatically inherit their parent's permissions.
When a process exits, all its child processes are immediatly killed. It's up to the process to ensure its children are properly terminated before it.
Threading
A process can create threads, which are still a part of the process. Threads allow to run multiple part of a process concurrently, as the kernel may run several threads in different processor cores.
All threads share the same address space and memory, although they also have a reserved space called the thread-local storage.
To avoid copying the whole memory, copy-on-write is used, which means that pages are identical and require no allocation until they are modified, in which case a new page is created and attached to the process in place of the original one.
Also, the stack is local to each thread.
Threads work as a hierarchy ; when a thread creates another, it is called the new thread's parent, while the new thread is its child. When a thread terminates, all its children are instantly destroyed.
Main thread
When a process starts, its instruction run its main thread. Due to threads being hierarchised, exiting the main thread will result in all other threads being closed immediatly, which is why the main thread should always first terminate its children properly to ensure all data are synchronized for instance.
Thread-local storage
Threads have a reserved portion of memory in their address space called the thread-local storage (TLS).
It is split into slots, which can contain arbitrary amount of data.
Each slot has a unique identifier, which can be used across threads to access TLS data from any thread of the process.
Automatic permissions inheritance
When an application process gets a new permission, all other processes from the same application inherit it, unless this permission is granted only for this instance of the application.