Linux for Shared memory synchronization

Source: Internet
Author: User

This paper mainly introduces four kinds of methods to realize shared memory synchronization.

Shared memory is the most efficient way to communicate between processes, and processes can read and write directly to memory without requiring any copy of the data. It is a kind of IPC object.

In order to exchange information between multiple processes, the kernel specifically leaves out a chunk of memory that can be mapped to its own private address space by the process that needs to be accessed. The process can read and write directly to this memory area without having to copy the data, thus greatly improving the efficiency.

Synchronization (synchronization) means that multiple tasks (threads) work together in the order in which they are contracted to do one thing. Because multiple processes share a piece of memory, they also need to rely on some kind of synchronization mechanism, such as mutexes and semaphores.

Semaphore (semaphore), also called Semaphore. It is a mechanism for synchronizing between different threads in different processes or within a given process. Semaphores include POSIX-known semaphores, POSIX memory-based beacons (nameless beacons), and System V semaphores (IPC objects)

Method one, using POSIX famous signal to realize the synchronization of shared memory

A well-known semaphore can be used for synchronization between threads and for inter-process synchronization.

Two processes, read and write to the same shared memory, can be synchronized using a known semaphore. One process is written, another process is read, using two well-known semaphore Semr, SEMW. Semr semaphore control can be read, initialized to 0. SEMW semaphore control can be written, initially 1.

The sample code for reading shared memory is as follows

SEMR = Sem_open ("Mysem_r", O_creat | O_rdwr, 0666, 0);
if (Semr = = sem_failed)
{
printf ("errno=%d\n", errno);
return-1;
}

SEMW = Sem_open ("Mysem_w", O_creat | O_rdwr, 0666, 1);
if (SEMW = = sem_failed)
{
printf ("errno=%d\n", errno);
return-1;
}

if (Shmid = Shmget (key, MAXSIZE, 0666 | ipc_creat)) = =-1)
{
Perror ("Semget");
Exit (-1);
}

if ((Shmadd = (char *) Shmat (shmid, NULL, 0) = = (char *) (-1))
{
Perror ("Shmat");
Exit (-1);
}

while (1)
{
Em_wait (SEMR);
printf ("%s\n", Shmadd);
Sem_post (SEMW);
}

The sample code for the program that writes shared memory is as follows

。。。。。。
Same-read procedure
while (1)
{
Sem_wait (SEMW);
printf (">");
Fgets (Shmadd, MAXSIZE, stdin);
Sem_post (SEMR);
}

Method two, using POSIX nameless signal to realize the synchronization of shared memory

The POSIX nameless semaphore is a memory-based semaphore that can be used for inter-thread synchronization and also for inter-process synchronization. For inter-process synchronization, you need to create a nameless semaphore in shared memory.

Therefore, the shared memory needs to define the following struct body.

typedef struct
{
Sem_t Semr;
Sem_t SEMW;
Char Buf[maxsize];
}SHM;

Read and write the program flow as shown.

Method three, using the System V signal to realize the synchronization of shared memory

The System v Semaphore is a collection of one or more semaphores. Each of these is a separate counting beacon. And a POSIX semaphore is a single counting beacon.

The System V semaphore is maintained by the kernel and the main function is semget,semop,semctl.

One process is written, another process is read, there are two lights in the signal set, and the subscript 0 indicates whether it can be read and initialized to 0. Subscript 1 Indicates whether it can be written, initially 1.

The program flow is as follows:

The writing process is similar to the front.

Method four, using the signal to realize the synchronization of shared memory

Signal is a kind of simulation of interrupt mechanism at software level, and it is an asynchronous communication mode. The use of signals also enables synchronization of shared memory.

Ideas:

Reader and writer must obtain the process number of each other by signaling communication, and can use shared memory to save the process number of both parties.

The order in which reader and writer runs is not deterministic, and it is possible to contract a running process to create shared memory and initialize it.

Use pause, kill, signal and other functions to implement the program (the process is similar to the front).

Linux for Shared memory synchronization

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.