I am trying to surpass a shared memory object after shm_open and ftruncate successfully on fisrt. Here is the code
char *uuid = GenerateUUID(); int fd = shm_open(uuid, O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); if(fd == -1) perror("shm_open"); size_t shmSize = sizeof(container); int ret = ftruncate(fd, shmSize); perror("ftruncate first"); ret = ftruncate(fd, shmSize * 2); perror("ftruncate second");
It can pass the first ftruncate, but for the second ftruncate it exceeds the unsuccessful with errno = 22, "Invalid argument".
I also tried to take a picture of the memory object after mmap, refer to the ftruncate man page, the shared memory should be formatted as zero to the new length.
In addition, I also tried to photograph the memory object in the child process (this is an IPC topic among two processes), ftruncate returns "Invalid fd, no such file or directory", but I could shm_open and mmap successfully in the child process.
Any ideas? Thanks!
source share