首页 > 系统相关 >C++ Linux下使用共享内存

C++ Linux下使用共享内存

时间:2022-08-15 15:45:20浏览次数:70  
标签:共享内存 memory C++ Linux test shared include shm

  • 在Linux下可以使用System V共享内存段实现共享内存,一共有4个API:
    • 创建共享内存段或使用已经创建的共享内存段-shmget()
    • 将进程附加到已经创建的共享内存段-shmat()
    • 从已连接的共享内存段中分离进程-shmdt()
    • 共享内存段上的控制操作-shmctl()
  • 使用System V共享内存段需要引入头文件:
#include <sys/shm.h>
  • 多读者和一个写者的样例
#include <sys/shm.h>
#include <unistd.h>
#include <error.h>
#include <iostream>
#include <string>
using namespace std;

#define BUF_SIZE 1024
#define SHM_KEY 114514

void reader(int seq) {
    cout << "reader " << seq << endl;
    int shm_id = shmget(SHM_KEY, BUF_SIZE, IPC_CREAT);
    if (shm_id == -1) {
        perror("Fail to get shared memory id");
        return;
    }

    // Attach to the segment to get a pointer to it.
    char* shm_buf = static_cast<char*>(shmat(shm_id, NULL, 0));
    if (shm_buf == nullptr) {
        perror("Fail to attach shared memory");
        return;
    }

    /* Transfer blocks of data from shared memory to stdout*/
    while (1) {
        cout << "buf[" << seq << "] = " << shm_buf << endl;
        if (string(shm_buf) == to_string(seq)) {
            break;
        }
        sleep(3);
    }
    cout << "Detaching shared memory" << endl;
    if (shmdt(shm_buf) == -1) {
        perror("Fail to detaching shared memory");
        return;
    }
}

void writer() {
    int shm_id = shmget(SHM_KEY, BUF_SIZE, IPC_CREAT);
    if (shm_id == -1) {
        perror("Fail to get shared memory id");
        return;
    }

    // Attach to the segment to get a pointer to it.
    char* shm_buf = static_cast<char*>(shmat(shm_id, NULL, 0));
    if (shm_buf == nullptr) {
        perror("Fail to attach shared memory");
        return;
    }
    /* Transfer blocks of data from shared memory to stdout*/
    while (1) {
        cin >> shm_buf;
    }
    cout << "Detaching shared memory" << endl;
    if (shmdt(shm_buf) == -1) {
        perror("Fail to detaching shared memory");
        return;
    }
}

int main(int argc, char* argv[]) {
    if (argc < 2) {
        return 0;
    }
    srand(::time(nullptr));
    cout << argv[1] << endl;
    string argv1 = argv[1];
    if (argv1 == "reader") {
        reader(rand() % 1000);
    } else {
        writer();
    }
    return 0;
}
  • 将文件命名为shm_test.cpp,编译后运行
./shm_test reader
./shm_test writer
  • 可以看到读者能够读取到写者写入的数据
  • 如果报错显示
    • Fail to get shared memory id: Permission denied
  • 那么执行
sudo ./shm_test reader
sudo ./shm_test writer

标签:共享内存,memory,C++,Linux,test,shared,include,shm
From: https://www.cnblogs.com/umichan0621/p/16588431.html

相关文章

  • C++动态链接库(DLL)文件的创建和调用
    出处:蓟_可爱的叔https://www.cnblogs.com/wjq13752525588/p/16364956.html 一、什么是库    我们在编写C/C++等语言程序的时候,经常会遇到很多反复使用的或者......
  • Linux用户管理
    Linux用户管理概念用户名:用户的名称用户组:当前用户所在的组用户的家目录:当前账号登录成功之后的目录常用操作sudouseradd名添加用户sudopasswd设置密码s......
  • linux通过xshell远程登录失败
    状态描述:确认ssh配置完好,重启后查看日志也正常。openssh版本8.5。通过xshell远程连接,提示服务器拒绝连接。处理过程:由于是虚拟机,之前通过vnc连接的一个终端还未关闭。修......
  • linux 使用docker 容器部署sqlserver2019
    前提安装完docker,安装完docker-composedocker-compose.ymlversion:"2"services:sqlserver-2019:restart:always#容器名称container_name:sqlser......
  • Linux锁定、解锁和查询账号锁定状态
    Linux锁定、解锁和查询账号锁定状态passwd-l锁定passwd-u解锁passwd-S查询锁定状态......
  • uboot\linux\gcc 云盘分享
    阿里云盘:https://www.aliyundrive.com/s/iEqEXPMix9o提取码:46jh2022-08-15:uboot:u-boot-2010.03.tar.bz2u-boot-2013.10.tar.bz2u-boot-2016.11.tar.bz2u-boot-20......
  • c++标准IO 中的string流
    c++标准IO中的string流sstream头文件sstream头文件定义了三个类型来支持内存和string之间的IO,在用户看来,string类就像是一个IO流一样。istringstream处理行内的多......
  • 基于C++的OpenGL 10 之光照贴图
    1.引言本文基于C++语言,描述OpenGL的光照贴图前置知识可参考:基于C++的OpenGL09之材质-当时明月在曾照彩云归-博客园(cnblogs.com)笔者这里不过多描述每个名词......
  • linux下运行一个java类
    java代码,使用了jdk的包,以及第三方jar包(user.jar)中的类,以下例子为所有文件均在同一目录下。也可以不在同一目录,执行命令时需指定绝对路径即可。importa.b.User;import......
  • linux系统下还原oracle数据库
    报错信息:Copyright(c)1982,2011,Oracleand/oritsaffiliates.Allrightsreserved.UDI-28009:operationgeneratedORACLEerror28009ORA-28009:connection......