问题描述
我已经编写了以下代码,但是该代码仍然是EEERROR
消息,告诉我mremap
无法扩展内存.
I have write the following code , but the code is still fiven me EEERROR
message , which tells that the mremap
failed to extend the memory.
int main()
{
int size_of_mem = 1024
int fd = shm_open("/myregion", O_CREAT | O_RDWR, S_IRWXO | S_IRUSR | S_IWUSR);
if (fd == -1)
printf("ERROR in shm_open \n") ;
if (ftruncate(fd, size_of_mem) == -1)
printf("ERROR in ftruncate \n") ;
int shm_address = mmap(0 , size_of_mem , PROT_READ | PROT_WRITE | PROT_EXEC ,MAP_SHARED , fd , 0) ;
if (shm_address == MAP_FAILED)
{
printf("Error mmapping the file \n");
exit(EXIT_FAILURE);
}
int temp = mremap(shm_address , size_of_mem ,4000 , MREMAP_MAYMOVE) ;
if( temp < 0)
{
printf("EEEEEEEERROR\n") ;
}
return 0 ;
}
推荐答案
这里有些错误.
第一,mmap()
和mremap()
返回void*
指针,您不能仅将其强制转换为int
.
First, mmap()
and mremap()
return a void*
pointer, which you must not just cast to an int
.
第二,mremap()
man页面状态:
成功时,mremap()返回一个指向新虚拟内存区域的指针.错误时,将返回值MAP_FAILED(即(void *)-1),并且已正确设置errno.
On success mremap() returns a pointer to the new virtual memory area. On error, the value MAP_FAILED (that is, (void *) -1) is returned, and errno is set appropriately.
所以您的支票temp < 0
是错误的.它应该是temp == (void*)-1
. mremap()
完全有可能在成功时返回一个有效指针,该指针在转换为int时小于零.
So your check temp < 0
is wrong. It should be temp == (void*)-1
. It is entirely possible that mremap()
returns a valid pointer on success that is smaller than zero when cast to an int.
第三,mmap()
和mremap()
均设置errno
(手册页)变量,当发生错误时.您可以阅读该内容,以获取有关到底出了什么问题的更多信息.要仅输出文本错误消息,请使用perror()
函数(手册页).请注意,您必须先#include <errno.h>
.
Third, both mmap()
and mremap()
set the errno
(man page) variable when an error occurs. You can read that to get more information about what exactly went wrong. To just output a text error message use the perror()
function (man page). Note that you have to #include <errno.h>
for that.
第四,如果您检测到错误情况,则始终打印一条消息,但大多数情况下允许执行继续.那没有道理.如果shm_open()
失败,则要立即返回(或调用exit(EXIT_FAILURE)
).如果您什至无法打开SHM文件,则以下功能均无法使用.
Fourth, if you detect an error condition, you always print a message, but you mostly allow execution to continue. That doesn't make sense. If shm_open()
failed, you want to return immediately (or call exit(EXIT_FAILURE)
). None of the following functions will work if you couldn't even open the SHM file.
因此,我的清理版本如下:
Thus, my cleaned up version looks like this:
#include <error.h>
int main()
{
int size_of_mem = 1024;
int fd = shm_open("/myregion", O_CREAT | O_RDWR,
S_IRWXO | S_IRUSR | S_IWUSR);
if (fd == -1)
{
perror("Error in shm_open");
return EXIT_FAILURE;
}
if (ftruncate(fd, size_of_mem) == -1)
{
perror("Error in ftruncate");
return EXIT_FAILURE;
}
void *shm_address = mmap(0, size_of_mem,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_SHARED, fd, 0);
if (shm_address == MAP_FAILED)
{
perror("Error mmapping the file");
return EXIT_FAILURE;
}
void *temp = mremap(shm_address, size_of_mem, 4000, MREMAP_MAYMOVE);
if(temp == (void*)-1)
{
perror("Error on mremap()");
return EXIT_FAILURE;
}
return 0;
}
注意:
- 更正数据类型(
void*
),更正mremap()
的错误检查,使用perror()
打印更多有用的错误消息,错误路径终止函数的执行. - 正确/一致的缩进.
- 函数调用中
,
之前没有空格.
- Correct data types (
void*
), correct error checking formremap()
, usage ofperror()
to print more informative error messages, error paths ending execution of the function. - Correct/consistent indentation.
- No spaces before
,
in function calls.
这篇关于mremap函数无法分配新的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!