Linux-interprocess communication-semaphore

Source: Internet
Author: User
Tags function prototype semaphore

First, the concept
In simple terms, the semaphore is a counter that describes the number of resources for a critical resource.
The essence of Semaphore is a data operation lock, which does not have the function of data exchange, but realizes interprocess communication by controlling other communication resources (file, external device, etc.).

He is more of an external resource identifier. Semaphore in this process is responsible for the data operation synchronization, mutual exclusion and other functions.
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, resources can be requested, equals 0, no resources available,

The process goes to sleep until the resource is available.

When a process no longer uses a shared resource that uses semaphore control, the value of the semaphore is +1, and the value of the semaphore increases or decreases by atomic operation, which is due to the main function of the semaphore

is a mutually exclusive or multi-process synchronous access that maintains resources. In the creation and initialization of the signal volume, the operation is not guaranteed to be atomic.
  

Atomic operation:
Atomic operations are operations that are not interrupted by the thread scheduling mechanism, which, once started, runs until the end, without any context switch (switching to another

a thread). the self-increment (+ +) operation of shared data (global variables or heap variables) is an error in a multithreaded environment because this operation (a C statement) is compiled into assembly code without

One instruction, so it is possible to execute half of the execution and be interrupted by the dispatch system to execute other code.

  We call the operation of the single instruction Atomic (Atomic), because in any case, the execution of the individual instruction is not interrupted.

All in all, the state of an atomic operation is either not done, done and done, not done, but not completed.

Second, why to use the semaphore

In order to prevent a series of problems caused by multiple programs accessing a shared resource at the same time, we need a method that can be authorized by generating and using tokens, at any moment only

A critical area of code that is accessed by an execution thread. A critical region is a code that performs data updates that requires exclusive execution. And the semaphore can provide such an access mechanism so that a critical section

Domain only one thread accesses it at the same time, that is, the semaphore is used to coordinate the process's access to the shared resource. The use of shared memory requires a semaphore.

Third, the operation principle of the signal volume:

We're talking about a two-dollar semaphore:

Binary semaphore (binary Semaphore) is the simplest kind of lock, which only has two states: occupancy and non-occupancy. It is suitable for resources that can only be accessed by a single thread. When the two-dollar signal volume

When in a non-occupied state, the first thread that attempts to acquire the two-dollar semaphore acquires the lock and resets the two-dollar semaphore to occupy state, after which all other threads attempting to acquire the two-dollar semaphore will

Wait and know that the lock is released.

Since semaphores can only operate in two ways: wait and send signals, P (SV) and V (SV). Their behavior is this:

P (SV): If the SV value is greater than 0, it is reduced by 1, and if his value is 0, the execution of the process is suspended.

V (SV) If any other process is suspended because it waits for SV, let him go back to work, and if no process is suspended for waiting, add 1 to them.

As an example:
Is that two processes share the semaphore SV, and once one of the processes performs a P (SV) operation, he will get the semaphore and can enter the critical section, reducing the SV by 1. While the second process will be blocked from entering a critical

Zone, because when it tries to execute P (SV), the SV is 0, he will be suspended to wait for the first process to leave the critical section and execute V (SV) To release the semaphore, at which point the second process can resume running.

Iv. related functions

1. Create a semaphore set (create more than one semaphore at a time)

Header files : #include <sys/types.h>

#include <sys/ipc.h>

#include <sys/sem.h>

Function prototype:int semget (key_t key,int nsems,int semflg);

Return value: The call succeeded in returning the IPC identifier, and the call failed to return -1;

Parameters:

Key: The keys of the semaphore set created or opened can also have function Ftok generation (different from fork)

Nsems: Represents the number of semaphores in the created semaphore, which is valid only when creating a semaphore set

SEMFLG: ipc_creat: If the IPC is present, open it and, if it does not exist, create an IPC resource.

IPC_EXCL: cannot be used alone, meaningless, when used in conjunction with Ipc_creat: If the IPC does not exist the IPC resource is created and, if present, an error occurs. Together, they are used to guarantee that the resulting object is new, rather than opening an existing object.

2, perform the control operation on the semaphore set

Header files:#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/sem.h>

Function prototypes:int semctl (int semid,int semnum,int cmd, ... );

Return value: The call succeeded in returning a positive number, and the call failed to return -1.

Parameter:semid: The semaphore sets the IPC identifier, because the semaphore is generally used as a semaphore set instead of a single semaphore. Therefore, in the operation of the signal volume set, not only to know the IPC key value, but also to know the specific signal volume concentration.

Semnum: Number of the operation signal in the semaphore set

CMD: The system is set up with three types of operations:

Ipc_stat: Reads the data structure of a semaphore set Semid_ds and stores it in Semun buf parameter.

Ipc_set: Sets the element ipc_perm in the data structure Senid_ds of the semaphore set , whose value is taken from the buf parameter in Semun.

Ipc_rmid: Removes the semaphore set from memory.

3. Operation Signal Volume

Header files:#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/sem.h>

Function prototype:int semop (int semid,struct sembuf * sops,unsigned nsops);

Return value: The call returned 0 successfully, and the call failed to return -1.

Parameter:semid: Semaphore set identification code

Nsops: Number of signal structures, constant greater than or equal to 1.

SOPs: An array of pointers to the operation structure of the signal, and the prototype of the signal operation structure is as follows:

struct SEMBUF

{

unsigned short sem_num; The operation signal is numbered in the semaphore set, and the first number is 0.

Short Sem_op;

Short SEM_FLG; Signal operation Flag, there are two possible options:

Ipc_nowait: When the operation of the signal is not satisfied,Semop () will not block, and immediately return, while setting the error message;

Sem_undo: The signal value is reset to the value before the SEMOP () call, at the end of the program (whether or not it ends properly). This is done to prevent the program from unlocking the locked resource at the end of an exceptional situation, causing the resource to be locked forever.

};

Five, the code implementation:

Linux-interprocess communication-semaphores

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.