Mutual exclusion and synchronization of processes and PV operations of semaphores

definition

Mutual exclusion definition of step process: One or more program segments in a group of concurrent processes must be executed in a unit that does not allow cross execution due to sharing a common resource. That is to say, mutual exclusion is to ensure that critical resources are only accessed by one process at a certain time.
Definition of process synchronization: A group of concurrent processes in an asynchronous environment send messages to each other due to direct constraints to cooperate and wait for each other, so that each process executes at a certain speed, which is called process synchronization.
That is to say, the processes are executed asynchronously, and synchronization is to make each process execute in a certain order and speed.
Mutual exclusion is a competitive relationship for resources, while synchronization is a cooperative relationship between processes.

Which resources require exclusive access

Some resources in the system can be used by multiple processes at the same time, and some resources are only allowed to be used by one process at a time. The resources that are only allowed to be used by one process at a time are called critical resources
. Processes can share resources in the system during concurrent execution. However, access to critical resources must be mutually exclusive, that is, the execution of the program that each process operates on critical resources (the program code that a process accesses critical resources is called a critical section) must also be mutually exclusive . Only in this way can we guarantee mutual exclusion of access to critical resources. Mutual
exclusion between processes can be described as: prohibiting two or more processes from simultaneously entering the critical section that accesses the same critical resource.

How the system coordinates critical sections

(1) Give in when free. When no process is in the critical area, if a process requests to enter the critical area, it will be allowed to enter immediately;
(2) If it is busy, it will wait. When an existing process enters its critical area, other processes trying to enter their critical area must wait to ensure that all processes enter the critical area exclusively; (
3) Limited waiting. When several processes request to enter the critical area, one process should enter the critical area within a limited time, that is, they should not wait for each other and no one enters the critical area; (4) Give up the right to wait
. For the process waiting to enter the critical section, the CPU it occupies must be released.

Semaphore PV operation

Semaphores can effectively implement process synchronization and mutual exclusion. In operating systems, a semaphore is an integer. When the semaphore is greater than or equal to 0, it represents the number of resource entities available for concurrent processes, and when the semaphore is less than zero, it represents the number of processes waiting to use the critical section. To establish a semaphore, it is necessary to explain the meaning of the semaphore and set the initial value, and establish the corresponding data structure so as to point to the processes waiting to use the critical section.

Both P and V operations are indivisible atomic operations, therefore, no interrupts are allowed during the execution of P and V primitives.
The process of the P (sem) operation is to subtract 1 from the value of the semaphore sem. If the value of sem becomes a negative number, the process calling the P operation will suspend execution until another process performs a V operation on the same semaphore. The process of V(sem) operation is to add 1 to the value of the semaphore sem. If the value of sem is less than or equal to 0, select a process from the corresponding queue (queue related to sem) and wake it up.

Mutual exclusion semaphore, the initial value is 1, and the synchronization semaphore is the number of all synchronization processes

Guess you like

Origin blog.csdn.net/ren365880/article/details/124956341
Recommended