Linux 共享内存函数

mmap 函数实现共享内存

1. 匿名mmap

  • 匿名存储映射:调用mmap函数时,指定 MAP_ANONYMOUS标识,并将文件描述符设置为-1。结果得到的是一个匿名共享存储区,是可被后代进程共享的存储区域。

接口

void * Shm_Mmap_Anon(size_t len, int prot, int flags)
{
    void *area = NULL;
    if(MAP_FAILED == (area = mmap(0, len, prot, flags | MAP_ANONYMOUS, -1, 0)))
    {
        perror("mmap error:");
        exit(-1);
    }

    return area;
}

2. 有名mmap

mmap函数

#include 

void *mmap(void *addr, size_t length, int prot, int flags,
                  int fd, off_t offset);

参数
addr:指定映射存储区的起始地址,一般指定为NULL,说明由系统指定。
len:映射存储区的长度。
prot:PROT_READ , PROT_WRITE, PROT_EXEC, PROT_NONE
flags:MAP_PRIVATE, MAP_SHARED, MAP_ANONYMOUS
fd:映射的文件描述符
offset:映射文件的其实位置,一般为0

munmap函数

#include 

int munmap(void *addr, size_t length);

参数
addr:起始地址
length:长度

shm系列函数实现共享内存

API接口
shm.h

#ifndef _SHM_H
#define _SHM_H

void * Shm_Mmap_Anon(size_t len, int prot, int flags);
void * Shm_Mmap_File(void *addr, size_t len, int prot, int flags, int fd, off_t offset);
int    Shm_Munmap(void *addr, size_t len);

int Shm_Get(key_t key, size_t size, int flag);
void *Shm_At(int shmid, const void *addr, int flag);
int Shm_Ctl(int shmid, int cmd, struct shmid_ds *buf);
int Shm_Remove(int shmid);
int Shm_Dt(const void *addr);

#endif

shm.c

#include 
#include 
#include 
#include 
#include 
#include 

#include "shm.h"


void * Shm_Mmap_Anon(size_t len, int prot, int flags)
{
    void *area = NULL;
    if(MAP_FAILED == (area = mmap(0, len, prot, flags | MAP_ANONYMOUS, -1, 0)))
    {
        perror("mmap error:");
        exit(-1);
    }

    return area;
}


void * Shm_Mmap_File(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
{
    void *area = NULL;
    if(MAP_FAILED == (area = mmap(addr, len, port, flags, fd, offset)))
    {
        perror("mmap error:");
        exit(-1);
    }

    return area;
}


int   Shm_Munmap(void *addr, size_t len)
{
    int val = 0;
    val = munmap(addr, len);
    if(val < 0)
    {
        perror("munmap error:");
    }

    return val;
}


int Shm_Get(key_t key, size_t size, int flag)
{
    int shmid = 0;
    shmid = shmget(key, size, flag);
    if(shmid < 0)
    {
        perror("shmget error:");
    }

    return shmid;
}


void * Shm_At(int shmid, const void *addr, int flag)
{
    void *area = NULL;
    area = shmat(shmid, addr, flag);
    if(area == NULL)
    {
        perror("shmat error:");
        exit(-1);
    }   

    return area;
}


int Shm_Ctl(int shmid, int cmd, struct shmid_ds *buf)
{
    int val = shmctl(shmid, cmd, buf);
    if(val < 0)
    {
        perror("shmctl error:");
    }

    return val;
}


int Shm_Remove(int shmid)
{
    int val = shmctl(shmid, IPC_RMID, 0);
    if(val < 0)
    {
        perror("shmctl error:");
    }

    return val;
}

//如果成功,shmdt将使相关shmid_ds 结构中的shm_nattch 计数器值减 1,类似于unlink的用法
int Shm_Dt(const void *addr)
{
    int val = shmdt(addr);
    if(val < 0)
    {
        perror("shmdt error:");
    }

    return val;
}

例程

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include "shm.h"

int main(void)
{
    key_t key = 0;
    pid_t pid = 0;
    int shmid = 0;

    key = ftok("/home", 0x01);
    shmid = shmget(key, 20, IPC_CREAT | 0666);
    void *mm0 = Shm_Mmap_Anon(20, PROT_READ | PROT_WRITE, MAP_SHARED);
    void *mm2 = Shm_At(shmid, NULL, 0);
    sem_unlink("/home");
    char str[] = "Welcome to the world!\n";

    pid = fork();

    if(0 == pid)
    {
        key = ftok("/home", 0x01);
        shmid = shmget(key, 20, IPC_CREAT | 0666);
        //void *mm0 = Shm_Mmap_Anon(20, PROT_READ | PROT_WRITE, MAP_SHARED);
        void *mm2 = Shm_At(shmid, NULL, 0);
        char arr[] = "This is magic world!\n";

        sem_t *sem = sem_open("/home", O_CREAT, 0666, 1);
        
        while(1)
        {
            sem_wait(sem);
            //printf("%s", mm0);
            printf("%s", mm2);
            //memcpy(mm0, arr, strlen(arr) + 1);
            memcpy(mm2, arr, strlen(arr) + 1);
            usleep(1000000);
            //printf("%s===========\n", mm0);
            printf("%s===========\n", mm2);
            sem_post(sem);
            usleep(1000000);
        }
    }

    sem_t *sem = sem_open("/home", O_CREAT, 0666, 1);
    while(1)
    {
            sem_wait(sem);
            //printf("%s", mm0);
            printf("%s", mm2);
            //memcpy(mm0, str, strlen(str) + 1);
            memcpy(mm2, str, strlen(str) + 1);
            usleep(1000000);
            //printf("%s###########\n", mm0);
            printf("%s###########\n", mm2);
            sem_post(sem);
            usleep(1000000);
    }

    return 0;

}

你可能感兴趣的:(Linux 共享内存函数)