簡體   English   中英

無法在共享 memory 中共享結構數組

[英]Can't shared an array of structs in shared memory

我創建了一個庫來初始化和填充 strutc 共享 2 進程數組。 問題是它不起作用,而是我創建了一個 int 數組並且它可以正常工作。 但是我需要共享 memory 中的這個數組,我想念什么?

EDIT: I have a theory but I need confirmation cause i'm studing now c and sh memory....is possible taht the problem are the transaction declared by pointer in the struct of block, and for that reason in the sh memory doesn '沒有任何結果?這個錯誤迫使進程因某種原因關閉?

編輯:所以我忘了張貼打印 function。 我將大小強制為 0,因為我只想插入 1 個元素並且至少只打印一個。 但是打印 function 在 if 之前的第一次打印之后卡住了,我不知道為什么。 我認為這是因為共享不起作用,但對於打印“Blocco n°0”,它表明Masterbook[i].id內部有一些價值。

編輯:更改了char*但也不起作用。

結構:

struct Transaction {
    char timestamp[24];
    int sender; /* pid user sent */
    int receiver;
    int reward;
    int money;
};

struct Block
{
    int id;
    struct Transaction* tr1;
    struct Transaction* tr2;
    struct Transaction* reward;
};

masterbook.c

    struct Block* MasterBook;
    int sizeMaster = -1;
        void initMasterBook(){
            int shmid,sizeid;
            shmid = shmget(SH_KEY_MASTERBOOK,MAX_MASTER*sizeof(struct Block),IPC_CREAT | 0666);
            if(shmid == -1) {
                perror("shmget error");
                exit(EXIT_FAILURE);
            }
            MasterBook = (struct Block*)shmat(shmid,NULL,0);
            if ( MasterBook == -1 ) {
                perror ( "Error in shmat: " );
                exit(EXIT_FAILURE);
            }
               
        }
        
        void freeMasterBook(){
            shmdt((void *) MasterBook); 
        }
        int addBlock(struct Block block){
            if(sizeMaster >= MAX_MASTER-1) return -1;
            ++sizeMaster;
            block.id = sizeMaster;
            MasterBook[sizeMaster] = block;
            return 0;
        }
int printMasterBook(){
    if(sizeMaster < 0) {
        printf("\nMasterbook vuoto");
        return -1;
    }
    int i = 0;
    for ( i; i <= sizeMaster; i++)
    {
        printf("\nBlocco n° : %d",MasterBook[i].id);
        
        if(MasterBook[i].tr1 != NULL){
            printf("\n\tTransazione n° 1 : ");
        struct Transaction *temp = MasterBook[i].tr1;
        printf("\n\t\tReceiver: %d",temp->receiver);
        printf("\n\t\tSender: %d",temp->sender);
        printf("\n\t\tReward: %d",temp->reward);
        printf("\n\t\tTimestamp: %s",temp->timestamp);
        }else printf("\nThere's no transaction 1 set!");

        if(MasterBook[i].tr2 != NULL){
            printf("\n\tTransazione n° 2 : ");
        struct Transaction *temp2 = MasterBook[i].tr2;
        printf("\n\t\tReceiver: %d",temp2->receiver);
        printf("\n\t\tSender: %d",temp2->sender);
        printf("\n\t\tReward: %d",temp2->reward);
        printf("\n\t\tTimestamp: %s",temp2->timestamp);
        }else printf("\nThere's no transaction 2 set!");

        if(MasterBook[i].reward != NULL){
            printf("\n\tTransazione Reward : ");
        struct Transaction *temp3 = MasterBook[i].reward;
        printf("\n\t\tReceiver: %d",temp3->receiver);
        printf("\n\t\tSender: %d",temp3->sender);
        printf("\n\t\tReward: %d",temp3->reward);
        printf("\n\t\tTimestamp: %s",temp3->timestamp);
        }else printf("\nThere's no transaction reward set!");
    
        printf("finish print");
    }
    return 0;
}

master.c:

initMasterBook();
printf("\nthe size id: %d",sizeMaster);
printMasterBook();
struct Block v;
struct Transaction t1;
t1.money = 4;
t1.receiver = 2;
t1.sender = 3;
strcpy(t1.timestamp,"ciao");
t1.reward = 4;
v.tr1 = &t1;
addBlock(v);
printMasterBook();
 printf("\nthe size id: %d",sizeMaster);

節點.c

int main(int argc, char const *argv[])
{
    initMasterBook();
    sizeMaster=0;
    printMasterBook();
    freeMasterBook();
    return 0;
}

我解決了 output 更改結構中的指針並重新排列代碼的問題。

struct Transaction {
    int empty;
    char timestamp[30];
    int sender; /* pid user sent */
    int receiver;
    int reward;
    int money;
};

struct Block
{
    int id;
    struct Transaction tr1;
    struct Transaction tr2;
    struct Transaction reward;
};

有問題的打印:

int printMasterBook(){
    if(sizeMaster < 0) {
        printf("\nMasterbook vuoto");
        return -1;
    }
    int i = 0;
    for ( i; i <= sizeMaster; i++)
    {
        printf("\nBlocco n° : %d",MasterBook[i].id);
        
        if(MasterBook[i].tr1.empty != -1){
            printf("\n\tTransazione n° 1 : ");
        printf("\n\t\tReceiver: %d",MasterBook[i].tr1.receiver);
        printf("\n\t\tSender: %d",MasterBook[i].tr1.sender);
        printf("\n\t\tReward: %d",MasterBook[i].tr1.reward);
        printf("\n\t\tTimestamp: %s",MasterBook[i].tr1.timestamp);
        }else printf("\nThere's no transaction 1 set!");

        if(MasterBook[i].tr2.empty!= -1){
            printf("\n\tTransazione n° 2 : ");
        printf("\n\t\tReceiver: %d",MasterBook[i].tr2.receiver);
        printf("\n\t\tSender: %d",MasterBook[i].tr2.sender);
        printf("\n\t\tReward: %d",MasterBook[i].tr2.reward);
        printf("\n\t\tTimestamp: %s",MasterBook[i].tr2.timestamp);
        }else printf("\nThere's no transaction 2 set!");

        if(MasterBook[i].reward.reward != -1){
            printf("\n\tTransazione Reward : ");
        printf("\n\t\tReceiver: %d",MasterBook[i].reward.receiver);
        printf("\n\t\tSender: %d",MasterBook[i].reward.sender);
        printf("\n\t\tReward: %d",MasterBook[i].reward.reward);
        printf("\n\t\tTimestamp: %s",MasterBook[i].reward.timestamp);
        }else printf("\nThere's no transaction reward set!");
    
        printf("finish print");
    }
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM