问题描述
我试图在 shm_open 和 ftruncate 成功后超过共享内存对象.这是代码,
I'm trying to exceed the shared memory object after shm_open and ftruncate successfully at 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");
它可以通过第一个 ftruncate,但是对于第二个 ftruncate,它超过了 failed 并且 errno=22, "Invalid argument".
It could pass the first ftruncate, but for the second ftruncate, it exceeds failed with errno=22, "Invalid argument".
我也尝试过在mmap之后对内存对象进行ftruncate,参考ftruncate的手册页,共享内存应该被格式化为零到新的长度.
I also tried to ftruncate the memory object after mmap, refer to the ftruncate's man page, the shared memory should be formatted as zero to the new length.
此外,我还尝试对子进程中的内存对象进行ftruncate(这是两个进程之间的IPC主题),ftruncate返回无效的fd,没有此类文件或目录"但我可以在子进程中成功shm_open和mmap过程.
Besides, I also tried to ftruncate the memory object in the child process (This is an IPC topic among two processes), the ftruncate returns "Invalid fd, no such file or directory" but I could shm_open and mmap successfully in child process.
有什么想法吗?谢谢!
推荐答案
我认为这是 shm_open()
、ftruncate()
、 mmap().
你必须在第一次 ftruncate()
给共享内存一个长度,但随后的 ftruncate()
给出错误号 22,你可以直接无视.
You have to ftruncate()
the first time through to give the shared memory a length, but subsequent times ftruncate()
gives that error number 22, which you can simply ignore.
这篇关于ftruncate 第二次失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!