———— signal volume for interprocess communication (IPC)

Source: Internet
Author: User
Tags message queue semaphore

I. signal Volume

Before we talk about semaphores, we should mention the concept of critical resources and critical zones , which refers to multiple process accesses but only one process exclusive resource for a period of time, while a critical section refers to the common code that multiple processes access critical resources.

The essence of the semaphore is a data manipulation lock, or a counter, which itself does not provide communication between processes, but by controlling a resource to complete the process of mutual exclusion and synchronization, such as when a process requests a semaphore to represent a critical resource, first to check the semaphore of the resource, If the semaphore is greater than 0, indicates that a resource is available, and if the semaphore equals zero, then the requested process needs to wait for the resource to be used by another process.

The presence of semaphores is actually a signal to the process, indicating that the resource can be accessed, and therefore also as a means of interprocess communication.


Two. creation and destruction of signal volume

The semaphore is the same as the previously mentioned pipeline, Message queue, which is required for function creation:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/00/wKiom1cPO7jAAimFAAAnd4ht7cQ852.png "title=" QQ picture 20160414145144.png "alt=" Wkiom1cpo7jaaimfaaand4ht7cq852.png "/>

In the function arguments, key is mentioned in the message queue, which can be considered a port number and created with the Ftok function:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/FD/wKioL1cPPPLDpGDmAAAJYnVVfDs415.png "title=" QQ picture 20160414145409.png "alt=" Wkiol1cpppldpgdmaaajynvvfds415.png "/>

Pathanme is the file pathname, and proj_id is an integer, and the combination can be converted to an integer identifier.

Nsems indicates how many semaphores to create because Semget creates a semaphore set, and there may be multiple resources that need to be identified;

SEMFLG is also the same as MSGFLG in Message Queuing, there are two options ipc_creat and ipc_excl, when two are used together to ensure that the semaphore set is newly created, the creation fails to return-1; when ipc_ creat if the semaphore set already exists, returns the existing one when it is used alone, and creates a new if it does not exist;


When Semget creates a semaphore set, it returns a semaphore set of sem_id integers, if the failure returns-1;


The semaphore set is created, and of course it needs to be destroyed after the use is complete:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/00/wKiom1cPQJ2AS4UtAAAIst0izPY700.png "title=" QQ picture 20160414151258.png "alt=" Wkiom1cpqj2as4utaaaist0izpy700.png "/>

The first of the function parameters is of course the sem_id of the semaphore set to be removed;

The semnum indicates that the number of semaphores in the semaphore set is to be CMD-operated;

cmd operation for Ipc_rmid Delete

The following is a reference to the cmd operation Setval, can be used to initialize the semaphore, you can see the Semctl function in the last item is a variable parameter list, that is, semctl parameters can be three or four, when the four parameters, the last must be a union, To define as follows:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/00/wKiom1cPQaPxrlgNAAAzXf-sYYs504.png "title=" QQ picture 20160414150737.png "alt=" Wkiom1cpqapxrlgnaaazxf-syys504.png "/>

This consortium is not necessarily defined in a system header file, and if not, it needs to be defined by the user; the first shape Val in the union is the value that needs to be initialized, and the back buf is the buffer used by IPC_STST and Ipc_set, and the array is GetAll, The arrays used by SetAll and the _buf represent the buffers used by the Ipc_info (Linux-specific), all of which are user-defined.


Three. signal Volume P operation with v Operation

When a process to request a resource, the system will first check whether the semaphore is greater than 0, if greater than 0 indicates that a resource is available, then the process will use the resource, so the resource should be reduced to zero semaphore, that is, P operation When the process is finished using the resource to be put back for use by another process, the value of the semaphore should be added back to the original value, which is V operation:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/7E/FD/wKioL1cPREnhmswhAAApAvici1s486.png "title=" QQ picture 20160414152521.png "alt=" Wkiol1cprenhmswhaaapavici1s486.png "/>

function parameters,

Semid won't say. Is the sem_id that indicates which semaphore operation to perform;

SOPs is a struct pointer, and the struct should be defined as follows:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7F/00/wKiom1cPRVmgtwuDAAAHBwYbc0w776.png "title=" QQ picture 20160414153242.png "alt=" Wkiom1cprvmgtwudaaahbwybc0w776.png "/>

Sem_num Indicates the number of semaphores in the concentration of the signal;

Sem_op indicates what action to take, less than 0 means minus, greater than 0 means plus, equals 0 is;

SEM_FLG has two options,ipc_nowait and Sem_undo, and ipc_nowait indicates that if no resources are available, it does not block the direct return Eagain if an operation is specified as Sem_ Undo, when the process terminates, it will automatically undo the operation to restore the original value;

Nsops Indicates that there are several semaphore numbers to be manipulated;



Chestnut Time:

As in the previous conversation about Message Queuing, encapsulate the functions needed for semaphores:

First the semaphore is created, in order to distinguish between the newly created semaphore or the existing semaphore, two functions Creat_sem and Get_sem can be encapsulated according to the different parameters, and also initialized:

650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/7F/00/wKioL1cPkazT3gP6AACqoydO4uY025.jpg "title=" create. jpg "alt=" Wkiol1cpkazt3gp6aacqoydo4uy025.jpg "/>

Next is the function p, v operation and destroy Semaphore:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/00/wKioL1cPkc-w5QsNAACqFTMibfY237.png "title=" pv.png "alt=" Wkiol1cpkc-w5qsnaacqftmibfy237.png "/>



Here we can write a chestnut, we know that the monitor only allows a process access to critical resources for a period of time, when there is no semaphore identification, two processes simultaneously to the display output will have what kind of result:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/02/wKiom1cPYprSCgm3AAAg-spxKvQ294.png "style=" float: none; "title=" QQ picture 20160414172623.png "alt=" Wkiom1cpyprscgm3aaag-spxkvq294.png "/>


The running program can see that A and B are out of order, that is, when two processes simultaneously access a resource, a conflict arises;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7E/FF/wKioL1cPY1LzPYiKAAALKYByUm4776.png "style=" float: none; "title=" QQ picture 20160414172533.png "alt=" Wkiol1cpy1lzpyikaaalkybyum4776.png "/>


The following can change the program to use semaphores to complete interprocess communication, that is, when two processes access critical resources at the same time there is a communication process, you use I just waiting for me to use you just wait:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/03/wKiom1cPkULSfD2TAAAa_HRGPiY438.png "style=" float: none; "title=" Zijincheng.png "alt=" Wkiom1cpkulsfd2taaaa_hrgpiy438.png "/>

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7F/03/wKiom1cPkUaDnTTRAAAQTSMq12g555.png "style=" float: none; "title=" Fujinchegn.png "alt=" Wkiom1cpkuadnttraaaqtsmq12g555.png "/>


The above program must note that the fork sub-process should be in the parent process after the Creat_sem, or if the child process first run Get_sem out of the semaphore, then the parent process run to Creat_sem will not get the semaphore, run the program will get the following AABB staggered ordered results, This completes another way of communicating between processes:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7F/00/wKioL1cPkf6Axbj1AAALKYByUm4570.png "style=" float: none; "title=" QQ picture 20160414172533.png "alt=" Wkiol1cpkf6axbj1aaalkybyum4570.png "/>

The last thing to say is that the operation of the semaphore is atomic because there is no intermediate value, but the creation and initialization of the semaphore is not necessarily atomic.



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1763921

———— signal volume for interprocess communication (IPC)

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.