preface

Your process, why did it hang up? The process hangs, which is a familiar problem. After learning this, you will have some idea of the process. You will soon find a solution to the problem of process hanging.

Processes are an important concept in operating systems. You are familiar with the operating system, can avoid some pitfalls, and write high-quality code.

The Windows interface is cool, not to mention. The following animation and text, to tell you about.

Why processes are needed

In general, programs cannot be executed concurrently, because the results of concurrent execution of programs are not reproducible. To enable programs to be executed concurrently, described and controlled, the concept of a process was introduced.

The operating system can only run one program at a time unless it introduces a process.

Characteristics and definitions of processes

A process is an execution of a program and an independent unit of the system for resource allocation and scheduling.

To enable the program to run independently, a process control block PCB should be configured. The process is composed of three parts: program segment, related data segment and PCB (process control block).

Dynamic: a process is an execution of a program. It is created, executed by scheduling, and eliminated by cancellation. The process has a certain life cycle.

Concurrency: Multiple process entities that exist in memory and can run simultaneously for a period of time.

Independence: Process entity, a basic unit that can operate independently, allocate resources independently, and receive scheduling independently. Asynchrony: Processes advance at their own independent and unpredictable rate.

Below the animation, compare the villain to the process, show the process of creation, destruction, dynamic, concurrent, independent, asynchronous.

Process status and transitions

Create: Ensure that the process scheduling, must be created after the completion of the work, before proceeding. Ensure the integrity of PCB operation on process control block.

Ready: The process has allocated all necessary resources except CPU.

Execution: The process has acquired the CPU and its instruction set is executing.

Blocked/suspended: A running process cannot continue executing temporarily due to an event.

Terminate: After other processes finish collecting information, the process is deleted, the PCB is cleared, and the process is returned to the system.

Here is the process status diagram:

PCB process control block

The signature of a base unit running independently: create a PCB when creating a process, return to the PCB when the process ends, and the process dies. The system is aware of the presence of the process through the PCB. The PCB has become the only indication that a process exists in the system.

Implement intermittent running mode: when the process is suspended running, it must retain relevant information such as the CPU at the time of running. When a process is running again, you need to restore CPU information.

Provides information needed for process management: When the process starts to run, the corresponding program and data can be found according to the starting address pointer in memory or external storage according to the program and data recorded in the PCB of the process.

Provides information needed for process scheduling: only processes in the ready state can be scheduled. The state of the process is recorded in the PCB, along with other information such as priority, wait time, execution time, and so on.

Synchronization and communication with other processes: Process synchronization mechanism, used to achieve coordinated operation of multiple processes. In the PCB, there are areas or communication queue Pointers to achieve process communication.

Information in PCB process control block: information in PCB can be roughly divided into four categories: process identifier, CPU status, scheduling information and control information.

Process identifier: separate external identifier and internal identifier. The external identifier is the process name, which can be specified by the parent process and usually consists of characters and numbers. Internal identifier, a unique process ID provided by the operating system.

CPU status: is mainly composed of various kinds of register contents, such as general-purpose registers, instruction counter (the address of the next instruction), the program state (state information, the condition code, implementation methods, such as shielding the interrupt flag) and stack pointer (pointing to hold process and system call parameters and invoke the address of the system of the stack stack).

Scheduling information: contains process status, process priority, other information, and events (blocking cause).

The process status is the basis for process scheduling and swapping. The process with a higher priority is preferentially executed by the CPU.

Information necessary to control a process, including the storage addresses of programs and data, so that when the process is scheduled to execute, its programs and data can be found in the PCB, process synchronization and communication mechanisms, such as message queues, semaphores, etc.

Process creation and termination

Process creation: Apply for a blank PCB and process ID from the operating system, allocate resources required for running, initialize the PCB, and wait for the process to be inserted into the ready queue.

Resources can be obtained from the operating system or parent process. The required resources must be informed in advance so that the operating system or parent process can allocate resources to the system.

At least two kinds of information need to be initialized on the PCB. 1. Identification information, that is, the process ID and parent process ID need to be filled in the PCB control block. State information, the instruction counter points to the entry address of the program, the stack pointer points to the top of the stack control information.

Process termination includes reading process state, terminating process, terminating descendant process, releasing resources, and removing PCB queue. The operating system retrieves the PCB of the process from the PCB collection by process ID and reads the state of the process from it.

If the process is in the running state, the execution of the process is terminated and the scheduling flag bit true is reset. If the process has descendant processes, all descendant processes are terminated to prevent the descendant processes from becoming zombie processes.

Resources are then released and returned to the operating system or parent process. The final step is to remove the PCB queue and wait for other processes to collect information.

Process blocking and wake up events 1. When the system service is requested but not met, such as asking the system to print a request.

2. If the started operation needs to be synchronized: For example, the operation and the process that requests the operation need to be synchronized.

3. New data has not arrived: If process A writes data and process B reads data, process A does not write data and process B cannot read data.

4. There’s no new job.

Suspend and activate a process 1. The suspend process is done by the process itself or by its parent process suspend primitive. Move the process PCB to the specified area, note the state change, possibly rescheduling.

2. Activate the active primitive to activate the process. The activation primitive calls a process from external memory into memory, checks the current state of the process, and acts accordingly.

Process synchronization

Animation shows that a critical section of resources can only be used by one process at a time. Once there is sharing of critical resources, it will inevitably involve competition limitation. A critical resource is a common resource or shared data that can be used by multiple threads. But only one thread can use it at a time. Once a critical resource is occupied, other threads must wait to use the resource.

The main task of process synchronization is to coordinate the execution sequence of multiple related processes, so that the concurrent execution of various processes can effectively share resources and cooperate with each other, so that the execution of the program, with reproducibility.

Critical section With the concept of critical resources, it is easy to understand the concept of critical section. In the program, all operations are performed by code, and the section of code that accesses critical resources is the critical section

Dealing with constraints caused by competing or cooperative dependencies Free advance: For critical resources, if free is not being used, anyone who comes can use it

Busy waiting: If a critical resource is being used, then other latecomers need to wait.

Finite wait: processes that require access to critical resources should be able to enter their own critical sections for a finite amount of time, and not wait stupidly themselves

Let the right to wait: if you can not enter their own critical area, should immediately release the processor, and can not occupy the CPU dead, you die and so on, others can not use.

The lock

A lock is a control over a resource, and a lock is a control. When a critical section is entered, it is called acquiring a lock, and once the lock is acquired, the critical resource can be accessed.

Other threads need to acquire the lock in order to enter the critical section. When the current thread terminates, the lock is released, and other threads can acquire the lock on the resource.

Deadlock A lock represents a control, access to a critical resource.

The following animation shows that two small people, both use resources 1 and 2, in order to reach the opposite side. The villain on the left possesses resource 1 and the villain on the right possesses resource 2. A deadlock occurs when they seize the current resource and then attempt to acquire the other resource.

If there is more than one critical resource, it is possible that two critical resources, A and B, need to be accessed successively. Thread1 has acquired the lock of thread A and is waiting for the lock of resource B, but thread2 has acquired the lock of resource B and is waiting for the lock of resource A.

The AND type semaphore mechanism is used to solve the synchronization problem under multiple shared resources. Allocate all resources required by a process during the entire running process to the process at one time and release them when the process is used up.

As long as one resource is not allocated to the process, all other resources that might be allocated to it are not allocated to it. That is, the allocation of several critical resources is atomic: it either allocates all of the resources it requests to the process, or it allocates none at all.

Interprocess communication

If two processes want to know what each other is doing or coordinate, they need interprocess communication. The following describes common interprocess communication methods.

Nameless pipe: A pipe is a half-duplex communication mode. Data can only flow in one direction and can only be used between related processes. The relationship between processes usually refers to the relationship between father and child processes.

Named pipes: A named pipe is also a half-duplex communication mode, but it allows communication between unrelated processes.

Message queues: Message queues are linked lists of messages stored in the kernel and identified by message queue identifiers. It overcomes the characteristics of little signal transfer, unformatted character stream and limited buffer size.

Semaphore: A counter that can be used to control access to shared memory by multiple processes. As a locking mechanism, it prevents other processes from accessing a shared resource when a process is accessing the same resource, resulting in resource preemption.

Signal: A more complex form of communication used to notify and receive processes of the occurrence of an event.

Shared memory: maps a segment of memory that can be accessed by other processes. This segment of shared memory is created by one process but can be accessed by multiple processes.

Socket: An interprocess communication mechanism that, unlike other communication mechanisms, can be used for process communication between different machines.

omg

The above introduces some basic knowledge of process, hope to help you. Process, as an important concept in operating system, will be involved in both work and interview.

Enjoy your study!

Wechat search [Chenmeng Siyu] focus on this different programmer. Author introduction: CSDN top40, original article 1000+, senior /Golang/C++ development, now working in a well-known Internet factory. This account shares backstage development technology, including algorithm and data structure, written test/interview, network programming, database theory, operating system, IT essays