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 ram[2] = 1
which I gather is from trying to mmap outside the file range. I've read that perhaps I need to fill the file with zeroes as placeholders, but I'm a bit confused as to why I have to do this, since I thought mmap would automatically set aside a memory chunk for me that would be allocated when I first touch it (as I am trying to do below with my test). What am I missing?
int16_t ramD;
if ( (ramD = open("ramMap.txt", O_RDWR | O_CREAT, 0666)) == -1)
{
errx(EX_OSERR, "RAM could not be initialized");
}
uint8_t* ram = mmap(0, ram_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, ramD, 0);
ram[2] = 1;
printf("%i", ram[2]);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…