Unix中的malloc()和sbrk()如何工作? [英] How malloc() and sbrk() works in unix?

查看:166
本文介绍了Unix中的malloc()和sbrk()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是UNIX的新手,我正在研究UNIX系统的某些调用,例如brk()sbrk()等....

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

前一天,我已经阅读了有关malloc()函数的信息,这让我有些困惑! 谁能告诉我为什么malloc减少了程序必须执行的sbrk()系统调用的数量?

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?

还有另一个问题,brk(0)sbrk(0)malloc(0)返回相同的值吗?

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

推荐答案

为什么malloc减少了程序的sbrk()系统调用的数量 必须执行?

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

例如,如果您调用malloc()请求10个字节的内存,则实现可能使用sbrk(或其他系统调用,例如mmap)从OS请求4K字节.然后,当您下次调用malloc()来请求另外10个字节时,它不必发出系统调用;它可能只返回最后一次4K的系统调用分配的一些内存.

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.

这篇关于Unix中的malloc()和sbrk()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆