In Programming, What Is a Semaphore?
Semaphores, sometimes called semaphores, are a facility used in a multi-threaded environment and can be used to ensure that two or more critical code segments are not called concurrently. The thread must acquire a semaphore before entering a critical code segment; once the critical code segment is complete, the thread must release the semaphore. Other threads that want to enter this critical code segment must wait until the first thread releases the semaphore. To complete this process, you need to create a semaphore VI, and then place the Acquire Semaphore VI and Release Semaphore VI at the beginning and end of each key code segment. Verify that these semaphore VIs reference the semaphore that was originally created.
signal
- Semaphores, sometimes called semaphores, are a facility used in a multi-threaded environment and can be used to ensure that two or more critical code segments are not blocked by
- Take the operation of a parking lot as an example. For simplicity, assume that there are only three parking spaces in the parking lot, and all three parking spaces are empty at first. At this time, if five vehicles come at the same time, the gatekeeper allows three of them to enter directly, then put down the barrier, and the remaining cars must wait at the entrance, and subsequent cars have to wait at the entrance. At this time, a car left the parking lot. After the gatekeeper learned about it, he opened the barrier and put one outside. If he left two more cars, he could put two more, so back and forth.
- In this parking system, parking spaces are public resources, and each car is like a
- In abstract terms, the characteristics of a semaphore are as follows: A semaphore is a non-negative integer (the number of cars).
- There are 4 operations on semaphores (#include <semaphore.h>):
- 1. Initialize (also called create) int sem_init (sem_t * sem, int pshared, unsigned int value);
- 2. Wait for the signal (wait), also known as suspend (suspend) int sem_wait (sem_t * sem);
- 3. Signal or post int sem_post (sem_t * sem);
- 4. Destroying (int) sem_destory (sem_t * sem); [1]
- with
- The following example demonstrates the
- In 1965, the semaphore (Semaphores) mechanism proposed by Dutch scholar Edsger Dijkstra was a very effective process synchronization tool. In the long-term wide application, the semaphore mechanism has greatly developed. The semaphore has developed into a "semaphore set mechanism". Now the semaphore mechanism has been widely used in uniprocessor and multiprocessor systems and computer networks.