Linux IPC Practice (9)--system v shared memory

Source: Internet
Author: User
Tags readable

Shared Memory API

#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);
System V shared memory basic data structure struct shmid_ds{    struct ipc_perm shm_perm;    /* Ownership and Permissions:system V IPC Common data structure */    size_t          shm_segsz;   /* Size of Segment (bytes): Sizes of Shared memory segments */    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 */    ...};

Shmget

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

create the shared memory and initialize the contents of the memory to 0;

open an already existing shared memory, if open without knowing the size of shared memory, you can specify size as 0, SHMFLG can be specified as 0 (according to the default permissions to open);     

Parameters:

key: This shared memory segment name;

Size: Shared memory sizes (bytes);

SHMFLG: Usage similar to MSGFLG parameters in Msgget;

return value:

a non-negative integer, the identifier of the shared memory segment, was successfully returned; 1 failure return

/** Example: Create and open a shared memory **/int main (int Argc,char **argv) {    const int shm_size = 1024x768;    int shmid = Shmget (0x1234, Shm_size, 0666| Ipc_creat);    if (Shmid = =-1)        err_exit ("Shmget error");    cout << "Share memory Get Success" << Endl;}

Shmat

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

Connect to the address space of this process, after a successful connection, the operation of the memory is very similar to that of malloc, and if there is data in this memory, it can be taken directly out of the data!!

Parameters:

shmaddr: Specify the address of the connection (Null is recommended)

SHMFLG: The general designation is 0, which means readable and writable; And two of its other possible values are Shm_rnd and shm_rdonly (see below)

return value:

successfully Returns a pointer to the shared memory start address; failed return (void *)-1


SHMADDR and SHMFLG combination description

SHMADDR is null

The Linux kernel automatically connects to the process's memory for the process (recommended)

Shmaddr NOT NULL and SHMFLG no shm_rnd tag

Use SHMADDR as the connection address

SHMADDR is not null and the shm_rnd tag is set SHMFLG

The connected address is automatically adjusted downward to an integer multiple of Shmlba;

Formula:shmaddr-(shmaddr% Shmlba)

Shmlba is the size of the memory page (4K)

shmflg=shm_rdonly

Read-only shared memory, otherwise it is readable, writable, note: There is no readable, can write this concept

Shmdt

int SHMDT (const void *SHMADDR);

Parameters:

SHMADDR: The pointer returned by Shmat

Note: removing shared memory segments from the current process does not equal the deletion of shared memory segments

/** example: writing data to/    read out Shared Memory program write: Writes data to the shared memory program read: Reads the data out of shared memory (of course, can read n multiple times) **///write program struct student{char name[26]; int age;}; int main (int argc,char **argv) {int shmid = Shmget (0x1234, sizeof (Student), 0666|    Ipc_creat);    if (Shmid = =-1) err_exit ("Shmget error");    Connect the shared memory in a readable, writable manner Student *p = (Student *) Shmat (shmid, NULL, 0);    if (p = = (void *)-1) err_exit ("Shmat error");    strcpy (P->name, "Xiaofang");    P->age = 22; SHMDT (P);} 
The Read program int main (int argc,char **argv) {    int shmid = shmget (0x1234, 0, 0);    if (Shmid = =-1)        err_exit ("Shmget error");    Connect the shared memory in read-only mode    Student *p = (Student *) Shmat (shmid, NULL, 0);    if (p = = (void *)-1)        err_exit ("Shmat error");    Print out the content directly    cout << "Name:" << p->name << ", Age:" << p->age << Endl;    SHMDT (P);}

Shmctl

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

Set/Get Shared memory properties

Parameters:

cmd: Action to be taken (see below for three values)

BUF: A data structure that points to a mode state and access rights that hold shared memory

System v Shared Memory Summary:

1. Shared memory is occupied by another program, the shared memory will not be deleted immediately (reference count count);

2. A phenomenon occurs when the shared memory key becomes 0x00000000 and becomes private;

3. It is also readable at this time, but there must be a way to get the ID of the shared memory (SHMID), because it is a waste of time trying to get the shared memory through the key of the shared memory!

/** Example: Delete shared memory **/int main (int Argc,char *argv[]) {    int shmid = shmget (0x1234, 0, 0);    if (Shmid = =-1)        err_exit ("Shmget error");    if (Shmctl (Shmid, Ipc_rmid, NULL) = =-1)        err_exit ("Shmctl ipc_rmid error");    cout << "Share memory Remove success" << Endl;}

Linux IPC Practice (9)--system v shared memory

Related Article

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.