Linux interprocess communication-Shared memory

Source: Internet
Author: User
Tags int size

Shmgetint Shmget (key_t Key, size_t size, int flag); Key: Rules for identifiers Size: Number of bytes shared bucket flag: Read-Write permission return value: The ID of the shared store was successfully returned, failure returned-1 key_t Key
-----------------------------------------------
Key identifies the value of the shared memory: 0/ipc_private 。 When the value of key is Ipc_private, the function shmget () Creates a new shared memory, and if the value of key is 0, and the IPC_PRIVATE flag is set in the parameter SHMFLG, a new shared memory will also be created.
In the IPC communication mode, whether using Message Queuing or shared memory, or even semaphores, each IPC object has a unique name called "key". With the key, the process is able to identify the object being used. The relation of "key" to the IPC object is like the file name to the file, through the filename, the process can read and write the data within the file, even multiple processes can share a file. In the IPC communication mode, the use of "keys" also enables an IPC object to be shared by multiple processes.
All data structures in the Linux system that represent the IPC objects in Systems v include a IPC_PERM structure that contains the key values for the IPC object, which is used to find the reference identifier of the IPC object in System V. If you do not use the key, the process will not be able to access the IPC object because the IPC object does not exist in the memory used by the process itself.
In general, you want your program to be able to pre-contract a unique key value with other programs, but it is not always possible, because your program cannot select a key value for a piece of shared memory. Therefore, the key is set to Ipc_private, so that the operating system ignores the key, establishes a new shared memory, specifies a key value, and returns this shared memory IPC identifier ID. Instead, the identifier ID of this new shared memory tells other processes to implement it either by deriving a child process or by writing to a file or pipeline after the shared memory is established.


int size (unit byte byte)
-----------------------------------------------
The size is the length of the shared memory to be established. All memory allocation operations are in page units. So if a process only requests a single byte of memory, the memory will also be allocated a full page (the default size of pace_size=4096 bytes on a page in the I386 machine) this way, the newly created shared memory size is actually the page size that is adjusted from the size parameter. That is, if size is 1 to 4096, the amount of shared memory actually requested is 4K (one page), and 4097 to 8192, the amount of shared memory actually requested is 8K (two pages), and so on.


int SHMFLG
-----------------------------------------------
SHMFLG mainly related to some signs. Valid include Ipc_creat and IPC_EXCL, and their function is equivalent to open () O_creat and O_EXCL.
Ipc_creat if the shared memory does not exist, create a shared memory or open the operation.
IPC_EXCL only if the shared memory does not exist, the new shared memory is established, or an error is generated.

If you use the Ipc_creat,shmget () function alone either returns an operator of the shared memory that already exists, or returns an identifier for the newly created shared memory. If the ipc_creat and IPC_EXCL flags are used together, Shmget () returns an identifier for the newly created shared memory, if the shared memory already exists, or 1 is returned. The IPC_EXEL flag itself does not make much sense, but working with the IPC_CREAT flag can be used to guarantee that the resulting object is new, rather than opening an existing object. Read and Write permissions for the user are specified Shm_rAnd Shm_w, (Shm_r>3) and (shm_w>3) are a set of read and write licenses, while (shm_r>6) and (SHM_W>6) are global read and write licenses.


return value
-----------------------------------------------
The identifier for the shared memory was successfully returned; the -1,errno storage error reason was not successfully returned.
The EINVAL parameter size is less than shmmin or greater than Shmmax.
Eexist shared memory due to pre-established key, but already exists.
The shared memory caused by the EIDRM parameter key has been deleted.
The ENOSPC exceeds the maximum amount of shared memory that the system allows to establish (Shmall).
The shared memory referred to by the ENOENT parameter key does not exist, and the parameter SHMFLG is not set ipc_creat bit.
Eacces does not have permissions.
Enomem core memory is low.


struct Shmid_ds
-----------------------------------------------
The SHMID_DS data structure represents each new shared memory. When Shmget () creates a new piece of shared memory, it returns an identifier that can be used to reference the SHMID_DS data structure of the shared memory.

Include/linux/shm.h

struct Shmid_ds {
struct Ipc_perm shm_perm; /* Operation Perms */
int shm_segsz; /* Size of segment (bytes) */
__kernel_time_t Shm_atime; /* Last Attach time */
__kernel_time_t Shm_dtime; /* Last Detach time */
__kernel_time_t Shm_ctime; /* Last Change time */
__kernel_ipc_pid_t Shm_cpid; /* PID of Creator */
__kernel_ipc_pid_t Shm_lpid; /* PID of last operator */
unsigned short shm_nattch; /* No. of current attaches */
unsigned short shm_unused; /* Compatibility */
void *shm_unused2; /* ditto-used by DIPC */
void *shm_unused3; /* Unused */
};


struct Ipc_perm
-----------------------------------------------
For each IPC object, the system uses a struct IPC_PERM data structure to hold the permission information to determine whether an IPC operation can access the IPC object.

struct Ipc_perm {
__kernel_key_t key; 
         __kernel_uid_t   uid;  
         __kernel_gid_t   GID;  
         __kernel_uid_t   cuid;  
         __kernel_gid_t   Cgid;  
         __kernel_mode_t mode;  
         unsigned short   seq;  
}; //----------------------------------------

Shmatvoid *shmat (int shmid, const void *addr, int flag); Shmid: Shared storage idaddr: typically 0, which indicates a connection to the first available address selected by the kernel, otherwise, if flag does not specify SHM_RND, it is connected to the address specified by addr, and if flag is shm_rnd, the address takes full flag: as described earlier, Typically 0 return value: If successful, returns the shared bucket address, an error returns -1shmdtint shmdt (void *addr), addr: The address of the shared bucket, the return value of the previous call to Shmat SHMDT will make the shmid_ds in the related SHM_ structure Nattch counter value minus 1shmctlint shmctl (int shmid,int cmd,struct shmid_ds *buf) Shmid: Shared bucket idcmd: Some commands, with: ipc_stat,ipc_rmid,shm_ Lock,shm_unlock Note that shared memory will not be automatically eliminated with the end of the program, either by calling Shmctl Delete or by tapping the command yourself to delete it, or stay in the system forever.

Linux interprocess communication-Shared memory

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.