Go language shared memory read-write instance Analysis _golang

Source: Internet
Author: User
Tags error code

This article analyzes the way of shared memory reading and writing in Go language. Share to everyone for your reference. The specific analysis is as follows:

The previous analysis of the Go language pointer operation and embedded C code method, do a go language shared memory reading and writing experiment.

Let's talk about what is shared memory. We know that the memory that the different processes see is independent of each other, there is no way to directly manipulate each other's data, while shared memory relies on the memory mapping mechanism provided by the operating system, allowing a different process to map a single address space to the same virtual memory area so that different processes can operate into a shared block of memory. Shared memory is the most efficient interprocess communication mechanism because data does not need to be replicated between the kernel and the program.

Shared memory uses the system-supplied mmap function. It can map a file to a region of virtual memory where the program uses pointers to refer to the area, and the operation of the memory area is written back to the file, and the go built-in Syscall package has the MMAP function, but it is encapsulated and returns []byte, no way to do the pointer operation I need, so I still use CGO to invoke native mmap.

The experiment is divided into reading and writing two programs so that we can observe that the read process can read the write process to write the shared memory information.

Here is the code for SHM_WRITER.GO:

Copy Code code as follows:
Package Main
/*
#cgo Linux ldflags:-LRT
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#define File_mode (S_IRUSR | S_IWUSR | S_irgrp | S_iroth)
int my_shm_new (char *name) {
Shm_unlink (name);
Return Shm_open (name, o_rdwr| O_creat| O_EXCL, File_mode);
}
*/
Import "C"
Import (
"FMT"
"Unsafe"
)
Const SHM_NAME = "MY_SHM"
Const SHM_SIZE = 4 * 1000 * 1000 * 1000
Type MyData struct {
Col1 int
Col2 int
Col3 int
}
Func Main () {
FD, Err: = C.my_shm_new (c.cstring (shm_name))
If Err!= nil {
Fmt. PRINTLN (ERR)
Return
}
C.ftruncate (FD, Shm_size)
PTR, err: = C.mmap (Nil, shm_size, c.prot_read| C.prot_write, c.map_shared, FD, 0)
If Err!= nil {
Fmt. PRINTLN (ERR)
Return
}
C.close (FD)
Data: = (*mydata) (unsafe. Pointer (PTR))
Data. Col1 = 100
Data. Col2 = 876
Data. Col3 = 8021
}

Here is the code for SHM_READER.GO:

Copy Code code as follows:
Package Main
/*
#cgo Linux ldflags:-LRT
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#define File_mode (S_IRUSR | S_IWUSR | S_irgrp | S_iroth)
int My_shm_open (char *name) {
Return Shm_open (name, O_RDWR);
}
*/
Import "C"
Import (
"FMT"
"Unsafe"
)
Const SHM_NAME = "MY_SHM"
Const SHM_SIZE = 4 * 1000 * 1000 * 1000
Type MyData struct {
Col1 int
Col2 int
Col3 int
}
Func Main () {
FD, Err: = C.my_shm_open (c.cstring (shm_name))
If Err!= nil {
Fmt. PRINTLN (ERR)
Return
}
PTR, err: = C.mmap (Nil, shm_size, c.prot_read| C.prot_write, c.map_shared, FD, 0)
If Err!= nil {
Fmt. PRINTLN (ERR)
Return
}
C.close (FD)
Data: = (*mydata) (unsafe. Pointer (PTR))
Fmt. PRINTLN (data)
}

The above program maps a 4G of virtual memory to prove that Mmap does not actually occupy 4G memory, but uses virtual memory.

Shm_writer creates a shared memory, writes a structure to the memory area, and Shm_reader reads a struct.

There is one line in the embedded C code:

Copy Code code as follows:
#cgo Linux ldflags:-LRT

Because Mmap does not need to connect LIBRT on the Mac, it is needed on Linux, so a conditional link is made, which is the function provided by CGO.

The above code also uses a CGO technique, like the shm_open and MMAP functions return errno in error, and if we use the multiple return syntax in Go, CGO will itself convert the error code to error information, which is convenient.

I hope this article will help you with your go language program.

Related Article

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.