c++ 进程间通信 修改中间文件 访问共享内存

进程间通信:目前两个方案

方案一:通过中间文件,不打开文件前提下,判断文件修改时间

方案二:访问共享内存

实验结论,访问共享内存要快一些,但是都不会相差数量级

cpp 复制代码
#include <iostream>
#include <sys/stat.h> // For stat() function
#include <ctime>      // For time_t type
#include <chrono>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cstring> // For strcpy function

bool isFileModified(const char *filePath, time_t &lastModifiedTime)
{
    struct stat fileStat;
    if (stat(filePath, &fileStat) != 0)
    {
        // Failed to get file status
        return false;
    }

    // Compare current modification time with the stored one
    if (lastModifiedTime != fileStat.st_mtime)
    {
        // File has been modified
        lastModifiedTime = fileStat.st_mtime;
        return true;
    }

    // File has not been modified
    return false;
}

int main()
{
    const char *filePath = "example.txt";
    time_t lastModifiedTime = 0; // Store the last modified time of the file

    auto start_mem = std::chrono::steady_clock::now();
    // Check if the file has been modified
    if (isFileModified(filePath, lastModifiedTime))
    {
        std::cout << "File has been modified." << std::endl;
    }
    else
    {
        std::cout << "File has not been modified." << std::endl;
    }

    auto end_mem = std::chrono::steady_clock::now();
    auto mem_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_mem - start_mem);
    std::cout << "查看文件修改耗时: " << mem_duration.count() << " microseconds" << std::endl;
    key_t key = ftok("/tmp", 'A');
    int shm_id = shmget(key, 0, 0);
    if (shm_id == -1)
    {
        std::cerr << "Failed to get shared memory segment." << std::endl;
        return 1;
    }

    // 映射共享内存段到进程的地址空间中
    start_mem = std::chrono::steady_clock::now();
    char *shm_ptr = (char *)shmat(shm_id, NULL, 0);
    if (shm_ptr == (char *)-1)
    {
        std::cerr << "Failed to attach shared memory segment." << std::endl;
        return 1;
    }

    // 读取共享内存中的数据并打印到标准输出
    // std::cout << "Data in shared memory:" << std::endl;
    // std::cout << shm_ptr << std::endl;

    // start_mem = std::chrono::steady_clock::now();

    // 解除共享内存映射
    // shmdt(shm_ptr);

    // 删除共享内存段
    // shmctl(shm_id, IPC_RMID, NULL);

    end_mem = std::chrono::steady_clock::now();
    mem_duration = std::chrono::duration_cast<std::chrono::microseconds>(end_mem - start_mem);
    std::cout << "访问共享内存数据耗时: " << mem_duration.count() << " microseconds" << std::endl;
    std::cout << shm_ptr << std::endl;

    return 0;
}

编译

bash 复制代码
g++ test.cpp
./a.out
ipcs -m

输出结果

bash 复制代码
(base) mazu@tegra-ubuntu-s2:~/hq$ ./a.out
File has been modified.
查看文件修改耗时: 50 microseconds
访问共享内存数据耗时: 21 microseconds
Hello from shared memory!
(base) mazu@tegra-ubuntu-s2:~/hq$ ipcs -m

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status
0x411d0001 1          mazu       666        1024       0
相关推荐
Fuyo_11195 分钟前
C++ AVL树保姆级详解:从平衡因子到四大旋转机制
java·c++·算法
努力努力再努力wz16 分钟前
【Lua 入门系列】Lua 函数深入理解:函数对象、多返回值、可变参数、闭包、递归与尾调用优化
开发语言·lua
飞浪纪元[FWC–FE]1 小时前
从零到一键绿:Qt 5.14.2 + VS2019 开发环境搭建、Kit 套件配置与构建错误修复完全手册
开发语言·qt
Wang's Blog1 小时前
Java 项目实战:交友项目 - 联系人列表
java·开发语言·交友
傲世仙尊1 小时前
继承-类设计层次复用切片转换与菱形继承的死结
c++
我命由我123451 小时前
访客系统形式(有用户概念、无用户概念)
java·开发语言·前端·javascript·python·产品运营·产品经理
架构师伯子明1 小时前
FFmpeg C++学习(一):编译和demo
开发语言·c++·学习
海域云SeaArea_1 小时前
Windows Server 2008 R2 从零安装 IIS7.5(二):php.ini 配置
开发语言·windows·php
叼烟扛炮1 小时前
[LangChain] 学习笔记(02):LCEL 链式调用与动态模型配置实战
服务器·开发语言·网络·笔记·学习·langchain
xixingzhe21 小时前
图表中同比与环比的区别:看趋势还是看变化?
开发语言·python