主题 : QSharedMemory共享问题 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 73530
精华: 0
发帖: 44
金钱: 225 两
威望: 45 点
综合积分: 88 分
注册时间: 2012-07-10
最后登录: 2014-09-03
楼主  发表于: 2014-01-07 22:31

 QSharedMemory共享问题

图片:
我在做一个东西,我想这样实现,用eclipse写了一个关于C的程序,调试并且能够正常工作了,现在编写界面程序,将前面程序计算得到的一些数据显示出来,并且动态显示,我想使用共享内存,因为我前面C程序中有大量的原始数据,用共享的方法来的更好,可是现在的问题是QT程序如何去共享到同一片内存,我使用的是qt4.7,一直以为在QT中也可以用shmget();结果发现不能,编译时没有问题,但是运行后就有问题了,共享内存打不开。连申请都失败。
源程序如下:


#include "mainwindow.h"
#include "ui_mainwindow.h"


#define        MY_SHM_ID    67888
#define        MY_SHM_ID2    67898
//注意这里的字节,但是在共享内存中使用到是int格式,所以实际能够存的数据DRAW_DATA_MAX = 5110/4约为1024
#define        MEMY_SIZE    5120        //每个波形采集160个点,共存储30个波形5120

typedef struct  com_share_ram
{
        int        dat1;
        int        dat2;
        int        dat3;
        int        dat4;
        int        dat5;
        int        count;
        int        dat7;
        int        dat8;
        int        dat9;
        int        dat10;
        int        gz_tab[1024];
}share_com3;

share_com3 *addcom;
int     *share_memory ;//共享到原始数据(read_com)
unsigned char   count_xon;


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //shareMemory.setKey("67888");

    timer=new QTimer();
    connect(timer,SIGNAL(timeout()),this,SLOT(timerDone()));
    timer->start(200);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::timerDone()
{
    count_xon++;
     ui->lcdNumber->display(count_xon);
    shareramget();
}


int MainWindow::creat_data_draw(int pt[])
{
    unsigned char i;

    for(i=0;i<64;i++)
    {
        pt = (50 * sin((2 * 3.1416 * i)/64)) + 160;
    }

    return 0;
}

int MainWindow::shareramget(void)
{
    int shmid;

    if(share_memory <= 0)
    {
        shmid=shmget(MY_SHM_ID,MEMY_SIZE,0666|O_CREAT);
        if(shmid<0)
        {
            printf("sorry get the memroy failed! sindraw\n");
            return -1;
        }
        //shmid=shmget(MY_SHM_ID,MEMY_SIZE,IPC_CREAT);

        share_memory=(int *)shmat(shmid,0,0);
        if(share_memory<0)
        {
            printf("sorry ram shmat failed! sindarw\n");
            return -2;
        }

        addcom = (struct  com_share_ram*)share_memory;
        creat_data_draw(addcom->gz_tab);
        addcom->count = 64;
    }
    else
    {
        ui->lcdNumber->display(10);
        printf("good ram has ok! sindarw\n");
    }
}
运行的结果是这样的:见截图

提示是这样的:
Starting /fst/sharememory/sharem-build-desktop/sharem...
sorry get the memroy failed! sindraw
sorry get the memroy failed! sindraw
sorry get the memroy failed! sindraw
sorry get the memroy failed! sindraw
sorry get the memroy failed! sindraw
sorry get the memroy failed! sindraw
sorry get the memroy failed! sindraw
/fst/sharememory/sharem-build-desktop/sharem exited with code 0


求大牛指导!
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2014-01-08 13:06

 回 楼主(玉林腾华) 的帖子

试试把
复制代码
  1. shmid=shmget(MY_SHM_ID,MEMY_SIZE,0666|O_CREAT);

改成
复制代码
  1. shmid=shmget(MY_SHM_ID,MEMY_SIZE,0666|IPC_CREAT);


关于shmget的用法,参考man shmget的输出
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 73530
精华: 0
发帖: 44
金钱: 225 两
威望: 45 点
综合积分: 88 分
注册时间: 2012-07-10
最后登录: 2014-09-03
2楼  发表于: 2014-01-08 20:50
图片:
多谢大神指导,成了,跪拜!
[ 此帖被玉林腾华在2014-01-08 22:42重新编辑 ]
级别: 新手上路
UID: 73530
精华: 0
发帖: 44
金钱: 225 两
威望: 45 点
综合积分: 88 分
注册时间: 2012-07-10
最后登录: 2014-09-03
3楼  发表于: 2014-01-08 22:45
现在又遇到这样一个问题,在paintEvent()中,不能使用上面定义的共享内存,不知道是什么原因,跪拜!
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
4楼  发表于: 2014-01-09 12:11

 回 3楼(玉林腾华) 的帖子

我想你最好把具体的错误信息和相关的代码贴出来。
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 73530
精华: 0
发帖: 44
金钱: 225 两
威望: 45 点
综合积分: 88 分
注册时间: 2012-07-10
最后登录: 2014-09-03
5楼  发表于: 2014-01-10 21:35
呵呵谢谢,我用另外的方法做了一界面