mlockall function

59 篇文章 0 订阅

From:http://alpha-supernova.dev.filibeto.org/lib/rel/4.0D/APS33DTE/DOCU_005.HTM


Themlockallfunction locks all of the pages mapped by aprocess's address space.On a successful call tomlockall, the specified process becomes locked andmemory-resident.Themlockallfunction takes two flags,MCL_CURRENT and MCL_FUTURE, which determine whether the pages to be lockedare those currently mapped, or if pages mapped in the future are to belocked.You must specify at least one flag for themlockallfunction to lock pages.If you specify bothflags, the address space to be locked is constructed from the logical OR ofthe two flags.

If you specify MCL_CURRENT only, all currently mapped pages of the process'saddress space are memory-resident and locked.Subsequent growth in any areaof the specified region is not locked into memory.If you specify theMCL_FUTURE flag only, all future pages are locked in memory.If you specifyboth MCL_CURRENT and MCL_FUTURE, then the current pages are locked andsubsequent growth is automatically locked into memory. 

简言之,MCL_CURRENT 就是会Lock住当前驻留内存的页,MCL_FUTURE会Lock住将来会驻留内存的页,比如后面有malloc的处理时,malloc的空间也不会交换到swap分区。


示例:

Example 4-2:  Using the mlockall Function

#include <unistd.h>     /* Support all standards    */
#include <stdlib.h>     /* malloc support           */
#include <sys/mman.h>   /* Memory locking functions */

#define BUFFER 2048

main()
{
  void *p[3];  /* Array of 3 pointers to void */

  p[0] = malloc(BUFFER);

       /* Currently no memory is locked */

  if ( mlockall(MCL_CURRENT) == -1 )
    perror("mlockall:1");

       /* All currently allocated memory is locked */

  p[1] = malloc(BUFFER);

       /* All memory but data pointed to by p[1] is locked */

  if ( munlockall() == -1 )
    perror("munlockall:1");

       /* No memory is now locked */

  if ( mlockall(MCL_FUTURE) == -1 )
    perror("mlockall:2");

       /* Only memory allocated in the future */
       /*   will be locked */

  p[2] = malloc(BUFFER);

       /* Only data pointed to by data[2] is locked */

  if ( mlockall(MCL_CURRENT|MCL_FUTURE) == -1 )
    perror("mlockall:3");

       /* All memory currently allocated and all memory that  */
       /* gets allocated in the future will be locked         */
}


[Contents]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值