DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

brk(S)


brk, sbrk -- change data segment space allocation

Synopsis

   #include <unistd.h>
   

int brk(void *endds);

void *sbrk(int incr);

Description

brk and sbrk are used to change dynamically the amount of space allocated for the calling process's data segment [see exec(S)]. The change is made by resetting the process's break value and allocating the appropriate amount of space. The break value is the address of the first location beyond the end of the data segment. The amount of allocated space increases as the break value increases. Newly allocated space is set to zero. If, however, the same memory space is reallocated to the same process its contents are undefined.

brk sets the break value to endds and changes the allocated space accordingly.

sbrk adds incr bytes to the break value and changes the allocated space accordingly. incr can be negative, in which case the amount of allocated space is decreased.

Return values

On success, brk returns 0 and sbrk returns the old break value. On failure, brk and sbrk return -1 and set errno to identify the error without making any change in the allocated space.

Errors

In the following conditions, brk and sbrk fail and set errno to:

ENOMEM
Such a change would result in more space being allocated than is allowed by the system-imposed maximum process size [see ulimit(S)].

EAGAIN
Total amount of system memory or swap space available is temporarily insufficient [see shmop(S)]. This may occur even though the space requested was less than the system-imposed maximum process size [see ulimit(S)].

References

end(S), exec(S), malloc(S), shmop(S), ulimit(S)

Notices

Usage

Applications generally cope with the details of dynamic memory allocation/deallocation by using the malloc(S) family of library functions. Moreover, use of brk or sbrk can interfere with the dynamic memory used by malloc(S).

Considerations for threads programming

Threads within a process share (by definition) the same address space; modifications to the address space by one thread can be perceived by sibling threads.

The sbrk function is actually a front end to the brk system call and has a potential race condition that might produce incorrect results if used concurrently by sibling threads. Such usage is not advised. The malloc(S) family of functions has been made safe for use by threads.


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 - 01 June 2005