【问题标题】:Not able to retrieve data from shared memory无法从共享内存中检索数据
【发布时间】:2013-05-09 07:20:56
【问题描述】:

有人可以指点一下我可能会出错的地方。我试图将指向结构类型元素的指针存储在共享内存中。但是在获取相同的内容时,我得到的只是零。

代码:

#include<iostream>
#include<cstdio>
#include<sys/shm.h>
#include<sys/stat.h>

using namespace std;
typedef struct demo
{
    int sensorID;
    float value;
    int time;
}demo;

int main()
{
    key_t key;
    int shmid;
    demo *ptr;

    key = ftok("/home/dilbert/work",'R');
    shmid = shmget(key,4096*2, 0755 | IPC_CREAT);
    ptr = (demo*)shmat(shmid, (void*)0, 0); //Is this step right?
                                            //I casted the void ptr into demo ptr type
    if(ptr == (demo*)(-1))                  
            perror("shmat");
    demo *pos = ptr;
    for(int i=0; i<10; ++i)
    {
            demo *A=new demo;  //Creating a struct elem
            A->sensorID=i+10;  //Storing some data
            A->value=2*i+98.344;
            A->time=3*i*1000;
            pos = A;           //Keeping the pointer to it in shared memory
            ++pos;             //Incrementing the pointer
    }

    pos = ptr;    //Reset the pointer back to start of shared memory. Might be going wrong here.
    for(int i=0; i<10; ++i)  //Now start printing the data.
    {
            cout<<"Sensor: "<<pos->sensorID<<"  Value: "<<pos->value<<"   Time: "<<pos->value<<"\n";
            ++pos;
    }
    //Just a demo program. So did not bother to delete the pointers in shared memory. I think I should because shared memory destruction will not call delete for its elements.
    shmdt(ptr);
    shmctl(shmid, IPC_RMID, NULL); 
    return 0;

}

我得到的结果是:

Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0
Sensor: 0  Value: 0   Time: 0

【问题讨论】:

    标签: c++ linux shared-memory


    【解决方案1】:

    您正在为 存储数据 的 for 循环中破坏 pos 的值。使用*pos = *A; 而不是pos = A;

    还想一想,您是要为A 保留新创建的内存的内存位置,还是要将A 中的数据存储到共享内存中。我的更改将存储数据。

    【讨论】:

    • 成功了。非常感谢。我正在考虑存储指向结构元素的指针,因为元素可能很大。因此正在考虑节省空间并让共享内存存储更多元素。
    【解决方案2】:

    在此处的代码中

    for(int i=0; i<10; ++i)
    {
            demo *A=new demo;  //Creating a struct elem
            A->sensorID=i+10;  //Storing some data
            A->value=2*i+98.344;
            A->time=3*i*1000;
            pos = A;           //Keeping the pointer to it in shared memory
            ++pos;             //Incrementing the pointer
    }
    

    您正在非共享内存中创建一个对象。此外,您没有将指针存储到共享内存中,您实际上是在修改指针本身(实际上是指向本地内存)。

    您是要存储实际对象还是仅存储指向共享内存的指针?如果您打算存储实际对象,则需要使用类似

    for(int i=0; i<10; ++i)
    {
            demo *A=new demo;  //Creating a struct elem
            A->sensorID=i+10;  //Storing some data
            A->value=2*i+98.344;
            A->time=3*i*1000;
            *pos = *A;         //Store object in shared memory
            ++pos;             //Incrementing the pointer
    }
    

    如果您尝试存储指针,请记住,您存储的指针几乎肯定会在另一个进程中无效,并且不会按预期工作。

    【讨论】:

    • 谢谢。有效。非常感谢。我正在考虑存储指针以使共享内存存储更多数据。由于结构的元素可以很大。指针将是固定大小的。但其他进程可能无法提取数据的事实令人担忧。将不得不检查更多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    相关资源
    最近更新 更多