Linux interprocess communication--shmget () shared memory (i)

Source: Internet
Author: User
Tags int size

Most of the specific implementations of shared memory are mapped to the same piece of physical memory by the memory shared between the different processes. Multiple processes map the physical memory area to their own virtual address space, which can access the shared memory area directly, allowing communication through the zone.

Shared memory allows two unrelated processes to access the same piece of physical memory, because the data does not need to replicate between different processes, so it is a very efficient way to pass data between two running processes, a process that writes data to a shared memory region, All processes that share the zone can immediately see the data content.

Attention:
1. As shown, pages that share virtual memory appear in each page table of the process that shares the page. However, it does not need to have the same virtual address in the virtual memory of all processes.
2. Synchronous control of shared memory must be the responsibility of the programmer. Use shared memory to provide effective access to large chunks of memory and to synchronize access to that memory by passing the grapevine.

5.2 Shared Memory functions

Function Prototypes:

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

Function Description:

Shmget function: Used to open up or point to a piece of shared memory, return the ID of the shared memory area, and create the appropriate zone if no shared area exists. Keyt key: The identifier for the shared memory.                      In the case of inter-process communication between parent and child relationships, this identifier is replaced with Ipc_private.           If two processes have no relationship, use Ftok () to figure out an identifier (or define it yourself).             int size: Specifies the amount of memory that needs to be shared in bytes.                        int flag: A permission flag that contains 9 bits, which is the mode of this memory and the identity of the permission. The pattern has the following values: Ipc_creat new (returns the ID of the current shared memory if created) Ipc_excl combined with ipc_creat The "mode" and "Permission ID" are performed or operated as the third parameter, if one is created. Example: Ipc_creat | Ipc_excl | 0640 where 0640 is the permission identifier, the 4/2/1 represents the read/write/execute 3 permissions respectively, the first 0 is the UID, the first 6 (4+2) represents the owner's permissions, the second 4 represents the same group of permissions, and the 3rd          A 0 represents the rights of others.          Returns the ID of the shared memory when the function call succeeds, and returns-1 on failure. Note: When creating shared memory, the SHMFLG parameter requires at least Ipc_creat |          Permission ID, if only ipc_creat the address requested is K=0XFFFFFFFF, cannot be used; Shmat function: A function that allows this process to access a piece of shared memory.          When you create shared memory for the first time, it cannot be accessed by any process, and to enable access to that shared memory, you must connect it to the address space of a process.          The Shmat function is used to complete this work.               int Shmid: The ID of the shared memory, which is the identity of the shared memory. Char *shmaddr: Shared memory is connected to the start address in the process, if SHMADDR is null, the kernel maps the shared memory to the selected address space of the system, and if SHMADDR is not NULL, the kernel maps the shared memory to the location specified by SHMADDR. Note: In general, we rarely need to control the address of a shared memory connection, usually to let the system choose an address, otherwise it will make the application of hardware dependency too high.          Therefore, the SHMADDR is generally set to null. int Shmflag: The operation mode of this process for this memory can be taken from two values: Shm_rnd and Shm_rdonly. The shm_rnd is read-write mode, and Shm_rdonly is readonly mode. It is important to note that the read and write permissions for shared memory are determined by its owner, its access rights, and the owner of the current process. If SHMFLG & Sm_rdonly is true, it cannot be written even if the access permission for the shared memory allows a write operation.         This parameter is usually set to 0. When the function call succeeds, returns the starting address of the shared memory, which returns-1 on failure.          SHMDT function: Used by the function to remove the memory used by this process.           SHMDT (), in contrast to Shmat (), is a function that prohibits this process from accessing a piece of shared memory.          Char *shmaddr is the starting address for that shared memory. The function call returns 0 when it succeeds, and 1 if it fails.            Shmctl function: Controls the use of this shared memory.            int Shmid: The ID of the shared memory, which is the shared memory identity. int cmd: The control command, which indicates the action to take, is as follows: Ipc_stat gets the state of shared memory: Sets the data in the SHMID_DS structure to the current associated value of the shared memory Ipc_set  Changing the state of shared memory: Sets the current association value of shared memory to the value given in the SHMID_DS structure ipc_rmid delete shared memory segments The SHMID_DS structure contains at least the following members:                struct Shmid_ds {uid_t shm_perm.uid;                       uid_t Shm_perm.gid;                  uid_t Shm_perm.mode; } struct Shmid_ds *buf: A struct-body pointer.                                  When Ipc_stat, the acquired state is placed in the structure.         If you want to change the state of shared memory, specify it with this struct. The function call returns 0 when it succeeds, and 1 if it fails.

Linux interprocess communication--shmget () shared memory (a)

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.