Signal Volume (SEM)

Source: Internet
Author: User
Tags semaphore

First, the signal volume: (Data operation Lock) control inter-process mutual exclusion, synchronization, etc., coordinate multiple programs to access a shared resource at the same time.

How it works: The semaphore can only have two operation waits and operation, namely: P v operation, must be atomic operation.

P (SV): If the value of SV is greater than 0 minus one, if its value is zero, it hangs;

V (SV): If there is a suspended, resume operation, if not, add one.

Second, the function prototype:

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

int semop (int semid,struct sembuf *sops,size_t nsops);

int semctl (int semid, int semnum, int cmd, ...);

/*

* Semid: identifier of the semaphore set; Semnum: Number of semaphores;

* cmd: The command to execute, depending on the command, the function has three or four parameters (Union)

*/

Cmd:ipc_rmid, immediately delete the signal set, wake up the blocked process;

Cmd:setval, sets the value of a single semaphore in the semaphore set.

The Cmd:getall is used to read the values of all semaphores in the semaphore set.

Union Semun

{

int Val;

struct Semid_ds *buf;

unsigned short *array;

struct Seminfo *_buf;

};//users need to define their own claims


Third, the code implementation:



Iv. Results of implementation:






Signal Volume (SEM)

Related Article

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.