Page 1 of 1
sbrk instruction
Posted: Fri Dec 05, 2003 11:34 am
by Fukuda
Im implementing malloc function for my os, but I dont understand this sbrk stuff. I search net, but get lost in huge, most of nonsense, results. Ill be glad if someone explains and gives a link about it. thanks.
Re:sbrk instruction
Posted: Fri Dec 05, 2003 11:46 am
by Tim
sbrk is how malloc gets its memory from the OS when it has run out.
sbrk originates from Old Unix, where a program's address space looked like this:
(low memory) | Code | Data +BSS | (high memory)
sbrk(n) adjusts the position of the end of the BSS by n bytes. Clearly this method only works for the layout above; it doesn't consider fragmentation at all.
A better, and more flexible, way is to have the kernel provide a function whereby malloc can say "give me a chunk of n bytes anywhere".
Re:sbrk instruction
Posted: Fri Dec 05, 2003 2:12 pm
by df
sbrk, archaic old **** that should not be in an os

Re:sbrk instruction
Posted: Fri Dec 05, 2003 2:31 pm
by BI lazy
rather use morecore. it is more friendly to your processes... *ggg*
Re:sbrk instruction
Posted: Sat Dec 06, 2003 3:31 am
by Fukuda
BI lazy wrote:
rather use morecore. it is more friendly to your processes... *ggg*
As I understood sbrk is a bad stuff to use in an os. I should use 'morecore' instead of 'sbrk'. Here is a morecore function that I found on net to study. Sorry, I dont know the owner so can not give credits.
Code: Select all
void *
_default_morecore(long size)
{
void *result;
result = sbrk(size);
if (result == (void *) -1)
return NULL;
return result;
}
Nearly, all the morecore functions that I found is same. Calls sbrk function. And some of you say, dont use sbrk in your os. Should I use morecore and call sbrk in it???
Indeed, Im implementing my malloc function. While doing so, I imagine that I have a big memory block to give piece by piece by malloc function. And now, I dont know how to get these big chunks. (I plan to use sbrk) I enabled paging and at map all virtual addrs on to their physical location. And I put a symbol at the end of my kernel end and know the memory after kernel is free(I dont use, so it should be, shouldt it?). Now how to get big chunks from that space to use with malloc? Every tutorial I found not interested in this. So I stuck here. I study lots of sbrk function, (not morecore because all morecore functions I found calls sbrk and I dont understand why I should use morecore instead of sbrk). All sbrk functions breaks free memory into pieces after a blank(dont know why) from end of kernel symbol.
If anyone have a good tutorial, Ill be glad. And explanations welcome. Thanks.
Re:sbrk instruction
Posted: Sat Dec 06, 2003 4:45 am
by Tim
Come up with a page-based virtual memory memory allocator in your kernel, which can allocate a group of pages, return the starting virtual address to the application (to morecore/malloc), and allocate and map physical pages when they are needed.
www.osdever.net, Memory Management 2 by somebody who might be familiar

.
Re:sbrk instruction
Posted: Sat Dec 06, 2003 6:00 am
by Fukuda
great Tim.
