C memory sharing across 2 programs

人盡茶涼 提交于 2019-12-11 06:53:58

问题


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];
}; 

回答1:


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).



来源:https://stackoverflow.com/questions/9660503/c-memory-sharing-across-2-programs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!