mmap SIGBUS error and initializing the file

前端 未结 1 1633
遥遥无期
遥遥无期 2020-12-21 01:05

I\'m trying to model a basic CPU by mmapping a 1 MiB file, corresponding to the RAM size. I want to read/write this file. Currently I\'m getting a SIGBUS error with ra

相关标签:
1条回答
  • 2020-12-21 01:26

    The SIGBUS means that you're writing outside the file. From Linux man pages mmap(2):

    SIGBUS

    Attempted access to a portion of the buffer that does not correspond to the file (for example, beyond the end of the file, including the case where another process has truncated the file).

    As you create a new file, it is initially empty, i.e. has size of 0 bytes. You need to resize it using ftruncate to be at least big enough to contain the address written to (possibly rounded up to the page size). As you wanted to have a ram disk of size ram_bytes, then:

    ftruncate(ramD, ram_bytes);
    

    See this answer for a longer explanation about the same mechanism, using POSIX shared memory objects.


    PS. open returns an int; you should use an int, not int16_t, to store the file descriptor.

    0 讨论(0)
提交回复
热议问题