Linux_c Development (5-6) interprocess communication _ Semaphore

Source: Internet
Author: User
Tags mutex semaphore

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.