Original address of this article: https://is.daryl.moe/2017/12/
A process is an executing program instance, which includes program counter, register and current value of variable. When a program is running, its logic counter is loaded into the program counter of CPU; when a program is paused, the program counter of CPU is saved in the logic program counter of memory, and the CPU is not occupied before the next run.
It should be noted that the process is not always running in the CPU, and the CPU will switch back and forth between processes, so the speed of each process is uncertain. Therefore, most processes are not affected by CPU multiprogramming or the relative speed of other processes.
Process creation and termination
There are four situations that lead to the creation of processes: system initialization, system call of creating process executed by running program, user request to create a new process, and initialization of a batch job. Take Linux for example. The first process when Linux starts is process 0, which is the ancestor of all processes. Next is process 1, which is the ancestor of all user processes.
We all know nginx. When we start nginx, it has been silently listening to the port to execute web services, but we have no awareness. Such processes are daemons.
Any process can create a new process. This process is a sub process, which executes
forkSystem call. A process can fork a child process, and the child process shares the data of the parent process. However, any modification made by the child process to the data is invisible to the parent process. The memory address space of the child process is a copy of the parent process, which is a different address space.
A process can only have one parent, but a process can have more than one child. In UNIX, a process cannot deprive its children of inheritance.
Writable space is not shared, and shared space is not writable. The child process can share the memory space of the parent process, but the share is copied at write time. That is to say, when modifying part of memory, first copy it explicitly to ensure that it occurs in the private memory area.
Process termination is usually caused by the following conditions: normal exit, error exit, serious error, being killed by other processes. The latter two are involuntary. In Linux, the voluntary termination of a process can be achieved by calling
exitSystem call complete.
Status of the process
A process has three states: run, block, and ready.
The process in running state actually occupies CPU at this time.
The process in ready state is ready to run, but it is suspended by the operating system because other processes are running.
The process in the blocking state is suspended because it calls the locally blocked system call. The process in blocking state does not have the conditions to run. Only when some external practice occurs, such as the local blocking call is completed, can it be converted to ready state and wait for the operating system to schedule.
The operating system maintains this process table, and the details of each process are saved in this process table. Including program counter, stack pointer, memory allocation, file opening, interrupt vector, etc.
As mentioned earlier, each process is not running in the CPU all the time. The ready state and blocking state are non operating states. When the operating system switches the running process to ready state, or the process enters blocking state because of calling local blocking system call, its running information is saved in the process table. When the operating system switches the process back to the running state, the information in the process table is recovered as if the process had not been interrupted.
Take io for example. When a process a calls the system call of network IO, because it is blocked during the system call, the operating system switches it to blocking state, saves its site in the process table, and saves the address and interrupt vector mapping. Then, switch the B process. After a period of time, it may be that the C process is running in the CPU. If the IO called by the a process is completed, an interrupt occurs to the hardware. At this time, the operating system will interrupt the running C process. At the same time, it will find the site of a process in the process table through the interrupt vector and recover to the state when a process is not interrupted, and continue to run.
A process, in its life cycle, will be interrupted thousands of times. Every time the operation is resumed after interruption, it will return to the state when it was not interrupted.
Misunderstandings about slow progress
Here is just my understanding. At work, I overheard my colleagues saying this, which roughly means that when a process requests from the network, it will affect the speed of other processes, causing other processes to slow down. This is not right. A process is interrupted when it encounters a local blocking call. At this time, its site is saved in the process table, and the CPU runs other processes. There is no possibility that one process will cause other processes to run slower. There is only a case that scheduling will slow down due to the number of processes. They are different.
This paper is a summary and understanding of the author’s reading of modern operating system, which has been read in the future. At the same time, I would like to share this knowledge with you. Due to the limited level of the author, I would like to thank you for any mistakes.