ftruncate not working on POSIX shared memory in Mac OS X

…衆ロ難τιáo~ 提交于 2019-11-30 20:04:07

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);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!