Process
A program is a passive set of machine code instructions and data stored in an executable image. A process can be thought of as this passive program in action.
Processes are independent separate tasks. If one process crashed, it will not affect any other processes. The end goal of processes is to achieve the illusion of having multiple CPUs when executing multiple programs. This is done by virtualization.
Machine state
Representing the execution of a program, a process includes the state of the program first and foremost. This includes
- The program counter
- CPU registers
- The process stack
- The address space
- etc.
Beyond the state of the program, each process is allocated its own process identifier (PID) and virtual memory among other things.
Virtualization
The virtualization of CPU's is what allows more processes than CPU to exist. This is implemented by low-level time-sharing mechanisms. On top of these mechanisms resides scheduling policies, which decides which program should run.
API
Typical process APIs include the following:
- Create
- Destroy - Killing processes forcefully
- Wait - Suspend a process to be restarted later
- Status - Obtain current information of the process
There are many other misc. controls that are possible.
Process creation
- Code and static data are loaded into memory. In modern operating systems, this is done lazily.
- Allocate memory such as runtime stack and heap
- Perform initialization tasks, especially those related to I/O.
- Start the program
Process state
A process has three states
- Running - Its instructions are currently being executed by the CPU
- Ready - Ready to run but OS is not running it for misc. reasons (such as time-sharing)
- Blocked - Waiting some I/O to finish, such as requesting a write to disk.
A process is scheduled when the OS moves it from Ready to Running. It is descheduled when it is moved from Running to Ready. This decision is made by the scheduler.
Processes in Linux
Init process
When the computer starts, the only thing the kernel execute in the user space is a init command (shown with ps -f 1). This process is allocated a PID of 1 and is the only process with no parent process.
Creating a process
In contrast to the init process, all other processes are created by parent processes. This is done with fork and exec. First, the shell will run fork to create a cloned process from the current process, and exec replaces the program in the cloned process with another program.
Sources
- Linux documentation: https://tldp.org/LDP/tlk/tlk.html
- Linux processes, init, fork/exec, ps, kill, fg, bg, jobs: https://www.youtube.com/watch?v=TJzltwv7jJs
- Operating Systems, Three Easy Pieces