简体   繁体   中英

Can't shared an array of structs in shared memory

i created a library that initialize and fill an array of strutc shared 2 process. The problem is that it doesn't work, instead i created an array of int and it work correctly. Bu i need this array in shared memory, what dis i miss?

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't result anything?And this error force the process to close for certain reason?

EDIT: so i forgot to post the print function. I forced the size to be 0 cause i want to inset only 1 element and at least print only one. But the print function stuck after the first print before the if and i don't know why. I think that is cause the shared doen't work but for print "Blocco n°0" it suggest that Masterbook[i].id have some value inside.

EDIT: changed the char* but doesn't work too.

struct:

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

node.c

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

I solved the problem of output changing the pointer in the struct and rearrange the code.

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

print that had problem:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM