11

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?

7
  • The OS X man page for ftruncate claims it can fail with EINVAL only 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 of p_size? And does the same thing happen if you open & ftruncate a regular file, and then mmap it?
    – Useless
    Aug 26, 2014 at 9:26
  • Can you provide a self-contained compilable example?
    – Useless
    Aug 26, 2014 at 10:01
  • memoryName is "TW9_Shm1" and size is 8192. Aug 26, 2014 at 10:21
  • It works on Linux but not on Mac OS X. It failed at ftruncate. Aug 26, 2014 at 10:21
  • @Useless: Yes. It is same thing. Open and ftruncate shared memory file and mmap it. mmap is done in the next function. Aug 26, 2014 at 10:26

1 Answer 1

12

This looks like OSX behaviour - ftruncate only works once on the initial creation of the segment. Any subsequent calls fail in this manner. The earliest reference I can find to this is a post to the apple mailing list.

If I put an shm_unlink before the shm_open the ftruncate works consistently.

assuming that you only want to resize the shared memory segment the once, you could wrap the ftruncate in an fstat to determine the current size and resize it in the case that st_size == 0

e.g.

struct stat mapstat;
if (-1 != fstat(fileHandle, &mapstat) && mapstat.st_size == 0) {
    ftruncate(fileHandle, 8192);
}
3
  • Note that this may introduce a race if this code runs in different threads or processes.
    – heiner
    Jun 16, 2021 at 13:36
  • The OSX kernel code for shared memory regions says: It can only be done once per shared memory region. The only race would be if you chose different sizes. You're still left with the same limitation - one truncate per lifetime. Jun 16, 2021 at 13:50
  • Sorry, I meant the fstat check could raise in different processes. Of course, if one just ignores the ftruncate return value it still works, but the fstat check isn't strictly speaking necessary then.
    – heiner
    Jun 16, 2021 at 14:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.