Content deleted Content added
No edit summary |
|||
(41 intermediate revisions by 35 users not shown) | |||
Line 1:
{{Short description|Basic memory management system calls used in Unix}}
{{lowercase title}}
'''{{mono|brk}}''' and '''{{mono|sbrk}}''' are basic [[memory management]] [[system call]]s used in [[Unix]] and [[Unix-like]] operating systems to control the amount of memory allocated to the
▲{{cleanup-rewrite|date=February 2012}}
▲'''brk''' and '''sbrk''' are basic [[memory management]] [[system call]]s used in [[Unix]] and [[Unix-like]] operating systems to control the amount of memory allocated to the [[data segment]] of the [[Process (computing)|process]]. These calls are typically made from a higher-level memory management library such as [[malloc]]. In the original Unix system, brk and sbrk were the only ways in which applications could acquire additional data space; later versions allowed this to also be done using the [[mmap]] call.
==Description==
The brk and sbrk calls [[dynamic memory allocation|dynamically change]] the amount of space allocated for the
{{mono|sbrk}} and {{mono|brk}} were considered legacy even by 1997 standards ([[Single UNIX Specification]] v2 or POSIX.1-1998).<ref name=SUSv2>{{cite web |title=brk, sbrk - change space allocation (LEGACY) |url=https://pubs.opengroup.org/onlinepubs/7908799/xsh/brk.html |website=The Single UNIX ® Specification, Version 2 |accessdate=30 November 2019 |date=1997}}</ref> They were removed in POSIX.1-2001.<ref>{{man|2|brk|Linux}}</ref>
==Function signatures and behavior==
<syntaxhighlight lang="c">
#include <unistd.h>
int brk(void
void *sbrk(intptr_t increment);
</syntaxhighlight>
Upon successful completion, the {{mono|brk}} subroutine returns a value of 0, and the {{mono|sbrk}} subroutine returns the prior value of the program break (if the available space is increased
Not every Unix-like system entertains the concept of having the user control the data segment. The [[Mac OS X]] implementation of {{mono|sbrk}} is an emulation and has a maximum allocation of 4 megabytes. On first call an area exactly this large is allocated to hold the simulated segment. When this limit is reached, −1 is returned and the {{mono|errno}} is set to {{mono|ENOMEM}}. {{mono|brk}} always errors.<ref>{{Cite web|url=https://opensource.apple.com/source/Libc/Libc-1272.250.1/emulated/brk.c.auto.html|title=BRK.c}}</ref>
▲Upon successful completion, the brk subroutine returns a value of 0, and the sbrk subroutine returns the prior value of the program break (if the available space is increased the return value points to the start of the new area). If either subroutine is unsuccessful, a value of -1 is returned and the [[Errno.h|errno]] [[global variable]] is set to indicate the error.
==Error codes==
The error
* The requested change allocates more space than is allowed by a system-imposed maximum.
* The requested change sets the break value to a value [[inequality (mathematics)|greater than or equal to]] the start address of any attached [[Shared memory (interprocess communication)|shared memory]] segment.
== See also ==
* [[Exec (
* {{section link|Memory address|Address space in application programming}}
==References==
{{Reflist}}
[[Category:Memory management
[[Category:
|