interprocess Communication-Semaphore

Source: Internet
Author: User
Tags semaphore

Semaphore: It describes the current quantity of a resource. It is intended for inter-process synchronization, which is equivalent to a counter, which itself does not have the function of data exchange, but rather by controlling other communication resources to achieve interprocess communication.

When a process to access critical resources, the first to carry out the P operation request resources, when greater than 0 o'clock, the resource can request, after the application, if 0, the process is suspended, when the process no longer accesses the semaphore control of the resource, the V operation, if there is a process hangs, wake the waiting process to resume access, if there is no suspended process Just add 1 to it.

Inter-Process synchronization: Access to critical resources is sequential, one execution is complete, and the other can be executed.

Note: The operation of the Semaphore is atomic operation, because the main function of the semaphore is to maintain the mutual exclusion of resources or multi-process synchronization access, and in the initialization and creation of the semaphore can not guarantee its atomic operation.

To create the semaphore: semget (key_t key,int nsems,int semflg);

P,v Operation:

SEMOP (int semid,struct sembuf* buf,unsigned nsops);//p/v operation on the number of semaphores on the current semaphore set

Release semaphore: semctl (int semid, int semnue,int cmd)//CMD instruction operation on the number of semaphores on the current semaphore set.

Union Semun {

int Val; The value used

struct Semid_ds *buf; Ipc_stat, Ipc_set use buffer

unsigned short *array; GETALL, SETALL used array

struct Seminfo *__buf; Ipc_info (Linux-specific) using buffers

};

Comm.h

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/7F/0E/wKiom1cQ9AmhWFNmAABzmsTGuYE617.png "title=" QQ picture 20160415212416.png "alt=" Wkiom1cq9amhwfnmaabzmstguye617.png "/>

Comm.c

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/0B/wKioL1cQ9PaBEQxGAABcHci9iZ4184.png "style=" float: none; "title=" Comm.c1.png "alt=" Wkiol1cq9pabeqxgaabchci9iz4184.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/0B/wKioL1cQ9PfBNgMaAABQOLEecDw872.png "style=" float: none; "title=" Comm.c2.png "alt=" Wkiol1cq9pfbngmaaabqoleecdw872.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/0E/wKiom1cQ9D6AdnzhAAAiWstxkco483.png "style=" float: none; "title=" Comm.c3.png "alt=" Wkiom1cq9d6adnzhaaaiwstxkco483.png "/>

TEST.c
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/0E/wKiom1cQ9F2jU7mGAABV_uTjZzQ401.png "title=" Test.png "alt=" Wkiom1cq9f2ju7mgaabv_utjzzq401.png "/>

Operation Result:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7F/0E/wKiom1cQ9H6TSffIAAAtJnMKjg4580.png "title=" QQ picture 20160415212338.png "alt=" Wkiom1cq9h6tsffiaaatjnmkjg4580.png "/>


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.