Inter-process Communication _ semaphore
Signal volume (known: semaphore) and other processes of communication between the same way, the main purpose is ** protect critical resources ** . The process can determine, based on it, whether certain shared resources can be accessed. In addition to access control, it can also be used for ** process synchronization ** .
Classification
Two value signal: The semaphore can only take 0 or 1, similar to the mutex. But the two are different: traffic lights emphasize sharing resources, as long as the shared resources are available, other processes can also modify the value of the semaphore; The mutex emphasizes the process, and the resource-hogging process uses the resource and must be unlocked by the process itself.
** count Lights: ** semaphore can take any non-negative value.
** Create/Open ** Span class= "token lf" style= "outline:none!important" >
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
int Semget (key_t key,int nests,int SEMFLG)
< Span class= "token MD md-strong" style= "outline:none!important" >** key:** key value, obtained by Ftok.
** nsems:** specifies open or newly created ** semaphore set ** will contain the number of semaphores included.
** semflg:** flag, with Message Queuing.
** operation **
int semop (int semid,struct sembuf *sops,unsigned nsops )
< Span class= "token MD md-strong" style= "outline:none!important" >** function: **
** semid:** The ID of the semaphore set.
** sops:** is an array of operations that indicates what to do.
* *nsops:* *The number of elements of the array to which sops points.
* * Operation * *
struct sembuf {< /span> unsigned short em_num; short Sem_op; short SEM_FLG; }
SEM_FLG: signal operation flag, there may be two options:
Ipc_nowait: When the signal operation is not satisfied, SEMIOP () does not block and returns immediately, while setting the error message.
Ipc_undo: The signal is released at the end of the program, and this is done to prevent the program from unlocking the locked resource at the end of an exceptional situation, causing the resource to be locked forever.
Markdown Editor Server There is a problem, write something not to send up.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linux_c Development (5-6) interprocess communication _ Semaphore