gpt4 book ai didi

c - 来自 munmap 的 Linux 错误

转载 作者:太空狗 更新时间:2023-10-29 12:16:34 24 4
gpt4 key购买 nike

我有一个关于 Linux 中的 mmap 和 munmap 的简单问题:是否有可能 mmap 成功但 munmap 失败?

假设所有参数都正确给出,例如,请看下面的代码片段。什么情况下会打印munmap failed!??

char *addr = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

... exit if mmap was not successful ...
... do some stuff using mmaped area ...

if( munmap(addr, 4096) == -1 ){
printf("munmap failed!\n");
}

最佳答案

是的,它可能会失败。来自 mmunmap man pages :

Upon successful completion, munmap() shall return 0; otherwise, it shall return -1 and set errno to indicate the error.

错误代码表明人们确实传递了无效参数。但是,如果您传递从 mmap() 获得的指针,并正确调整大小,那么它就不会失败。

Assuming all the parameters are correctly given,

那么就不会失败了。这就是为什么大多数实现 (99%) 只是不检查 unmmap() 的返回值。在这种情况下,即使它失败了,你也不能做任何事情(除了通知用户)。

关于c - 来自 munmap 的 Linux 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22779556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com