如何映射类成员变量?

发布于 2025-01-15 04:57:18 字数 1886 浏览 2 评论 0原文

我有一个带有成员变量的类,我想将成员变量放入共享内存中,以便其他进程可以访问它。

这是我的代码:

pro.cpp:

#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<bits/stdc++.h>
using namespace std;

struct Data {
  int a = 0;
  void print() const { cout << a << endl; }
};

class A{
 public:
  A() {
    // i want to mmap d_ into a file, so the other process can read d_;
    int fd = open("shared_data", O_CREAT | O_RDWR | O_TRUNC, 0777);
    lseek(fd, sizeof(d_), SEEK_SET);
    write(fd,"",1);
    mmap(&d_, sizeof(d_), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);  // I want to mmap d_'s memory here
  }
  void revise_d(int v) {d_.a = v;}
 private:
  Data d_; // i want to share this in shared memory, so con.cpp can get it
};

int main() {
  A a;
  a.revise_d(3);  // in my imagination, this will revise d, and in con.cpp, it can be informed
  while(1);
}

//con.cpp

#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
using namespace std;

struct Data {
  int a = 0;
  void print() const { cout << a << endl; }
};

int main() {
  int fd = open("shared_data", O_RDWR, 0777);
  Data* p = (Data*)mmap(NULL, sizeof(Data), PROT_READ, MAP_SHARED, fd, 0); 
  close(fd);
  std::string s;
  while (std::cin >> s && s != "quit") {
    p->print();  // i want p can get the update from pro, should be 3
    // but it is actually 0  !!!!!
  }
}

然后我遵守了:

g++ pro.cpp -o pro
g++ con.cpp -o con

我运行它们:

./pro

没有输入,pro只是

./con

在我输入键盘时修改d_的数据,它将输出d_的数据。

在我的想象中,con会捕获d_的更新,它应该输出3。

但结果是0,这意味着没有捕获到更新。

你能帮忙吗?我如何共享类的成员变量?

附:我希望不要增加内存成本,这意味着重用成员变量的内存是最好的选择。

I have a class with a member variable, I want to put the member vaiable into shared memory, so that the other process can access it.

here is my code:

pro.cpp:

#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
#include<bits/stdc++.h>
using namespace std;

struct Data {
  int a = 0;
  void print() const { cout << a << endl; }
};

class A{
 public:
  A() {
    // i want to mmap d_ into a file, so the other process can read d_;
    int fd = open("shared_data", O_CREAT | O_RDWR | O_TRUNC, 0777);
    lseek(fd, sizeof(d_), SEEK_SET);
    write(fd,"",1);
    mmap(&d_, sizeof(d_), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);  // I want to mmap d_'s memory here
  }
  void revise_d(int v) {d_.a = v;}
 private:
  Data d_; // i want to share this in shared memory, so con.cpp can get it
};

int main() {
  A a;
  a.revise_d(3);  // in my imagination, this will revise d, and in con.cpp, it can be informed
  while(1);
}

// con.cpp

#include<sys/mman.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
using namespace std;

struct Data {
  int a = 0;
  void print() const { cout << a << endl; }
};

int main() {
  int fd = open("shared_data", O_RDWR, 0777);
  Data* p = (Data*)mmap(NULL, sizeof(Data), PROT_READ, MAP_SHARED, fd, 0); 
  close(fd);
  std::string s;
  while (std::cin >> s && s != "quit") {
    p->print();  // i want p can get the update from pro, should be 3
    // but it is actually 0  !!!!!
  }
}

then i complied:

g++ pro.cpp -o pro
g++ con.cpp -o con

i run them as:

./pro

there is no input, pro just revise d_'s data

./con

when i type keyboard, it will output the d_'s data.

in my imaginition, the con will catch the update of d_, it should output 3.

but the result is 0, which means the update not be caught.

could you help on this? how can i shared a class's member variable?

ps. I perfer not increased memory cost, which means reuse the member variable's memory is the best choice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文