Shared memory of the Linux IPC

Source: Internet
Author: User
Tags int size

System V Shared memory mechanism: Shmget Shmat shmdt Shmctl

Principle and realization:

system V IPC the nature of shared memory under the mechanism is a special area of memory where the data that needs to be shared between processes is placed in the shared memory area, and all processes that need to access the shared zone map the shared area to the address space of the process. Such a process using shared memory can write information to that space, while another process that uses shared memory can get the information just written by using a simple memory read operation, which enables the exchange of information between two different processes, enabling interprocess communication.

shared memory allows one or more processes to communicate through memory that appears in their virtual address space at the same time, and this virtual memory page is referenced by the page table entry for each shared process, and does not require that the virtual memory of all processes have the same address. The process object's access to the shared memory is controlled by key(key) while the access is checked by key.

The function is defined as follows:

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

key_t Ftok (const char *pathname, int proj_id);

The function Ftok is used to create a keyword that can be associated with a shared memory segment with that keyword.

The parameter pathname is a full-path file name, and the file must be accessible.

Parameter proj_id typically passes in a non-0 character

A unique key can be created with pathname and proj_id combinations

If the call succeeds, returns a keyword, otherwise returns-1

int Shmget (key_t key, int size, int shmflg);

The function shmget is used to create or open a shared memory segment that is uniquely created by the first parameter of the function. If the function succeeds, it returns a unique shared memory identifier (equivalent to the process number, which uniquely identifies the shared memory), and the failure returns-1.

The parameter key is a keyword associated with the shared memory segment, and if there is already a shared memory segment associated with the specified keyword, the identity of that memory segment is returned directly, indicating open and, if not present, creating a new shared memory segment. The value of key can be either generated with the ftok function or ipc_rpivate(used to create a shared memory that is only part of the creation process, primarily for parent-child communication), which indicates that a new shared memory segment is always created;

Argument size Specifies the size of the shared memory segment, in bytes;

The parameter SHMFLG is a masked composite value that can be a composition of access permission values with (Ipc_creat or IPC_EXCL). Ipc_creat means that if the memory segment does not exist, it is created. IPC_EXCL indicates that if the memory segment exists, the function returns a failed result (-1). Returns the memory segment ID if the call succeeds, otherwise returns-1

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

The Shmat function maps a shared memory segment to an address in the process space .

The parameter shmid is the identity of the shared memory segment and should normally be the successful return value of Shmget

The parameter shmaddr specifies the location of the shared memory connection to the address in the current process. Usually null, which means that the system is allowed to select the address where the shared memory appears.

The parameter SHMFLG is a set of bit identifiers, typically 0.

If the call succeeds, returns the first address of the mapped process space, otherwise returns (char *)-1.

int SHMDT (const void *SHMADDR);

The function Shmdt is used to separate shared memory segments from process space .

The parameter shmaddr is typically a successful return value of Shmat.

The function returns 0 successfully, and returns 1 on failure. Note that separating the shared memory does not remove it, only making the shared memory unavailable to the current process.

int shmctl (int shmid, int cmd, struct shmid_ds *buf);

function Shmctl is a control function for shared memory that can be used to delete shared memory segments .

The parameter shmid is a shared memory segment identity that should normally be a successful return value of Shmget

The parameter cmd is the mode of operation for the shared memory segment and is optional as ipc_stat,ipc_set,ipc_rmid. Typically Ipc_rmid, which represents the deletion of shared memory segments.

The parameter buf is the information structure body data that represents the shared memory segment, usually null.

For example, Shmctl (ksharemem,ipc_rmid,null) means deleting a shared memory segment Kharemem

Example:

shm1.c

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <unistd.h>5#include <sys/types.h>6#include <sys/ipc.h>7#include <sys/shm.h>8 9 intMainint*ARGC,Char*argv[])Ten { One key_t My_key; A     intMy_shm; -     Char*pstr; -  the     //1, create a keyword to associate a shared memory segment, return the keyword -My_key = Ftok ("./temp.c",123); -  -     //2, create or open a shared memory segment, return the shared memory segment flag number +MY_SHM = Shmget (My_key,1024x768,0666|ipc_creat); -     if(My_shm = =-1) +     { Aprintf"Shmget error!\n"); atExit1); -     } -  -     //3, Map the shared memory segment to an address in the process space and return to the first address of the mapping process space -Pstr = (Char*) Shmat (My_shm,null,0); -  instrcpy (PSTR,"Hello world!\n"); -Sleep5); toprintf"recv from 2:%s\n", pstr); +  -     //4, separating shared memory segments from process space the SHMDT (PSTR); *  $     //5. Delete Shared memory SegmentsPanax Notoginseng Shmctl (my_shm,ipc_rmid,null);  -  the     return 0; +}

Shm2.c

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4#include <unistd.h>5#include <sys/types.h>6#include <sys/ipc.h>7#include <sys/shm.h>8 9 intMainint*ARGC,Char*argv[])Ten { One key_t My_key; A     intMy_shm; -     Char*pstr; -  the     //1, create keywords -My_key = Ftok ("./temp.c",123); -  -     //2. Create or open a shared memory segment +MY_SHM = Shmget (My_key,1024x768,0666); -     if(My_shm = =-1) +     { Aprintf"Shmget error!\n"); atExit1); -     } -  -     //3, mapping shared memory segments to a process space -Pstr = (Char*) Shmat (My_shm,null,0); -printf"recv from 1:%s\n", pstr); instrcpy (PSTR,"How is you!\n"); -  to     //4 separating shared memory segments from process space + SHMDT (PSTR); -      the     return 0; *}

Shared memory of the Linux IPC

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.