- The relation and difference between process and program
1) Process is an instance of a program running on a computer. A process is a running activity of a program and its data on a computer. It is a running process and a dynamic concept. The running entity of the process is the program, and the process leaving the program has no meaning. A program is an ordered set of instructions, a static concept.
2) A process is an execution process of a program, which is dynamically created and dies, has a certain life cycle, and exists temporarily; while a program is a collection of a set of codes, which is permanent and can be stored for a long time. Process is a program in execution.
3) One process can execute one or several programs, and one program can also constitute multiple processes. A process can create a process, but a program cannot form a new program.
4) The composition of the process and the program is different. From a static point of view, the process is composed of three parts: program, data, and process control block (PCB). The program is an ordered set of instructions.
- Why is the process state divided into five parts
We first complete the hot task and the state analysis that the process needs to complete. Because multiple processes share system resources during concurrent execution, they show intermittent operation rules during their operation. Therefore, the process may have more than one in its life cycle. Kind of state. Generally speaking, there are three states required by a process, and each process should be in at least one of the following three basic states:
(1) Ready state. This means that the process is ready to run, that is, after the process has been allocated all necessary resources except the CPU, it can be executed immediately as long as it obtains the CPU. If there are many processes in the ready state in the system, they are usually arranged into a queue according to a certain strategy (such as a priority strategy), which is called a ready queue.
(2) Running state. This refers to the state in which the process has obtained the CPU and its program is executing. At any one time, in a single-processor system, only one process is in the execution state, while in a multi-processor system, there are multiple processes in the execution state.
(3) Block state. This refers to the state when the process being executed cannot continue execution temporarily due to an event (such as an I/O request, failure to apply for a buffer, etc.), that is, the execution of the process is blocked. At this time, process scheduling is caused, and the OS assigns the processor to another ready process, and the blocked process is in a suspended state. This suspended state is generally called a blocked state, sometimes also called a waiting state or a blocked state. Usually the system arranges processes in a blocked state into a queue, which is called a blocking queue.
Secondly, it is not enough to just prepare the state phase required by the process. If we want to run the process independently on the target of parallel execution of the program, we need to package the process into a process control block, in order to meet the process control block's data and operation integrity requirements And to enhance the flexibility of management, usually two common states are introduced for the process in the system: the creation state and the termination state.
(4) Creation status. The process is created by creation. Creating a process is a very complicated process, and it usually takes multiple steps to complete: for example, the process first applies for a blank PCB, and fills in the PCB with information for controlling and managing the process; then assigns the runtime location to the process. The necessary resources; finally, the process is transferred to the ready state and inserted into the ready queue. However, if the resources required by the process cannot be met, for example, the system does not have enough memory to make the process unable to load into it, the creation work has not been completed at this time, and the process cannot be scheduled to run, so the state of the process at this time is called Create status.
The introduction of the creation state is to ensure that the scheduling of the process must be performed after the creation work is completed to ensure the integrity of the process control block operation. At the same time, the introduction of the creation state also increases the flexibility of management. The OS can postpone the submission of a new process (the creation state) according to the system performance or main memory capacity limitations. For a process in the created state, when it has obtained the required resources and the initialization of its PCB is completed, it can be transferred from the created state to the ready state.
(5) Termination status. The termination of the process also has to go through two steps: First, it waits for the operating system to do the aftermath, and finally clears its PCB, and returns the PCB space to the system. When a process reaches its natural end point, or has an insurmountable error, or is terminated by the operating system, or is terminated by other processes with termination rights, it will enter the termination state. The process that enters the terminated state cannot be executed again, but a record is still kept in the operating system, which saves the status code and some timing statistics for other processes to collect. Once other processes have completed their information extraction, the operating system will delete the process, that is, clear its PCB, and return the blank PCB to the system.
- The content of the process control block PCB (Process Control Blok)
As a part of the process entity, PCB records all the information needed by the operating system to describe the current situation of the process and manage the operation of the process. It is the most important recorded data structure in the operating system.
The role of PCB is to make a program (including data) that cannot run independently in a multi-program environment become a basic unit that can run independently, a process that can be executed concurrently with other processes. The following is a further explanation of the specific role of PCB:
(1) As a mark of the basic unit of independent operation. When a program (including data) is configured with PCB, it means that it is already a legal basic unit that can run independently in a multi-program environment, and it also has the right to obtain OS services, such as opening files in the file system , Request for I/O devices in the system, and communicate with other related processes. Therefore, when the system creates a new process, a PCB is created for it. At the end of the process, the PCB was recycled, and the process died. The system perceives the existence of the process through the PCB. In fact, PCB has become the only sign that the process exists in the system.
(2) It can realize intermittent operation mode. In a multi-program environment, the program runs in a stop-and-go intermittent operation mode. When a process is suspended due to blocking, it must retain the CPU site information when it is running, and when it is scheduled to run again, it also needs to restore its CPU site information. With the PCB, the system can save the CPU site information in the PCB of the interrupted process for use when the process is scheduled to execute again when the CPU site is restored. From this, it can be clarified again that in a multi-program environment, as a static program in the traditional sense, it does not have the means to protect or protect its own operation site, and cannot guarantee the reproducibility of its operation results, thus losing its operation. Meaning.
(3) Provide information needed for process management. When the scheduler schedules a certain process to run, it can only find the corresponding program and data according to the rent and the start address of the data in the internal or external memory of the progressive nan searched by the gate: in the process of running, when you need to access files or IO devices in the file system, you also need to rely on the information in the PCB. In addition, according to the resource list in the PCB, all the resources required for the process can be learned. It can be seen that throughout the life of a process, the operating system always controls and manages the process according to the PCB.
(4) Provide information needed for process scheduling. Only processes in the ready state can be scheduled for execution, and the PCB provides information about what state the process is in. If the process is in the ready state, the system inserts it into the process list, waiting for the scheduler to schedule: In addition, it is often necessary to know other information about the process when scheduling, such as in the priority scheduling algorithm. To know the priority of the process. In some fairer scheduling algorithms, the body needs to know the waiting time of the process and the time it has been executed. ,
(5) Realize synchronization and communication with other processes. The process synchronization mechanism is used to realize the coordinated operation of various processes. When the semaphore mechanism is adopted, it requires that a corresponding semaphore for synchronization is set in each process. The PCB also has an area or communication queue pointer for process communication.