Linux shared memory practices (1) and linux shared memory practices

Source: Internet
Author: User

Linux shared memory practices (1) and linux shared memory practices
Basic concepts of shared memory

The shared memory zone is the fastest IPC format. Once such memory is mapped to the address space of the process that shares it, data transmission between these processes no longer involves the kernel, in other words, processes no longer pass each other's data () by executing system calls that enter the kernel ().


 

Shared Memory VS. Other IPC formats

Transmit data using pipelines/message queues

 

 

 

Transmit data with shared memory

 


(The kernel maintains a data structure for each IPC object)

After the shared memory is generated, data transmission does not need to go through the Linux kernel. The shared memory allows two or more processes to share a given storage region. data does not need to be copied between multiple processes. Therefore, shared Memory transmission speed is faster !!

 

System V shared memory data structure and basic APIs
// Basic Data Structure struct shmid_ds {struct ipc_perm shm_perm;/* Ownership and permissions */size_t shm_segsz;/* Size of segment (bytes) */time_t shm_atime; /* Last attach time */time_t shm_dtime;/* Last detach time */time_t shm_ctime;/* Last change time */pid_t shm_cpid;/* PID of creator */pid_t shm_lpid; /* PID of last shmat (2)/shmdt (2) */shmatt_t shm_nattch;/* No. of current attaches */...};

Shared memory functions

#include <sys/ipc.h>#include <sys/shm.h>int shmget(key_t key, size_t size, int shmflg);void *shmat(int shmid, const void *shmaddr, int shmflg);int shmdt(const void *shmaddr);int shmctl(int shmid, int cmd, struct shmid_ds *buf);

 

Shmget Function

Function: Creates a shared memory and initializes the content of the memory to 0.

Prototype:

int shmget(key_t key, size_t size, int shmflg);

Parameters:

Key: name of the shared memory segment

Size: the size of the shared memory.

Shmflg: consists of nine permission marks. Their usage is the same as the mode flag used when creating a file.

 

Return Value:

A non-negative integer is returned, that is, the ID code of the shared memory segment;-1 is returned if a failure occurs.

// Experiment 1: Open the existing shared memory int main () {// int shmget (key_t key, size_t size, int shmflg ); // open an existing shared memory int shmid = shmget (0x225,1024, 0666); if (shmid =-1) {if (errno = ENOENT) {cout <"ENOENT =" <errno <endl;} err_exit ("shmget error");} return 0 ;}

// Experiment 2: If the shared memory exists, it will be used directly; if it does not exist, it will be created. [IPC_CREAT option function] int main () {// int shmget (key_t key, size_t size, int shmflg); int shmid = shmget (0x225,1024, 0666 | IPC_CREAT ); if (shmid =-1) {if (errno = ENOENT) {cout <"ENOENT =" <errno <endl ;} err_exit ("shmget error");} else {cout <"shmid =" <shmid <endl;} return 0 ;}

// Experiment 3: If the shared memory does not exist, it is created. If yes, an error is returned. [IPC_CREAT | combination of IPC_EXCL] int main () {// int shmget (key_t key, size_t size, int shmflg); int shmid = shmget (0x225,1024, 0666 | IPC_CREAT | IPC_EXCL); if (shmid =-1) {if (errno = ENOENT) {cout <"ENOENT =" <errno <endl ;} else if (errno = EEXIST) {cout <"File is exits... EEXIST = "<errno <endl;} err_exit (" shmget error ");} else {cout <" shmid = "<shmid <endl ;} return 0 ;}

 

 

Shmat Function

Function: connect the shared memory segment to the process address space.

Prototype:

void *shmat(int shmid, const void *shmaddr, int shmflg);

Parameters:

Shmid: Shared Memory ID

Shmaddr: Specifies the connection address.

Shmflg: two possible values are SHM_RND and SHM_RDONLY.

 

Return Value:

A pointer is returned, pointing to the first section of the shared memory;-1 is returned if a failure occurs.

 

Description of Shmaddr and shmflg combinations

Shmaddr is NULL and will be automatically connected to the memory for the process in Linux (recommended)

If shmaddr is not NULL and shmflg is not marked with SHM_RND, shmaddr is used as the connection address.

If shmaddr is not NULL and SHM_RND mark is set for shmflg, the connected address is automatically adjusted down to an integer multiple of SHMLBA. Formula: shmaddr-(shmaddr % SHMLBA)

Shmflg = SHM_RDONLY, indicating that the connection operation is used for read-only shared memory.


// Example 1int main () {// int shmget (key_t key, size_t size, int shmflg); // obtain or enable the shared memory int shmid = shmget (0x1576422, sizeof (Student), 0666 | IPC_CREAT); if (shmid =-1) {err_exit ("shmget error ");} // connect the shared memory with the ID shmid to the Student * pStudent = static_cast <Student *> (shmat (shmid, NULL, 0) of the process )); // write data to the memory Student studentA = {"xiaofang", 2012}; memcpy (pStudent, & studentA, sizeof (Student )); cout <pStudent-> name <"" <pStudent-> number <endl; // You can also obtain the memory content (in fact, there is no difference with local memory) student * pNewStudent = pStudent; cout <pNewStudent-> name <"" <pNewStudent-> number <endl; return 0 ;}

// Example 2: Use the shared memory as an array int main () {// int shmget (key_t key, size_t size, int shmflg ); // obtain or enable the shared memory int shmid = shmget (0x15764221,102 4 * sizeof (int), 0666 | IPC_CREAT); if (shmid =-1) {err_exit ("shmget error");} // connect the shared memory with the ID shmid to the process int * pArray = static_cast <int *> (shmat (shmid, NULL, 0); if (pArray = (void *)-1) {err_exit ("shmat error") ;}for (int I = 0; I! = 1024; ++ I) {pArray [I] = I + 1 ;}for (int I = 0; I! = 1024; ++ I) {cout <pArray [I] <endl;} return 0 ;}


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.