In the integer semaphore mechanism, the semaphore is defined as an shaping variable. In addition to the initial barbarian, only two standard atomic operations Wait (s) and signal (s) are accessible. They are usually referred to as P, v operations.
The description is as follows:
P Operation: s=s-1; if S is less than 0, the process goes into a wait state, otherwise it continues execution.
V Operation: s=s+1; if s>=0, a wait process in the wait queue is awakened.
The semaphore has its own physical meaning: when s>0, its value represents the number of classes of resources to be managed, and when s<0, its absolute values represent the number of processes waiting in the related queue.
Synchronization of processes
In general, the speed with which a process runs relative to another process is uncertain, that is, the process runs in an asynchronous environment, and each process advances at its own independent, unpredictable speed. However, the process of co-operation needs to coordinate their work at certain points, and when a process reaches these points, it will have to stop waiting for the operation to end unless another process has already done something. This is the synchronization of the process.
Mutex for process
In a multi-channel program system, each process can share various resources, but some resources can only be used by one process at a time, which is called critical resources.
The management principle of the critical zone: there is no time to enter, no empty then, limited waiting, let right wait.
When the process is mutually exclusive, the initial value of the semaphore is 1, while in the case of synchronization, the initial value of the semaphore is 0.
In solving the specific problems, in the face of various concurrent processes, we should first analyze which are mutually exclusive relationships, which are synchronous relationships, thereby determining which semaphores should be set and their initial value.
If the semaphore is set, each related process will execute P operation on it, and V operation on it, it is called a common semaphore. The common semaphore is used for mutual exclusion.
If the semaphore is set, only one process can perform p operations on it, and the other process can only perform v operations on it, it is called a private semaphore for that process. The amount of semaphores used for synchronization or resource allocation management is a private semaphore.
Integer semaphore and PV operation (computer operating system)