Sbrk
   HOME

TheInfoList



OR:

and are basic
memory management Memory management is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when ...
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
s used in
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, and ot ...
and
Unix-like A Unix-like (sometimes referred to as UN*X or *nix) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Unix-li ...
operating systems to control the amount of memory allocated to the
data segment In computing, a data segment (often denoted .data) is a portion of an object file or the corresponding address space of a program that contains initialized static variables, that is, global variables and static local variables. The size of this seg ...
of the
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management *Business process, activities that produce a specific se ...
. These functions are typically called from a higher-level memory management library function such as . In the original Unix system, and were the only ways in which applications could acquire additional data space; later versions allowed this to also be done using the call.


Description

The brk and sbrk calls dynamically change the amount of space allocated for the data segment of the calling process. The change is made by resetting the program break of the process, which determines the maximum space that can be allocated. The program break is the address of the first location beyond the current end of the data region. The amount of available space increases as the break value increases. The available space is initialized to a value of zero, unless the break is lowered and then increased, as it may reuse the same pages in some unspecified way. The break value can be automatically rounded up to a size appropriate for the
memory management Memory management is a form of resource management applied to computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and free it for reuse when ...
architecture. and were considered legacy even by 1997 standards ( Single UNIX Specification v2 or POSIX.1-1998). They were removed in POSIX.1-2001.


Function signatures and behavior

#include int brk(void* end_data_segment); void *sbrk(intptr_t increment); is used to adjust the program break value by adding a possibly negative size, while is used to set the break value to the value of a pointer. Set parameter to zero to fetch the current value of the program break. Upon successful completion, the subroutine returns a value of 0, and the subroutine returns the prior value of the program break (if the available space is increased then this prior value also points to the start of the new area). If either subroutine is unsuccessful, a value of is returned and the
global variable In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global s ...
is set to indicate the error. Not every Unix-like system entertains the concept of having the user control the data segment. The
Mac OS X macOS (; previously OS X and originally Mac OS X) is a Unix operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for Apple's Mac (computer), Mac computers. Within the market of ...
implementation of is an emulation and has a maximum allocation of 4 megabytes. On first call an area of exactly this large is allocated to hold the simulated segment. When this limit is reached, −1 is returned and the is set to . always errors.


Error codes

The error is set and the allocated space remains unchanged if one or more of the following are true: * The requested change allocates more space than is allowed by a system-imposed maximum. * The requested change sets the break value to a value
greater than or equal to In mathematics, an inequality is a relation which makes a non-equal comparison between two numbers or other mathematical expressions. It is used most often to compare two numbers on the number line by their size. There are several different ...
the start address of any attached
shared memory In computer science, shared memory is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Shared memory is an efficient means of passing data between progr ...
segment.


See also

*
Exec (computing) In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay. It is especially important i ...
* Memory address#Address space in application programming


References

{{Reflist Memory management Operating system APIs