Closed xyang619 closed 6 years ago
@xyang619 Thanks for using mio and reporting this. I compiled it with the same compilation commands and on my platform (Arch with most recent updates installed) the example as well as the tests run as expected, no errors. I'm not sure what caused the issue for you. Perhaps something with permissions?
@mandreyel Thanks for your quick response. I checked the permission and I did have read and write permission. I will check it further.
@xyang619 I just had the chance to run it on a similar Debian platform and it indeed does not run as expected. I'm looking into it now!
And apologies for the slow response, I hadn't seen your response.
@xyang619 Oh, right, I forgot that when mapping a file, the file must exist (i.e. open
is not called with the O_CREAT
flag), so the first error is expected. [edit] And I also forgot to specify in the example that "file.txt" must be non-zero long, so it won't work on an empty file created by touch
. I believe there is a check in there somewhere against zero file lengths. I'm sorry for not being clear on this, I'll update the example with comments explaining these.
Although maybe this is not the expected behavior and mio should create the file when mapping it in read-write mode. The motivation for not doing so was twofold.
One, it didn't make a lot of sense in read-only mode to create an "empty" file and I suppose this behavior became the default for read-write mode as well.
Moreover, I think it complicates the API. Suppose that the file does not exist but the user creates a mapping at offset f
and mapping length of n
. Do we now create a file that is f + n
long? I think this would make behavior more confusing, but I could be wrong. Any input is appreciated.
@mandreyel Thanks a lot. I also found the similar behavior via mmap. The file must exist and have non-zero size. Therefore, when using in writing mode, I suggest to check the existence of file and truncate to target size.
@xyang619 Having thought some more about it, I have come to find the idea of silently allocating the file a bad idea. Memory mapping, to my understanding, is about creating a memory view into a file, and to view something, that something must already exist. At least I find this more intuitive and less surprising than silent allocations. So I'm going to close this for now, but if more people wish to change this behavior, I'll reconsider my decision. Thanks for your input, it helped me to clarify the example and the docs!
I have tried the example.cpp and it compiled successfully. However when I try to run it, it does not work.
The error message is:
then I tried to touch a file named file.txt,another error occured.
Platform Linux Debian 9 .5 gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
Thanks