Hi folks,
Can some help in understanding brk and sbrk. how does these are
implemeted(means how the memeory will be allocated). How malloc will
call these brk and sbrk. Please also give any example C program, where
brk and sbrk are used to allocate memory.
Appreciate your help in this regard,
Thanks,
Vikas.
brk, sbrk - you need unistd.h
They change the amount of space allocated for the
calling process's data segment. They change data segment
size .
int brk(void *end_data_segment);
void *sbrk(ptrdiff_t increment);
brk sets the end of the data segment to the value specified by
end_data_segment, when that value is reasonable, the system does have
enough memory and the process does not exceed its max data size (see
setrlimit(2)).
sbrk increments the program's data space by increment bytes. sbrk
isn't a system call, it is just a C library wrapper. Calling sbrk with
an increment of 0 can be used to find the current location of the
program break.
On success, brk returns zero, and sbrk returns a pointer to the start
of the new area. On error, -1 is returned, and errno is set to
ENOMEM.
The amount of allocated space increases as the break value
increases. Newly allocated
space is set to zero. If, how-ever, the same memory space is
reallocated to the same pro-
cess its contents are undefined.
When a program begins execution using execve() the break is set at
the highest location
defined by the program and data storage areas.
getrlimit and setrlimit get and set resource limits respectively. Each
resource has an associated soft and hard limit, as defined by the
rlimit structure (the rlim argument to both getrlimit() and
setrlimit()):
struct rlimit {
rlim_t rlim_cur; /* Soft limit */
rlim_t rlim_max; /* Hard limit (ceiling
for rlim_cur) */
};
The soft limit is the value that the kernel enforces for the
corresponding resource. The hard limit acts as a ceiling for the soft
limit: an unprivileged process may only set its soft limit to a value
in the range from 0 up to the hard limit, and (irreversibly) lower its
hard limit. A privileged process may make arbitrary changes to either
limit value.
Have a look at these links ->
1)
http://linux.about.com/library/cmd/blcmdl2_brk.htm
2)
http://www.minix3.org/manpages/man2/brk.2.html
Karthik Balaguru