Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How malloc() and sbrk() works in unix?

Tags:

unix

malloc

I am new to UNIX, and I am studying some of UNIX system calls such as brk(), sbrk(), and so on....

Last day I have read about malloc() function, and I was confused a little bit! Can anybody tell me why malloc reduces the number of sbrk() system calls that the program must perform?

And another question, do brk(0), sbrk(0) and malloc(0) return the same value?

like image 378
arze ramade Avatar asked Oct 30 '13 07:10

arze ramade


People also ask

How does malloc sbrk work?

When a program asks malloc for space, malloc asks sbrk to increment the heap size and returns a pointer to the start of the new region on the heap. This is missing a technicality, that malloc(0) should either return NULL or another pointer that can be passed to free without causing havoc, but it basically works.

What does sbrk do in Linux?

The sbrk() function is used to change the space allocated for the calling process. The change is made by adding incr bytes to the process's break value and allocating the appropriate amount of space. The amount of allocated space increases when incr is positive and decreases when incr is negative.

What is the difference between brk () and sbrk ()?

brk identifies the lowest data segment location not used by the caller as addr . This location is rounded up to the next multiple of the system page size. sbrk , the alternate interface, adds incr bytes to the caller data space and returns a pointer to the start of the new data area.

How is sbrk implemented?

On Linux, sbrk() is implemented as a library function that uses the brk() system call, and does some internal bookkeeping so that it can return the old break value.


4 Answers

Syscalls are expensive to process because of the additional overhead that a syscall places: you have to switch to kernel mode. A system call gets into the kernel by issuing a "trap" or interrupt. It's a call to the kernel for a service, and because it executes in the kernel address space, it has a high overhead switch to kernel (and then switching back).

This is why malloc reduces the number of calls to sbrk() and brk(). It does so by requesting more memory than you asked it to, so that it doesn't have to issue a syscall everytime you need more memory.

brk() and sbrk() are different.

brk is used to set the end of the data segment to the value you specify. It says "set the end of my data segment to this address". Of course, the address you specify must be reasonable, the operating system must have enough memory, and you can't make it point to somewhere that would otherwise exceed the process maximum data size. Thus, brk(0) is invalid, since you'd be trying to set the end of the data segment to address 0, which is nonsense.

On the other hand, sbrk increments the data segment size by the amount you specify, and returns a pointer to the previous break value. Calling sbrk with 0 is valid; it is a way to get a pointer to the current data segment break address.

malloc is not a system call, it's a C library function that manages memory using sbrk. According to the manpage, malloc(0) is valid, but not of much use:

If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

So, no, brk(0), sbrk(0) and malloc(0) are not equivalent: the first of them is invalid, the second is used to obtain the address of the program's break, and the latter is useless.

Keep in mind that you should never use both malloc and brk or sbrk throughout your program. malloc assumes it's got full control of brk and sbrk, if you interchange calls to malloc and brk, very weird things can happen.

like image 70
Filipe Gonçalves Avatar answered Oct 28 '22 13:10

Filipe Gonçalves


why malloc reduces the number of sbrk() system calls that the program must perform?

say, if you call malloc() to request 10 bytes memory, the implementation may use sbrk (or other system call like mmap) to request 4K bytes from OS. Then when you call malloc() next time to request another 10 bytes, it doesn't have to issue system call; it may just return some memory allocated by system call of the last time 4K.

like image 27
tristan Avatar answered Oct 28 '22 13:10

tristan


malloc() function is used to call the sbrk system call to create a memory dynamically during the process.

malloc() function is already assigned in stdlib.h header file so the as per the required function is recursively call by the malloc function using the library function.

with the help of sbrk we need to explicitly declare some thing to call the system call.

According to the size given in function or through system call it return to the variable and store.

like image 1
Chandru Avatar answered Oct 28 '22 13:10

Chandru


sbrk() function increases the programs data segment allocation by specified bytes.

malloc(4096); // sbrk += 4096 Bytes
free();       // freeing memory will not bring down the sbrk by 4096 Bytes  
malloc(4096); // malloc'ing again will not increase the sbrk and it will use 
                 the existing space which not result in sbrk() call.  
like image 1
Sunil Bojanapally Avatar answered Oct 28 '22 12:10

Sunil Bojanapally