简体   繁体   中英

When does malloc() set EAGAIN error?

I read the manual on malloc() in Solaris, and find that malloc() can set EAGAIN error in Solaris.

The manual writes:

EAGAIN There is not enough memory available to allocate size bytes of memory; but the application could try again later.

Personally, I think if the malloc() returns NULL , there must be a memory leak or some other persistent problem. If that happens how would trying again later help?

So I want to know, in what conditions can malloc() set EAGAIN errno? Has anyone encountered such situation?

Standard malloc() does not set errno to EAGAIN on failure.

Under Unix, malloc() will most probably set errno to ENOMEM .

In general errno EAGAIN means Resource temporarily unavailable . Which means that the operating system may have the resource available in some time.

This is just a way of saying right now I haven't enough memory, but I will try to free some in the nearest future and then I can give it to you .

This may be related to the way operating systems usually allocate memory to processes - even if the memory is free()'d it does not return to the operating system, but is still reserved for that process.

I am only speculating , but perhaps in case of EAGAIN the system will try to reallocate the unused memory assigned to the other processes. This may take time, hence the EAGAIN return code.

I would suggest using sleep() after receiving EAGAIN and then trying it again. After the second call either memory will be allocated or another error returned. If it's ENOMEM , then the case is clear, there's no memory. If it's EAGAIN again... It's up to you.

Standard malloc function doesn't set errno on failure. So it's only specific to the implementation of malloc on Solaris that has this additional feature. Note that malloc still returns NULL on failure.

So you can still check for return value of malloc() and not bother checking about errno which is the standard malloc's behaviour and should be enough on all occasions. Just that errno provides additional information about failure which may be helpful on certain occasions.

Generally speacking, checking errno makes sense only along with the return code. Relying errno may or may not indicate any failures.

opengroup.org (POSIX) says:

Upon successful completion with size not equal to 0, malloc() shall return a pointer to the allocated space. If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() shall be returned. Otherwise, it shall return a null pointer [CX] and set errno to indicate the error.

ERRORS

The malloc() function shall fail if:

[ENOMEM]

[CX] Insufficient storage space is available.

POSIX malloc description

Solaris comes from a different UNIX family and you will encounter lots of differences between POSIX and base Solaris - ie, The most glaring thing for new users is normally awk. Solaris has an ancient awk : /usr/bin/awk, /usr/xpg/bin/awk is more "modern", /usr/bin/nawk is what you use when porting shell scripts to Solaris. These anachronisms come from way back and are there so old utilities and syscalls will remain functional on new versions of Solaris.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM