Inter-process Communication---semaphores (SEMGET,SEMCTL,SEMOP)

Source: Internet
Author: User
Tags semaphore

The essence of the semaphore is a data manipulation lock, which itself does not have the function of data exchange, but rather by controlling other communication resources (files, external devices) to achieve interprocess communication, which is itself an identifier of an external resource. The semaphore is a counter.

When a resource is requested to be represented by a semaphore , the process needs to read the value of the semaphore to determine if the resource is available. greater than 0, the resource can be requested, equals 0, no resources are available, and the process goes to sleep until the resource is available. When a process no longer uses a semaphore-controlled shared resource, the value of the semaphore is +1, and the increment or decrement of the semaphore value is atomic , because the main function of the semaphore is to maintain a mutually exclusive or multi-process synchronous access to the resource . In the creation and initialization of semaphores, the operation is not guaranteed to be atomic.


reasons for using semaphores: to prevent a series of problems caused by multiple programs accessing a shared resource at the same time, it can be authorized by generating and using tokens, and there can be only one critical region that executes thread access code at any time. A critical region is a code that performs data updates that needs to be executed exclusively. The semaphore can provide an access mechanism that allows a critical section to access it at the same time, which means that the semaphore is used to coordinate the process's access to the shared resource.


How the Semaphore works:

Because the semaphore can only perform two operation waits and sends the signal, namely P (SV) and V (SV)

P (SV): If the value of SV is greater than 0 minus 1, if its value is 0, the execution of the process is suspended

V (SV): If another process is suspended because it waits for the SV, let it run again, and if no process hangs because it waits for the SV, +1


Several functions:

    1. Create a semaphore Set object (get a semaphore set Identifier)

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


      Key: Obtained by the Ftok () function

      Nsems: Creating the number of signals in a semaphore set

      SEMFLG:

Ipc_creat: If there is no semaphore set with key values equal to key in the kernel, it is created, otherwise, returns the identifier of this semaphore set

IPC_EXCL: No meaning for use alone

Ipc_creat | IPC_EXCL: Creates a new semaphore set and returns the identifier of the semaphore set, otherwise returns-1.

return value: The identifier for the semaphore set that was successfully returned. Failed to return -1.


2. complete the p,v operation of the semaphore

int semop (int semid,struct sembuf* sops,unsigned nsops)

Return value: Returns the identifier of the semaphore set when successful. Otherwise, return-1.

Semid: Semaphore Set Identifier

Nsops: The number of operational semaphores , that is, the number of SOPs structure variables, need to be greater than or equal to 1.

SOPs: The first address of a semaphore set structure array that points to an operation

struct SEMBUF

{

The semaphore number in the short semnum;//semaphore set, 0 represents the first semaphore

The short val;//val>0 operates a V-operation Semaphore value +val, which represents the resource for process release control

Short flag; Set semaphore default Action ipc_nowait set semaphore operation does not wait

The sem_undo option causes the kernel to record an undo record associated with the calling process and, if the process crashes, automatically recovers the corresponding semaphore count based on the undo record of the process

}


      sem_undo is used to return the modified semaphore value to the semaphore when the process exits normally (call exit exits or main executes) or exits abnormally (such as a segment exception, 0 exception, received kill signal, etc.).
If the semaphore initial value is 20, The process operates in Sem_undo mode minus 2, minus 5, plus 1; When the process does not exit, the semaphore becomes 20-2-5+1=14; when the process exits, the modified value is returned to the semaphore, and the semaphore becomes 14+2+5-1=20.

3. Perform operation control on a signal within a specified signal set or signal set

function prototypes: int semctl (int semid,int semnum,int cmd,union semun Arg)

Semid: Semaphore Set Identifier

Semnum: Subscript on an array of semaphore sets, indicating a semaphore

Arg

Union Semun {

Short Val; The value of the/*setval * /

struct semid_ds* buf; semid_ds structure for/*ipc_stat, Ipc_set */

unsigned short* array;/*setall, getall array value */

struct Seminfo *buf; /* Cache provided for control ipc_info * /

} arg;


"Comm.h"

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/84/7F/wKiom1eSFrmS8nE9AAB5sSMjTQI978.png "style=" float: none; "title=" 2.PNG "alt=" Wkiom1esfrms8ne9aab5ssmjtqi978.png "/>


"COMM.C"

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/84/7F/wKiom1eSFrrh5RTxAABYcs00X1s421.png "style=" float: none; "title=" 3.PNG "alt=" Wkiom1esfrrh5rtxaabycs00x1s421.png "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/84/7F/wKioL1eSFrrwGM6JAABYxRGITDc819.png "style=" float: none; "title=" 5.PNG "alt=" Wkiol1esfrrwgm6jaabyxrgitdc819.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/84/7F/wKiom1eSFrqxmzjxAABLhTPgi9M719.png "style=" float: none; "title=" 6.PNG "alt=" Wkiom1esfrqxmzjxaablhtpgi9m719.png "/>


"TEST.c"

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/84/7F/wKiom1eSFz2j1p-RAABFs78vhyM856.png "style=" float: none; "title=" 11.PNG "alt=" Wkiom1esfz2j1p-raabfs78vhym856.png "/>

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/84/7F/wKioL1eSFz3i9F7OAAA3CuZnMSU383.png "style=" float: none; "title=" 12.PNG "alt=" Wkiol1esfz3i9f7oaaa3cuznmsu383.png "/>

Test results:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/84/7F/wKioL1eSFrrj4zj4AABVnezfyl8132.png "style=" float: none; "title=" 1.PNG "alt=" Wkiol1esfrrj4zj4aabvnezfyl8132.png "/>


This article is from the "go to see the Stars" blog, please be sure to keep this source http://10810429.blog.51cto.com/10800429/1828958

Inter-process Communication---semaphores (SEMGET,SEMCTL,SEMOP)

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.