进程间通信(IPC)之共享内存

共享内存有如下API

// 创建或获取一个共享内存;成功返回共享内存ID,失败返回-1
int shmget(key_t key, size_t size, int flag);

// 连接共享内存到当前进程的地址空间;成功返回指向共享内存的指针,失败返回 -1
void *shmat(int shmid, const void *addr, int flag);

//断开与共享内存的连接;成功返回0,失败返回-1
int shmdt(const void *addr);

// 控制共享内存的相关信息;成功返回0,失败返回-1
int shmctl(int shmid, int cmd, struct shmid_ds *buf);

具体步骤:

  1. 创建共享内存/打开 (shmget)

  1. 映射 (shmat)

  1. 数据交互 (stycpy)

  1. 释放共享内存 (shmdt)

  1. 删除 (shmctl)

具体例子:

写:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.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);

int main()
{
        int shmid;
        char *shmaddr;

        key_t key;
        key = ftok(".", 1);
        // 1. 打开/创建
        shmid = shmget(key, 1024*4, IPC_CREAT|0666);
        if(shmid == -1){
                printf("shmget noOK\n");
                exit(-1);
        }
        // 2. 映射
        shmaddr = shmat(shmid, 0, 0);
        printf("shmat ok\n");

        // 3. 数据交互
        strcpy(shmaddr, "hello");
        sleep(5);
        // 4. 释放共享内存
        shmdt(shmaddr);
        // 5. 删除
        shmctl(shmid, IPC_RMID, 0);
        printf("quit \n");
        return 0;
}

读:

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdlib.h>
#include <string.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);

int main()
{
        int shmid;
        char *shmaddr;

        key_t key;
        key = ftok(".", 1);
        shmid = shmget(key, 1024*4, 0);
        if(shmid == -1){
                printf("shmget noOK\n");
                exit(-1);
        }
        shmaddr = shmat(shmid, 0, 0);
        printf("shmat ok\n");
        printf("data :%s\n",shmaddr);
        shmdt(shmaddr);
        printf("quit \n");
        return 0;
}

用命令 ipcs可以查看进程间通信的共享内存的使用情况 (也可以查看信号量、消息队列的使用情况)

pcrm命令可以移除一个消息对象。或者共享内存段,或者一个信号集,同时会将与ipc对象相关链的数据也一起移除。当然,只有超级管理员,或者ipc对象的创建者才有这项权利。

但共享内存的使用会面临同步机制,需要与其它通信方式配合,最合适的就是信号,当然,互斥锁也可以解决这个问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值