简体   繁体   English

在Unix中使用sbrk系统调用

[英]Use of sbrk system call in Unix

What is the use of sbrk system call in Unix and how is it to be used in C? Unix中sbrk系统调用的用途是什么,如何在C中使用它? Most of them say that malloc uses the sbrk system call to allocate memory. 他们中的大多数人都说malloc使用sbrk系统调用来分配内存。 So, how does the sbrk system call allocate memory? 那么, sbrk系统调用如何分配内存? What are the arguments and return type of sbrk? sbrk的参数和返回类型是什么?

sbrk() takes a positive integer as an argument. sbrk()接受一个正整数作为参数。 It increases the size of the current process's heap by that number of bytes, then returns a void * to the new end of the heap. 它将当前进程堆的大小增加该字节数,然后将void *返回到堆的新末端。

However, the information you're reading is outdated . 但是, 您正在阅读的信息已过时 To quote the Mac OS X manual page for this system call: 要引用此系统调用的Mac OS X手册页:

The brk and sbrk functions are historical curiosities left over from earlier days before the advent of virtual memory management. brksbrk函数是虚拟内存管理出现之前的早期历史遗留问题。

Modern systems do not use sbrk() for memory management, as it is incredibly limited. 现代系统不使用sbrk()进行内存管理,因为它受到难以置信的限制。 In particular, it cannot manage a heap which is not contiguous in memory! 特别是,它无法管理内存中不连续的堆! Instead, they use the mmap() system call to map new ranges of memory as needed. 相反,他们使用mmap()系统调用来根据需要映射新的内存范围。

Function definition: void *sbrk(intptr_t increment); 函数定义: void *sbrk(intptr_t increment);

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

From http://linux.die.net/man/2/sbrk http://linux.die.net/man/2/sbrk

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

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