Linux Shared Memory Shmat,shmget,shmdt,shmctl

Source: Internet
Author: User

Shmget

int shmget(key_t key, size_t size, int flag);//Open up a section of shared memory

key_t key: Rules for Identifiers ()

size_t Size: Amount of shared memory

int flag: Permission to read and write

Return value: Successfully returns the ID of a contributing memory, failure returns-1;

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.

Size to create the length of the contribution memory, to the page as the smallest to the allocation unit,pace_size=4096 bytes , that is, size = 1 o'clock the actual opening 4K

Flag

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 occurs.

shmat
void *shmat (int shmid, const void *addr, int flag)//Gets the address of the shared memory bucket span>

int Shmid:shmget () ID of the returned bucket

void* Addr: is typically 0, which means connecting 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 is rounded

int flag: typically 0

return value: The address of a bucket that successfully returns a common memory. Failed return-1

SHMDT

int shmdt (void *addr); BR style= "COLOR: #333333; font-family:arial; line-height:26px; " >ADDR: The address of the shared bucket, The return value when Shmat was previously called
SHMDT will subtract the Shm_nattch counter value from the related SHMID_DS structure by 1

When a process no longer needs a shared memory segment, it calls the SHMDT () system call to cancel the segment, but this does not really delete the segment from the kernel, but rather the value of the shm_nattch domain of the associated SHMID_DS structure is reduced 1, when this value is 0 , the kernel removes the shared segment physically.

Shmctl

int shmctl (int shmid,int cmd,struct shmid_ds *buf)
Shmid: ID of shared bucket
CMD: some commands

Ipc_stat.To the state of shared memory
          

The Ipc_rmid command does not actually delete a segment from the kernel, but simply marks the segment as deleted, and the actual deletion occurs when the last process leaves the shared segment.

Note that shared memory is not automatically eliminated as the program ends, or SHMCTL is called to delete,

You can either use your hand to delete it or leave it in the system forever.


Linux shared Memory Shmat,shmget,shmdt,shmctl

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.