2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Linux] メモリのロック

Last updated at Posted at 2019-08-27

概要

仮想メモリに比べて物理メモリは非常に量が少ない、Linuxでは不要なページをスワップアウトして必要に応じてスワップインするデマンドページングを採用している。メモリーのロックとロック解除を行うシステムコール、mlock(2)に関する記事

mlock(2)

torvalds/linux/mm/mlock.c

/* mlock - メモリーのロックとロック解除を行う */
# include <sys/mman.h>

int mlock(const void *addr, size_t len);

mlockではaddrからlenバイト分の仮装メモリ領域を物理メモリへロックし、0を返します。

使用例

# include <stdlib.h>
# include <stdio.h>
# include <errno.h>
# include <sys/mman.h>

int main(int argc, char **argv)
{
    char *c    = "AAA";
    size_t ls  = sizeof(c);

    if (!mlock(c, ls))
        fprintf(stdout, "lock pass\n");
    else
        fprintf(stderr, "lock failed(%d)\n", errno);

  return 0;
}

munlock(): mlock() でピン留めした状態を解除する。

エラー

  • ENOMEM

RLIMIT_MEMLOCK を持つが、制限が許可している以上のメモリーをロックしようとした。 この制限は、プロセスが特権 (CAP_IPC_LOCK) を持っている場合は適用されない。

  • EPERM

呼び出し側が特権を持っていないが、 要求された操作を実行するには特権 (CAP_IPC_LOCK) が必要である。
mlock() と munlock() 用として:

参考リンク

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?