c/linux - ftruncate and POSIX Shared Memory Segments

拟墨画扇 提交于 2019-12-12 19:12:35

问题


The end goal here is that I'd like to be able to extend the size of a shared memory segment and notify processes to remap the segment after the extension. However it seems that calling ftruncate a second time on a shared memory fd fails with EINVAL. The only other question I could find about this has no answer: ftruncate failed at the second time

The manpages for ftruncate and shm_open make no mention of disallowing the expansion of shared memory segments after creation, in fact they seem to indicate that they can be resized via ftruncate but so far my testing has shown otherwise. The only solution I can think of would be to destroy the shared memory segment and recreate it at a larger size, however this would require all processes that have mmap'd the segment to unmap it before the object will be destroyed and available for recreation.

Any thoughts? Thanks!

EDIT: As requested as simple example

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>

int main(int argc, char *argv[]){
    const char * name = "testfile";
    size_t sz = 4096; // page size on my sys
    int fd;
    if((fd = shm_open(name, O_CREAT | O_RDWR, 0666)) == -1){
        perror("shm_open");
        exit(1);
    }
    ftruncate(fd, sz);
    perror("First truncate");
    ftruncate(fd, 2*sz);
    perror("second truncate");

    shm_unlink(name);
    return 0;
}

Output:

First truncate: Undefined error: 0
second truncate: Invalid argument

EDIT - Answer: Appears that this is an issue with OSX implementation of the POSIX standard, the above snippet works on a 3.13.0-53-generic GNU/Linux kernel and likely others I'd guess.


回答1:


With respect to your end goal, here's an open source library I wrote that seems to be a match: rszshm - resizable pointer-safe shared memory.



来源:https://stackoverflow.com/questions/35373511/c-linux-ftruncate-and-posix-shared-memory-segments

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