XSI inter-process communication-----signal Volume

Source: Internet
Author: User
Tags semaphore

1. Basic Features

1) equivalent to a counter that restricts access to limited shared resources by multiple processes.

2) multiple processes get the operating mode of limited shared resources

A. Test the semaphore that controls the resource;

B. If the semaphore is greater than 0, the process can use the resource, and in order to indicate that the process has obtained the resource, the semaphore needs to be reduced by 1;

C. If the semaphore equals 0, the process sleeps waiting for the resource until the semaphore is greater than 0and the process is awakened, performing step A ;

D. When a process no longer uses the resource, the semaphore increases by 1, and other processes that are sleeping waiting for the resource are awakened.


2. Common Functions

1) create / get semaphore

int Semget (key_t key, int nsems, int semflg);

A. the function creates a semaphore set with the key parameter for the value (the Nsems parameter represents the number of semaphores in the collection , or gets the existing set of semaphores (Nsems take 0) .

B. SEMFLG Value:
0-Gets, does not exist or fails.
Ipc_creat-Creates, does not exist that is created, already exists that is acquired, unless ...
IPC_EXCL-exclusion, which already exists that fails.
C. Successful return semaphore set identity, failure return-1.

2) operating signal volume

int semop (int semid, struct sembuf* sops,unsigned nsops); <span style= "font-family: Song body; font-size:12px; " > </span>
struct SEMBUF {    unsigned short sem_num;//Signal Volume subscript short          sem_op;  Operand short          sem_flg;//Operation tag};

A. In the set of semaphores identified by the semid parameter, the function that is pointed to by the SOPs parameter contains nsops Element , each element in the struct array, in turn, performs the following actions:

a) if sem_op is greater than 0, add it to section sem_num value of the semaphore to indicate the release of the resource;

   b)   sem_op less than 0 sem_num

   c)   if sem_num ( semaphore cannot be negative "   this function blocks, Until the semaphore is reduced enough,

   d)   sem_flg contains ipc_nowait sem_num   this function does not block, but instead returns -1 errno eagain

   e)   sem_op equals 0 sem_num 0   unless sem_flg contains ipc_nowait bit.

B. successful return 0, failure return -1.


3) destruction / Control Signal Volume




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Original address: http://blog.csdn.net/meetings/article/details/47191957

XSI inter-process communication-----signal Volume

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.