I2C存储器在2个程序共享 [英] C memory sharing across 2 programs

查看:136
本文介绍了I2C存储器在2个程序共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个节目的作家和读者。作家应该创建共享内存,然后结构的数组保存到一块内存...读者应该使用一块内存,并能够输出什么是由作家保存在存储器中。我非常努力输出更多的则只是数组的第一个部分,所以我甚至不知道如果阵列正确保存到共享内存中,所以我说我会在这里,也许有人张贴我的code可以帮助我...

编剧:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / wait.h>
#包括LT&;&ASSERT.H GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&; SYS / shm.h>
#包括LT&;&unistd.h中GT;
的#includeheader.h
诠释的main()
{
    关键的key_t = 1234;
    INT的shmid;
    INT I = 0;
    INT磷;
    结构公司信息* PDATA [4];    对于(I = 0; I&小于5;我+ +)
    {
        PDATA [I] =的malloc(sizeof的(结构公司信息));
        P = sizeof的(结构公司信息);
        //输出(大小数:%d \\ n,p)的;
        //的printf(外观:%X \\ n,PDATA [I]);
    }    INT sizeOfCompanyInfo = sizeof的(结构公司信息);    INT sizeMem = sizeOfCompanyInfo * 5;    的shmid = shmget的(键,sizeMem,0644 | IPC_CREAT);
    如果(的shmid == -1)
    {
        PERROR(shmget的);
        出口(1);
    }    * PDATA =(结构公司信息*)的shmat(的shmid,(无效*)0,0);
    如果(* PDATA ==(结构公司信息*)-1)
    {
        PERROR(schmat错误);
        出口(1);
    }    的strcpy(PDATA [0] - GT;公司名称,AIB);
    PDATA [0] - GT; sharePrice = 11.2;
    的strcpy(PDATA [1] - >公司名称,BOI);
    PDATA [1] - > sharePrice = 10.2;
    的strcpy(PDATA [2] - >公司名称,TSB);
    PDATA [2] - > sharePrice = 9.2;
    的printf(名字%s和%F \\ N中,pdata [0] - >公司名称,PDATA [0] - > sharePrice);
    的printf(名字%s和%F \\ N中,pdata [1] - >公司名称,PDATA [1] - > sharePrice);
    的printf(名字%s和%F \\ N中,pdata [2] - >公司名称,PDATA [2] - > sharePrice);    出口(0);}

读者:

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&; SYS / wait.h>
#包括LT&;&ASSERT.H GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&; SYS / types.h中>
#包括LT&; SYS / stat.h>
#包括LT&; SYS / shm.h>
#包括LT&;&unistd.h中GT;
的#includeheader.h诠释的main()
{
    关键的key_t = 1234;
    INT的shmid;
    INT sizeMem = 100;    结构公司信息* PDATA [4];    // INT sizeOfCompanyInfo = sizeof的(PDATA);    //的printf(大小数:%d \\ n,sizeOfCompanyInfo);    的shmid = shmget的(键,0,0);
    如果(的shmid == -1)
    {
        PERROR(shmget的);
        出口(1);
    }    * PDATA =(结构公司信息*)的shmat(的shmid,(无效*)0,0);
    如果(* PDATA ==(结构公司信息*)-1)
    {
        PERROR(错误的shmat);
        出口(1);
    }    输出(的ID为%d \\ n的shmid);    的printf(银行%s和%F \\ n,PDATA [0] - >公司名称,PDATA [0] - > sharePrice);
    的printf(银行%s和%d个\\ n,PDATA [1] - >公司名称,PDATA [1] - > sharePrice);
    的printf(银行%s和%d个\\ n,PDATA [2] - >公司名称,PDATA [2] - > sharePrice);
    出口(0);
}

标题:

 结构公司信息
{
    双sharePrice;
    焦炭的companyName [100];
};


解决方案

的问题是,你的PDATA是一个指针,以结构数组,当你做的shmat()您在数组中设置只有第一个指针(* PDATA)。所以,当你写的结构,只有第零实际上是要到共享内存中,其他人到前面malloc分配空间(你不应该做)。

正确的方法是这样的:

  INT的main()
{
    关键的key_t = 1234;
    INT的shmid;
    INT I = 0;
    INT磷;
    结构公司信息* PDATA;
    INT ncompanies = 5;    INT sizeMem = sizeof的(* PDATA)* ncompanies;    的shmid = shmget的(键,sizeMem,0644 | IPC_CREAT);
    如果(的shmid == -1)
    {
        PERROR(shmget的);
        出口(1);
    }    PDATA =(结构公司信息*)的shmat(的shmid,(无效*)0,0);
    如果(PDATA ==(无效*) - 1)
    {
        PERROR(schmat错误);
        出口(1);
    }    的strcpy(PDATA [0] .companyNameAIB);
    PDATA [0] .sharePrice = 11.2;
    的strcpy(PDATA [1] .companyNameBOI);
    PDATA [1] .sharePrice = 10.2;
    的strcpy(PDATA [2] .companyNameTSB);
    PDATA [2] .sharePrice = 9.2;    出口(0);}

本存储在共享内存中所有的结构,而不是仅仅三分球。

随着该作品(我会离开,作为一个练习对你,对现在)读者相应的改变。

I have 2 programs a writer and a reader. The writer is supposed to create shared memory and then save an array of structs to that piece of memory... reader is supposed to use that piece of memory and be able to output what was saved in the memory by the writer. I'm very much struggling to output more then just the first part of the array so I'm not even sure if the array is saving correctly to the shared memory so i said I'd post my code here and maybe someone could help me out...

WRITER:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <unistd.h>
#include "header.h"


int main()
{
    key_t key = 1234;
    int shmid;
    int i = 0;
    int p;
    struct companyInfo * pdata[4];

    for (i = 0; i < 5; i++)
    {
        pdata[i] = malloc(sizeof(struct companyInfo));
        p = sizeof(struct companyInfo);
        //printf("size: %d\n", p);
        //printf("look: %x\n", pdata[i]);
    }

    int sizeOfCompanyInfo = sizeof(struct companyInfo);

    int sizeMem = sizeOfCompanyInfo*5;

    shmid = shmget(key, sizeMem, 0644 | IPC_CREAT);
    if(shmid == -1)
    {
        perror("shmget");       
        exit(1);
    }

    *pdata = (struct companyInfo*) shmat(shmid, (void*) 0, 0);
    if(*pdata == (struct companyInfo*) -1)
    {
        perror("schmat error");
        exit(1);
    }

    strcpy(pdata[0]->companyName,"AIB");
    pdata[0]->sharePrice = 11.2;
    strcpy(pdata[1]->companyName,"BOI");
    pdata[1]->sharePrice = 10.2;
    strcpy(pdata[2]->companyName,"TSB");
    pdata[2]->sharePrice = 9.2;


    printf("name is %s and %f \n",pdata[0]->companyName,pdata[0]->sharePrice);
    printf("name is %s and %f \n",pdata[1]->companyName,pdata[1]->sharePrice);
    printf("name is %s and %f \n",pdata[2]->companyName,pdata[2]->sharePrice);

    exit(0);

}

READER:

#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <unistd.h>
#include "header.h"

int main()
{
    key_t key = 1234;
    int shmid;
    int sizeMem = 100;

    struct companyInfo * pdata[4];

    //int sizeOfCompanyInfo = sizeof(pdata);

    //printf("Size: %d\n", sizeOfCompanyInfo);

    shmid = shmget(key, 0, 0);
    if(shmid == -1)
    {
        perror("shmget");       
        exit(1);
    }

    *pdata = (struct companyInfo*) shmat(shmid,(void*)0,0);
    if(*pdata==(struct companyInfo*) -1)
    {
        perror("shmat error");
        exit(1);
    }

    printf("The id is %d\n",shmid);

    printf("Bank is %s and %f . \n",pdata[0]->companyName,pdata[0]->sharePrice);
    printf("Bank is %s and %d . \n",pdata[1]->companyName,pdata[1]->sharePrice);
    printf("Bank is %s and %d . \n",pdata[2]->companyName,pdata[2]->sharePrice);
    exit(0);
}

HEADER:

struct companyInfo
{
    double sharePrice;
    char companyName[100];
}; 

解决方案

The problem is that your pdata is an array of pointers to structs, and when you do the shmat() you set only the first pointer in your array (*pdata). So when you write to the structs, only the zero'th is actually going into shared memory, the others are going to the space you malloc'd earlier (which you shouldn't be doing).

The correct way is something like this:

int main()
{
    key_t key = 1234;
    int shmid;
    int i = 0;
    int p;
    struct companyInfo *pdata;
    int ncompanies = 5;

    int sizeMem = sizeof(*pdata) * ncompanies;

    shmid = shmget(key, sizeMem, 0644 | IPC_CREAT);
    if(shmid == -1)
    {
        perror("shmget");       
        exit(1);
    }

    pdata = (struct companyInfo*) shmat(shmid, (void*) 0, 0);
    if(pdata == (void*)-1)
    {
        perror("schmat error");
        exit(1);
    }

    strcpy(pdata[0].companyName,"AIB");
    pdata[0].sharePrice = 11.2;
    strcpy(pdata[1].companyName,"BOI");
    pdata[1].sharePrice = 10.2;
    strcpy(pdata[2].companyName,"TSB");
    pdata[2].sharePrice = 9.2;

    exit(0);

}

This stores all the structs in the shared memory rather than just pointers.

Along with appropriate changes to the reader this works (I'll leave that as an exercise for you, for now).

这篇关于I2C存储器在2个程序共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆