Operating system concept Chapter 3 —— process
A process is an executing program , It's just informal . Processes are not just program code , Program code is called a text segment （ Code segment ）, It also includes current activities , It is represented by the value of the program counter and the contents of the processor register . Besides , Processes also include process stack segments （ Temporary data 、 Function parameter 、 local variable 、 Address ） And data segment （ Including global variables . It could also include heaps （leap）, Is the dynamic allocation of memory while the process is running .
Programs are passive entities , For example, the contents of a file stored on a disk containing a series of instructions （ Executable file ）, And process is an active entity , It has a program counter that represents the next command to execute and the set of related resources .
Although two processes can be related to the same program , It's like the difference between the same user's calls web Browser copy , But they are independent processes . Although the code snippets are the same , But the data segment 、 The stack is different .
Process status :
- new ： The process is being created
- function ： Instructions are being executed
- wait for ： The process waits for an event to occur （ Such as I/O Complete or receive a signal ）
- be ready ： The process is waiting for the processor to be allocated
- End ： The process is completed and executed
Only one process can run on one processor at a time , But multiple processes can be in a ready or wait state .
Process control block
Processes use process control blocks within the operating system （process control block,PCB) To express ,PCB Contains process state 、 Program counter 、cpu register 、cpu Scheduling information 、 Memory management information 、 Billing information 、I/O Status information, etc .
Process status ： States may include new , be ready , function , wait for , Termination, etc . Program counter ： The counter indicates the address of the next instruction that the process will execute . CPU register ： With the program counter , The status information of these registers also needs to be saved in case of interrupt , So that the process can be executed correctly in the future . CPU Scheduling information ： This type of information includes process priority 、 Scheduling queue pointer and other scheduling parameters . Memory management information ： According to the memory system , This information includes the base address and the value of the limit register , Page table or segment table . Billing information ： This kind of information includes CPU Time 、 Actual usage time 、 Time limit 、 Billing data 、 Number of jobs or processes, etc . I/O State information ： This type of information contains the I/O The equipment list 、 List of open files, etc .
The goal of multiprogramming is to have processes execute at all times , So that cpu Maximum utilization rate . The purpose of time-sharing system is to quickly switch between processes cpu So that the user can interact with the program when it is running . For this purpose , Process scheduling selects an available process to cpu On the implementation . A single processor system never has more than one process executing . If there are multiple processes , Then the rest needs to wait CPU Idle and reschedule .
When a process enters the system, it is added to the job queue , This queue contains all processes in the system . Programs that reside in memory and wait to run are stored in the ready queue , The queue is usually implemented by linked list , Its head node points to the first and last of the list PCB The pointer to the block . Every PCB Include a next... Pointing to the ready queue PCB A pointer to the domain .
stay linux Every process in is made up of task_struct Data structure to define . task_struct It's what we usually say PCB, It also contains pointers to the parent and child processes . for example , The state of the process is through the structure of long state Field to represent .
stay Linux In the kernel , All activities are carried out through a process called task_struct The two-way linked list of , The kernel holds a pointer to the currently running process .
For example, the kernel wants to change the current running process status value to new_state. If current Is a pointer to the current process , be ：
`current -> state = new_state;“
The operating system also has other queues . Waiting for a specific IO The process list of a device is called a device queue . Each device has its own device queue .
The new process begins to be in the ready queue , It waits in the ready queue until it is selected for execution or dispatch . When a process is assigned to cpu Execution time , It could happen ：
a. The process sends out a IO request , And on the IO In line .
b. Process creates a new child process , And wait for it to end
c. Process forced to release due to interruption cpu, And put it back in the ready queue
For the first two cases , The process eventually switches from the waiting state to the ready state , And put it back in the ready queue . The process continues this cycle until it stops , At that point, it will be removed from all queues , Its PCB And resources will be released .
Processes migrate between various scheduling queues , To schedule , The operating system must somehow select processes from these queues . The selection of the process is determined by the corresponding The scheduler （scheduler） To perform the .
Usually in a batch system , The process is more committed , Not immediately . These processes are usually put into the disk buffer pool , In order to carry out later . Long term scheduler or Job scheduler Select the process from the pool , And load it into memory to prepare for execution . Short term scheduler or cpu The scheduler Select a process from the processes that are ready to execute , And distribute it cpu.
The main difference between the two schedulers is the frequency of scheduling .
Short term schedulers usually 100ms At least once , Due to the short time between each execution , The short-term scheduler must be fast .
Long term schedulers do not execute frequently , So the long-term scheduler can use more time to select the execution process . The degree to which long-term scheduler controls multiprogramming （ The number of processes in memory ）. The long-term scheduler must carefully select the execution process . Usually , Most processes can be divided into ：I/O Main or CPU Mainly .I/O The main process usually executes I/O Aspects take more time than performing calculations , On the other hand ,CPU The dominant process rarely produces I/O request . In order to achieve balance and better performance of the system , The long-term scheduler should choose a reasonable inclusion of ＩＯ Mainly and ｃｐｕ The main combination process is to make full use of the device and short-term scheduler .
about Linux and Windows Systems usually have no long-term scheduler , The stability of these systems depends on physical constraints , If the system performance drops a lot , There will be user exits .
Some systems, such as time-sharing systems , It is possible to introduce an interim scheduler , Its core idea is to be able to move the process out of memory , Thus reducing the degree of multiprogramming , After that, the process can be swapped in .
Interrupt to make CPU From the current task to run the kernel subroutine . When an interrupt occurs , The system needs to save the current running in CPU The context of the process in , So that the context can be restored after it has been processed . The context of the process is PCB To express . Usually by performing a state save （state save） To preserve cpu current state , Then perform a state recovery （state restore） Start running again .
take CPU Switching to another process requires saving the current state and restoring the state of the other process , This is called context switching （context switch）. When a context switch occurs , The kernel will save the state of the old process in PCB in , Then load the context of the new process scheduled to be executed and saved .
Context switching time is an additional overhead , Because the system can't do anything useful when switching . The switching time is closely related to the hardware support .
For details, see ：
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .
本文为[The struggling rabbit of florist]所创，转载请带上原文链接，感谢