I have written a code on Mac OS X to use POSIX shared memory as shown below:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
int main() {
int fileHandle = shm_open("TW_ShMem1",O_CREAT|O_RDWR, 0666);
if(fileHandle==-1) {
//error.
} else {
//Here, it is failing on Mac OS X
if(-1==ftruncate(fileHandle, 8192)) {
shm_unlink("TW_ShMem1");
fileHandle = -1;
} else {
return 0;
}
}
return 1;
}
ftruncate on Linux is working without any problem. On Mac OS X, it is returning -1 and errno is EINVAL (as seen in the debugger).
Why is it failing? What is being missed here?
EINVALonly if it's a socket rather than a file, it's not open for writing, or the length argument is less than zero. So, what's the value ofp_size? And does the same thing happen if you open & ftruncate a regular file, and thenmmapit?mmapit.mmapis done in the next function.