2012-03-12 34 views
1

我有2個程序作家和讀者。作者應該創建共享內存,然後將結構數組保存到該內存中......讀者應該使用該內存並能夠輸出寫入者保存在內存中的內容。我非常努力地輸出更多,然後只是數組的第一部分,所以我甚至不知道數組是否正確保存到共享內存,所以我說我會在這裏發佈我的代碼,也許有人可以幫助我出...C程序存儲器共享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); 
} 

部首:

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

爲了方便,請告訴我們你的輸出是什麼。 – 2012-03-12 01:17:36

回答

2

問題是你的pdata是一個指向數組結構的指針數組,當你做shmat()時,你只設置數組中的第一個指針(* pdata)。所以當你寫入結構體時,只有零點實際上進入共享內存,其他的則進入了你之前malloc'd的空間(你不應該這樣做)。

正確的方法是這樣的:

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 2012-03-12 02:18:28