Linux network Programming system V shared memory and series functions

Source: Internet
Author: User
Tags integer

Like Message Queuing, shared memory has its own data structure, as follows:

struct Shmid_ds {

struct Ipc_perm shm_perm; /* Ownership and Permissions * *

size_t Shm_segsz; /* Size of Segment (bytes) * *

time_t Shm_atime; /* Last Attach time * *

time_t Shm_dtime; /* Last Detach time * *

time_t Shm_ctime; /* Last Change time * *

pid_t Shm_cpid; /* PID of Creator * *

pid_t Shm_lpid; /* PID of Last Shmat (2)/SHMDT (2) * *

shmatt_t Shm_nattch; /* No. of current Attaches * *

...

};

Similarly, the first member is a shared IPC kernel data structure, and the remainder is private.

Here are a few shared memory functions:

#include <sys/ipc.h>

#include <sys/shm.h>

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

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

int SHMDT (const void *SHMADDR);

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

Features: Used to create shared memory

Prototype int shmget (key_t key, size_t size, int shmflg);

Parameters

Key: This shared memory segment name

Size: Shared memory size

SHMFLG: Consists of nine permission flags, and their usage is the same as the mode mode flag used to create the file

Return value: Successfully returns a non-negative integer, that is, the identity code of the shared memory segment; failed return-1

Function: Connect shared memory segments to process address space

Prototype

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

Parameters

Shmid: Shared Memory identity

SHMADDR: Specify the address of the connection

SHMFLG: Its two possible values are shm_rnd and shm_rdonly

Return value: Successfully returns a pointer to the first byte of shared memory; failed return-1

SHMADDR is NULL, core automatically selects an address

SHMADDR is not null and SHMFLG has no shm_rnd tag, the SHMADDR is the connection address.

SHMADDR is not null and the SHM_RND tag is set SHMFLG, the address of the connection is automatically adjusted downward to the integer multiple of Shmlba.

Formula: shmaddr-(shmaddr% Shmlba)

Shmflg=shm_rdonly, which indicates that the connection operation is used to read-only shared memory

Function: Detach the shared memory segment from the current process

Prototype int SHMDT (const void *SHMADDR);

Parameters

SHMADDR: Pointers returned by Shmat

Return value: successful return 0; failure return-1

Note: Breaking the shared memory segment from the current process is not equal to deleting the shared memory segment

Features: Used to control shared memory

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

Parameters

Shmid: Shared memory identity code returned by Shmget

CMD: Action to be taken (three desirable values)

BUF: Point to a data structure that holds the mode state and access rights of shared memory

Return value: successful return 0; failure return-1

The value of CMD is the following, similar to Message Queuing:

Ipc_stat sets the data in the SHMID_DS structure to the current associated value of shared memory

Ipc_set Sets the current association value of shared memory to the value given in the SHMID_DS data structure, provided that the process has sufficient permissions

Ipc_rmid Delete Shared memory segment

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.