[Embedded General Review] Definition of PV Operation

Definition of PV operation

A semaphore is a special type of variable whose access by the program is an atomic operation, and only P (signal variable) and V (signal variable) operations are allowed on it.

The PV operation must be an atomic operation: the essence of the semaphore is to protect critical resources. It is impossible to use the semaphore to protect the semaphore, so the PV operation of the semaphore must be an atomic operation.

P operation

We call the application semaphore P operation

The essence of applying for a semaphore is to apply for permission to use a certain resource in the critical resource. When the application is successful, the number of resources in the critical resource should be reduced by one, so the essence of the P operation is to reduce the counter by one.

V operation

We refer to releasing the semaphore as the V operation

The essence of releasing the semaphore is to return the right to use a certain resource in the critical resource. When the release is successful, the number of resources in the critical resource should be increased by one, so the essence of the V operation is to increase the counter by one.

binary signal

The value is only "0" or "1", which is mainly used to realize mutual exclusion;

general semaphore

The initial value is the total number of available physical resources, which is used for collaborative synchronization between processes;

A semaphore may be initialized to a non-negative integer;

  1. The sem_wait operation (P operation) decrements the semaphore by one. If the value is negative, the process executing sem_wait is blocked; otherwise, the process continues to execute;
  2. The sem_post operation (V operation) increments the semaphore by one. If the value is less than or equal to zero, unblock the process blocked by the sem_wait operation;

The P operation is responsible for allocating resources, and no resources are waiting to enter the blocking queue; the V operation is responsible for releasing resources, and wakes up a process to enter the critical section when the blocking queue is not empty

Guess you like

Origin blog.csdn.net/weixin_51911075/article/details/128154429