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()
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.
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.
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.
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.