简体   繁体   English

brk和sbrk之间的区别

[英]Difference between brk and sbrk

I new to this can any one tell the exact difference between brk and sbrk with a brief example ? 我是新来的,能用一个简单的例子告诉brksbrk之间的确切区别吗? is there any efficiency factor to choose from any of the two? 是否有任何效率因素可供选择? malloc and new internally call brk or sbrk . mallocnew内部调用brksbrk

int brk(void *addr);

brk() sets the end of the data segment to the value specified by addr, when that value is reasonable, the system has enough memory, and the process does not exceed its maximum data size. brk()将数据段的结尾设置为addr指定的值,当该值合理时,系统有足够的内存,并且进程不超过其最大数据大小。

On success, brk() returns zero. 成功时,brk()返回零。 On error, -1 is returned, and errno is set to ENOMEM. 出错时,返回-1,并将errno设置为ENOMEM。

void *sbrk(intptr_t increment);

sbrk() increments the program's data space by increment bytes. sbrk()按递增字节递增程序的数据空间。 Calling sbrk() with an increment of 0 can be used to find the current location of the program break. 以增量0调用sbrk()可用于查找程序中断的当前位置。

On success, sbrk() returns the previous program break. 成功时,sbrk()返回上一个程序中断。 (If the break was increased, then this value is a pointer to the start of the newly allocated memory). (如果中断增加,则此值是指向新分配的内存的开始的指针)。 On error, (void *) -1 is returned, and errno is set to ENOMEM. 出错时,返回(void *) - 1,并将errno设置为ENOMEM。

From linux manual page 来自linux手册页

brk sets the upper limit of the data segment, sbrk increments it. brk设置数据段的上限, sbrk递增它。 In ancient Unixes malloc/free used sbrk . 在古代Unix中malloc/free使用的sbrk On modern ones things could be very different, for example, OSX does not use brk/sbrk to manage heap allocations but mmap , brk/sbrk exist but are just emulation in a small segment of memory. 在现代的情况下,事情可能会有很大的不同,例如,OSX不使用brk/sbrk来管理堆分配,但是mmapbrk/sbrk存在但只是在一小段内存中进行仿真。 This is almost the same on Linux (source code of mention the history of transition from brk/sbrk to mmap). 这在Linux上几乎相同(提及从brk / sbrk到mmap的过渡历史的源代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM