两个程序共享C内存

时间:2012-03-12 00:56:17

标签: c memory shared-memory

我有2个程序作者和读者。编写器应该创建共享内存,然后将结构数组保存到那段内存中...读者应该使用那段内存并能够输出作者在内存中保存的内容。我非常努力输出更多,然后只是数组的第一部分,所以我甚至不确定数组是否正确保存到共享内存所以我说我会在这里发布我的代码,也许有人可以帮助我出...

作者:

#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);

}

读者:

#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 个答案:

答案 0 :(得分:2)

问题是你的pdata是一个指向结构的指针数组,当你执行shmat()时,你只设置数组中的第一个指针(* pdata)。因此,当你写入结构时,只有零'实际进入共享内存,其他的进入你前面的malloc(你不应该这样做)。

正确的方法是这样的:

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

}

这会将所有结构存储在共享内存中,而不仅仅是指针。

随着对读者的适当改变,这是有效的(我现在将其留作练习)。