The concept of the semaphore is first made by E.W.DIJKSTRA in 1965. Semaphore is an integer variable greater than equal to zero. There are two atomic operations for the signal: - and , down () and Up (), SLEEP () and Wakeup (), P () and V (), WAIT () and Signal (). Although their names are different, it is conceivable to describe the same, DOWN and UP operations. DOWN (T) The value of the selection of the signal is: first check if the T is greater than 0, if t is more than 0 t minus 1; if the value is 0, the process enters the sleep state, and the DOWN operation at this time is not end. This is an atomic operation to ensure that the amount of processes are not allowed to access the signal before the operation of one semaphore is started. UP (T) operation The value of the quantity T is: If one or more processes sleep on the semaphore, a previous DOWN operation cannot be completed, and one of the system selects one and allows the DOWN operation to accomplish it. . Therefore, after the amount of time the signal is executed in the amount of sleep, the value of the signal is still 0, but the process of sleep is less. The value of increment semaphore and the operation of waking up a process is also atomic operations, which are indisparable. This is guaranteed that there is no process is blocked because of the execution of UP operations. Below is an example of the manufacturer and consumer of the semaphore. #define N 100 / * number of slots in the buffer * / typedef int semaphore; semaphore mutex = 1; semaphore empty = N; semaphore full = 0; void producer (void) {int item; while (TRUE) {produce_item (& item ); Down (& EMPTY); DOWN (& MUTEX); Enter_Item (item); UP (& Mutex); UP (& Full);}} void consumer (void) {int item; while (true) {down (& full); Down & mutex); Remove_Item (& item); UP (& Mutex); UP (& Empty); Consume_Item (item);} The simplest synchronization mechanism in multi-threaded program is MUTEX (mutually exclusive). A MUTEX is also a semaphore, which only provides two basic operations: Down and Up. Once a thread calls Down, other threads will be blocked when the DOWN is called. When this thread calls the UP, it has just blocked in the thread in the Down, there will be one and only one waken.