Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux error from munmap

Tags:

c

linux

mmap

I have a simple question regarding mmap and munmap in Linux : is it possible that mmap succeeds but munmap fails?

Assuming all the parameters are correctly given, for example, see the following code snippet. In what circumstances munmap failed! will be printed??

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");
}
like image 306
daehee Avatar asked May 20 '26 04:05

daehee


1 Answers

Yes, it can fail. From mmunmap man pages :

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

The error codes indicates that people do pass invalid parameters. But if you pass pointer you got from mmap(), and correct size, then it will not fail.

Assuming all the parameters are correctly given,

Then it will not fail. That is why most implementations (99%) just don't check the return value of unmmap(). In such case, even if it fails, you can't do anything (other then informing the user).

like image 142
BЈовић Avatar answered May 21 '26 18:05

BЈовић



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!