Shmget ()-create shared memory

Source: Internet
Author: User

# Include <sys/IPC. h>
# Include <sys/SHM. h>
IntShmget(Key_t key, size_t size, int shmflg );

Key_t key
-----------------------------------------------
Key:0/ipc_private. When the key value is ipc_private, The shmget () function creates a new shared memory. If the key value is 0, and the ipc_private flag is set in the shmflg parameter, A new shared memory will also be created.
In the communication mode of IPC, whether it is using message queue, shared memory, or even semaphores, each IPC object has a unique name, called a key ). Through the "key", the process can identify the objects used. The relationship between the "key" and the IPC object is like the file name in the file. Through the file name, the process can read and write data in the file, and even multiple processes can share one file. In IPC communication mode, an IPC object can be shared by multiple processes through the use of keys.
In Linux, the data structure of all ipc objects in system v includes an ipc_perm structure, which contains the key value of the IPC object, this key is used to find the reference identifier of the IPC object in System V. Without the "key", the process cannot access the IPC object because the IPC object does not exist in the memory used by the process itself.
Generally, you want your program to have a unique key value in advance with other programs, but it is not always possible, because your program cannot select a key value for a shared memory. Therefore, set the key to ipc_private. In this way, the operating system will ignore the key, create a new shared memory, specify a key value, and return the ipc id of the shared memory. The ID of the new Shared Memory identifier tells other processes that the shared memory can be created by sending child processes or writing files or pipelines.

Int size (in bytes)
-----------------------------------------------
Size is the length of the shared memory to be created. All memory allocation operations are in the unit of pages. Therefore, if a process requests only one piece of memory with only one byte, the memory will be allocated a whole page (the default size of one page on the i386 machine is pace_size = 4096 bytes, the size of the newly created shared memory is actually the page size adjusted from the size parameter. That is, if the size is 1 to 4096, the size of the actually applied shared memory is 4 K (one page); 4097 to 8192, the size of the actually applied shared memory is 8 K (two pages), and so on.

Int shmflg
-----------------------------------------------
The shmflg is mainly related to some labels. The valid values include ipc_creat and ipc_excl. The o_creat of their delta 1_pen () is equivalent to that of o_excl.
Ipc_creat: if the shared memory does not exist, a shared memory is created. Otherwise, the operation is enabled.
Ipc_excl is created only when the shared memory does not exist. Otherwise, an error occurs.

If you use ipc_creat separately, the shmget () function either returns an existing shared memory operator or a new shared memory identifier. If ipc_creat and ipc_excl are used together, shmget () returns a new shared memory identifier. If the shared memory already exists,-1 is returned. The ipc_exel flag does not make much sense, but it can be used with the ipc_creat flag to ensure that the obtained object is newly created, rather than opening an existing object. Specify the user's read and write permissionsShm_rAndShm_w, (Shm_r> 3) and (shm_w> 3) are read and write permissions, while (shm_r> 6) and (shm_w> 6) are global read and write permissions.

Return Value
-----------------------------------------------
The identifier of the shared memory is returned. If the shared memory is not returned, the error-1 and errno are returned.
The einval parameter size is smaller than or greater than shmmax.
Shared Memory caused by key pre-created by eexist, but already exists.
The shared memory caused by the eidrm parameter key has been deleted.
Enospc exceeds the maximum (Shmall) shared memory allowed by the system ).
The shared memory specified by the enoent parameter key does not exist, and the ipc_creat bit is not set for the shmflg parameter.
The eacces does not have the permission.
The enomem core memory is insufficient.

Struct shmid_ds
-----------------------------------------------
The shmid_ds data structure indicates each new shared memory. When shmget () creates a new shared memory, it returns an identifier of the shmid_ds data structure that can be used to reference 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 shares a struct ipc_perm data structure to store permission information to determine whether an IPC operation can access this 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;
};
 

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.