Operating system~The concept and implementation of process synchronization and process mutual exclusion

What is process synchronization

Knowledge point review: Process is characterized by asynchrony.
Asynchrony means that each concurrently executing process advances at an independent and unpredictable speed.
Insert picture description here
The read process and the write process run concurrently. Since concurrency inevitably leads to asynchrony, the order of execution of the two operations "write data" and "read data" is uncertain.
In actual applications, it must be executed in the order of "write data→read data".
How to solve this asynchronous problem is what is discussed in "process synchronization".
Synchronization is also known as direct control relationship. It refers to two or more processes established to complete a certain task. These processes need to coordinate their work order in certain positions and produce a control relationship. The direct restrictive relationship between processes stems from their mutual cooperation.

What is process mutual exclusion

The "concurrency" of the process needs the support of "sharing". Each concurrently executing process inevitably needs to share some system resources (such as memory, or l/o devices such as printers and cameras)

We call the resources that are only allowed to be used by one process in a period of time as critical resources. Many physical devices (such as camera printers) are critical resources. In addition, many variables, data, memory buffers, etc. are all critical resources.

Access to critical resources must be mutually exclusive.
Mutually exclusive, also known as indirect restriction relationship. Process mutual exclusion means that when a process accesses a critical resource, another process that wants to access the critical resource must wait.
The access of the process currently accessing the critical resource ends. After the resource is released, another process can access the critical resource.
Insert picture description here
In order to achieve mutually exclusive access to critical resources while ensuring the overall performance of the system, the following principles need to be followed:
1. Let in idle. When the critical section is free, a process requesting to enter the critical section can be allowed to enter the critical section immediately;
2. Waiting when busy. When an existing process enters the critical section, other processes trying to enter the critical section must wait;
3. Limited wait. For the process requesting access, it should be ensured that it can enter the critical zone within a limited time (to ensure that it will not be starved);
4. Give the power to wait. When the process cannot enter the critical section, the processor should be released immediately to prevent the process from waiting busy.

Process synchronization implementation

1, the critical area (Critical Section): serial multi-threaded access to public resources or through a piece of code, speed control for data access.

Advantages : An easy way to ensure that only one thread can access the data at a certain time

Disadvantages : Although the critical section synchronization speed is very fast, it can only be used to synchronize threads in this process, and cannot be used to synchronize threads in multiple processes.

2, mutex (Mutex): to coordinate common for individual access to a shared resource and design.

Mutex is very similar to critical section, and more complex than critical section. There is only one mutex, and only the thread that owns the mutex has the authority to access resources.

Advantages : The use of mutual exclusion can not only realize the safe sharing of resources in different threads of the same application, but also realize the safe sharing of resources between threads of different applications.

Disadvantages : ① The mutex can be named, which means it can be used across processes, so creating a mutex requires more resources, so if it is only used within the process, using the critical section will bring speed Advantages and can reduce resource usage. Because the mutex is a cross-process mutex once it is created, it can be opened by name.

②Using mutexes can specify that resources are used exclusively, but if one of the following situations cannot be handled through mutexes, for example, a user now purchases a database system with three concurrent access permissions, which can be based on the user The number of access licenses purchased determines how many threads/processes can perform database operations at the same time. At this time, if you use mutex, there is no way to fulfill this requirement. The semaphore object can be said to be a resource counter.

3, the semaphore (Semaphore): control with a limited number of users and resources designed. It allows multiple threads to access the same resource at the same time, but it needs to limit the maximum number of threads that can access this resource at the same time. Mutex is a special case of semaphore. When the maximum number of resources of the semaphore=1, it is a mutex.

Advantages : suitable for synchronization of threads in Socket (socket) programs. (For example, the HTTP server on the network should limit the number of users accessing the same page at the same time. Only threads that are not greater than the set maximum number of users can access, and other access attempts are suspended. It is possible to enter after the user exits the visit to this page.)

Disadvantages : ① The semaphore mechanism must have public memory and cannot be used in a distributed operating system. This is its biggest weakness;

② The semaphore mechanism is powerful, but the operation of the semaphore is scattered during use, and it is difficult to control, and it is difficult to read, write and maintain, which increases the coding burden of the programmer;

③The core operation PV is scattered in the code of each user program, which is not easy to control and manage. Once an error occurs, the consequences are serious and it is not easy to find and correct.

4, the event (Event): used to notify the thread has some event has occurred, thereby initiating the start of the successor task.

Advantages : The event object maintains thread synchronization by means of notification operations, and can realize thread synchronization operations in different processes.

Implementation of process mutual exclusion

  1. The interrupt shielding method is
    realized by using "on/off interrupt instruction" (the same as the original realization idea, that is, it is not allowed to be interrupted when a process starts to access the critical area until the end of the access, so process switching cannot occur, so it is impossible The situation where two threads access the critical section at the same time)
    Insert picture description here
    Advantages : simple and efficient
    Disadvantages : not suitable for multiprocessors; only suitable for operating system kernel processes, not suitable for user processes (because the on/off interrupt command can only run in the kernel If this group of instructions can be used by users at will, it would be very dangerous)
  2. The TestAndSet instruction is
    referred to as the TS instruction. In some places, it is also called the TestAndSetLock instruction, or the TSL instruction. The
    TSL instruction is implemented by hardware. The execution process is not allowed to be interrupted and can only be done in one go.
    Insert picture description here
    If the lock is false at the beginning, the old value returned by TSL is false, and the while loop condition is not satisfied, skip the loop directly and enter the critical section. If the lock is true at the beginning, the value returned by old after TLS is true, and the while loop condition is met, it will continue to loop until the process currently accessing the critical section is "unlocked" in the exit section.
    Compared with the software implementation method, the TSL instruction turns the hardware method of "locking" and "checking" operations into atomic operations in one go.
    Advantages : simple implementation, no need to strictly check whether there are logic loopholes like the software implementation method; suitable for
    Disadvantages of the multi-processor environment : do not meet the principle of "giving power to wait", and the process that cannot enter the critical section temporarily will occupy the CPU and execute TSL instructions cyclically, resulting in "busy waiting".

Guess you like

Origin blog.csdn.net/Shangxingya/article/details/113799732