[Notes] Shared Memory (SHM)

First, the characteristics

  Shared memory allows a number of different processes can access the same memory. Compared to other forms of IPC, high speed, high efficiency characteristics, decreases in memory shared memory in large-scale data processing consumption.

Second, create a shared memory

  1, header files

    #include <sys/ipc.h>

    #include <sys/shm.h>

    #include <sys/types.h>

  2, function

    ftok key_t (pathname const char *, int PROJ_ID);   ID value necessary for creating IPC communication.

      pathname: Specifies the existing file name, typically the current directory

      proj_id: sequence number, size 1-255.

      Return Value: value of ID, size before inode number plus the sequence number, for example: pathname inode number is 0x101010, proj_id is 0x32, the ID is 0x32101010.

    int shmget (key_t key, size_t size  , int shmflg);  create or open a shared memory

      key: identifier, each IPC object corresponding to a key, the size is not an integer of 0

      size: Specifies the shared memory capacity

      shmflg: permission flags, and mode of function parameters open similar. IPC_CREAT, if memory key identifier does not exist, it is created. IPC_EXCL, memory key identifier if there is, then the error, errno is EEXIST.

      Return value: the successful return of the shared memory identifier, failed to return -1.

    void * shmat (int shmid, const  void * shmaddr, int shmflg);  the shared memory object is mapped into the address space of the calling process

      shmid: shared memory identifier.

      shmaddr: Specifies the shared memory connected to the address location of the current process, which can be NULL, indicating that the system selected shared memory address

      shmflg: permission flags, SHM_RND read and write, SHM_RDONLY read-only.

      Return value: the successful return of the first address of shared memory, failure to return -1.

      Note: fork after the child process inherit the shared memory address is connected. After the child process exec with shared memory address it is automatically connected off (detach). After the end of the process, the shared memory address is automatically connected off (detach)

    int shmdt (const void * shmaddr) ;   cancel the association process with the shared memory.

      shmaddr: shared memory of the first address.

      Return Value: returns 0 on success, -1 failure.

      Note: shmdt does not remove the shared memory, but the current process with separate shared memory.

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

      shmid: shared memory identifier.

      cmd: IPC_STAT, to obtain the status of shared memory, the shared memory copy of the configuration shmid_ds buf. To IPC_SET, changing the status of shared memory, copy shmid_ds buf structure referred to in the uid, gid, mode shmid_ds into the shared memory structure. IPC_RMID, this logo will delete shared memory.

      buf: shared memory management structure

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 */
    ...
};

struct ipc_perm {
    key_t          __key;    /* Key supplied to shmget(2) */
    uid_t          uid;      /* Effective UID of owner */
    gid_t          gid;      /* Effective GID of owner */
    uid_t          cuid;     /* Effective UID of creator */
    gid_t          cgid;     /* Effective GID of creator */
    unsigned short mode;     /* Permissions + SHM_DEST and
                                SHM_LOCKED flags */
    unsigned short __seq;    /* Sequence number */
};

      Note: IPC_RMID only the shared memory marked for deletion. If the shared memory is only connected to the current process, then delete it. If there are other processes, it will be deleted from the current process connection, and shared memory identifier to 0, indicating that it is a PRIVATE state to prevent other processes and then connect the shared memory, waiting for other processes to disconnect after you remove the shared memory .

Check the status of shared memory: ipcs command

Guess you like

Origin www.cnblogs.com/qiu00/p/11621225.html