繁体   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