Application of Sem_init(1) The semaphore is created with the Sem_init function, the following is its description: #include <semaphore.h>int sem_init (sem_t *sem, int pshared, unsigned int value);The function is to initialize the semaphore specified by the SEM, set its sharing options, and specify the initial value of an integer type. The pshared parameter controls the type of semaphore.
If the value of pshared is 0, it indicates that it is the local semaphore of the current process or
the shared semaphore of the thread Otherwise, other processes will be able to share the semaphore. value is the initial value of the semaphore(2) These two functions control the value of the semaphore, which is defined as follows: #include <semaphore.h>int sem_wait (sem_t * sem); int sem_post (sem_t * sem); Both functions are to be parameterized with a pointer to the semaphore object initialized by the Sem_init call.the role of the Sem_post function is to
add a "1"to the semaphore value, which is an "
atomic operation "that is, the same signal at the same time to do the "1" operation of two threads will not conflict;The
two programs that read, append, and write to the same file at the same time may cause conflicts. (How to handle?) The semaphore value is always correctly added to a "2"-because there are two threads trying to change it. The Sem_wait function is also an
atomic operation whose function is to
subtract a "1"from the value of the semaphore, but it will always wait until the semaphore is a non-0 value before starting the subtraction. That is , if you call Sem_wait () on a semaphore with a value of 2, the thread will continue executing and the semaphore value will be reduced to 1. If you call Sem_wait () on a semaphore with a value of 0,This function waits until another thread has added this value so that it is no longer 0 (
blocked ). If there are two threads in sem_wait () waiting for the same semaphore to become a non-0 value, then when it is added to the third thread by a "1", only one of the waiting threads can subtract the semaphore and continue execution, and the other will be in a wait state. This is the value of the semaphore, the ability to atomically test and set with only one function. There is another semaphore function sem_trywait, which is a sem_wait non-blocking partner. Sem_trywait is an immediate return function and will not be blocked by anything. Depending on its return it is worthwhile to have different information. If the return value is 0, the semaphore is greater than 0 before the function call, but it is automatically reduced by 1 after the call, and it is not known whether the call is zero after it is called. If the return value is Eagain, the semaphore count is 0. (3) Obtain the value of the signal volume SEM and save it to the Valp. The following definitions: #include <semaphore.h>int sem_getvalue (sem_t *sem, int *valp); (4) The last semaphore function is Sem_destroy. This function is used to clean up the semaphore as we run out of semaphores. The following definition: #include <semaphore.h>int sem_destroy (sem_t *sem); This function also uses a semaphore pointer to make parameters, returning all the resources that it occupies. When the semaphore is cleared, the user receives an error if the thread is waiting for it. In Linux, however, there is no resource associated with a semaphore object that needs to be freed, so in Linux, the destruction of the semaphore object is simply a test of whether the thread is wired because the semaphore is waiting. If the function returns a 0 description without, the normal logoff semaphore, if returned Ebusy, indicates also that the thread is waiting for the semaphore signal. As with other functions, these functions return "0" on success.
Number of Linux semaphores