3

このプロジェクトを前学期に初めて作成したとき、コードは問題なく動作しました。プロセス間で共有する mmaped メモリが書き込まれているときにバス エラーが発生し、なぜそれが機能しなくなったのかわかりません。

Account_Info *mapData()
{
    int fd;
    //open/create file with read and write permission and check return value
    if ((fd = open("accounts", O_RDWR|O_CREAT, 0644)) == -1)
    {
            perror("Unable to open account list file.");
            exit(0);
    }

    //map data to be shared with different processes
    Account_Info *accounts = mmap((void*)0, (size_t) 100*(sizeof(Account_Info)), PROT_WRITE,
    MAP_SHARED, fd, 0);

    int count= 0;

    //loop to initialize values of Account_Info struct
    while (count != 20)
    {
            //bus error occurs here
            accounts[count].CurrBalance= 0;
            accounts[count].flag = 0;
            int i = 0;
            while (i != 100)
            {
                    //place NULL terminator into each element of AccName
                    accounts[count].AccName[i]= '\0';
                    i++;
            }

            count++;
    }

    close(fd);
    return accounts;
}
4

2 に答える 2