2012-03-12 2 views
1

私はライターとリーダーの2つのプログラムを持っています。ライターは、共有メモリを作成し、そのメモリに構造体の配列を保存することになっています。リーダーは、そのメモリを使用して、ライターがメモリに保存したものを出力できるようになっています。私は非常に配列の最初の部分だけを出力するために非常に苦労しているので、配列が共有メモリに正しく保存されているかどうかもわからないので、私はここに私のコードを掲示し、アウト...2つのプログラム間でCのメモリ共有

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

便宜のために、出力が何であるか教えてください。 –

答えて

2

問題は、あなたのpdataが構造体へのポインタの配列であり、shmat()を実行するときに、配列(* pdata)の最初のポインタだけを設定することです。だから、構造体に書き込むときには、実際には0番目の部分だけが共有メモリに入り、他の部分は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); 

} 

読者の適切な変更に加えて、これは機能します(これについては、今のところあなたの練習として残します)。

+1

非常に助けてくれてありがとう – kev670

関連する問題