Definition: Protect shared resources so that only one process (thread) owns the resource at a time
Principle: When the semaphore value is positive, it means it is idle, if it is 0 or negative, it means it is occupied
Classification: kernel semaphore and user semaphore. User semaphore is divided into POXIS semaphore and SYSTEMV semaphore. POXIS semaphore is divided into named semaphore and unnamed semaphore.
Kernel semaphore:
#include
void sema_init(struct semaphore *sem, int val);
void init_MUTEX(struct semaphore *sem); //Initial value 1
void init_MUTEX_LOCKED(struct semaphore *sem); //Initial value 0
void down(struct semaphore *sem); //Can sleep
int down_interruptible(struct semaphore *sem); //Can be interrupted
int down_trylock(struct semaphore *sem); //m non-blocking
void up(struct semaphore *sem);
SYSTEMV semaphore:
include
##int sem_post(sem_t *sem);
int sem_close(sem_t *sem);
int sem_unlink(const char *name);
Each The position of open requires close and unlink, but only the last executed unlink takes effect
The above is the detailed content of Detailed explanation of various types of semaphores in Linux. For more information, please follow other related articles on the PHP Chinese website!