mprotect/mlock使用demo

本文介绍了Linux中mprotect、mlock和mlockall函数的使用,通过示例演示了它们如何防止内存交换以提高效率。详细讨论了mlockall如何工作,以及它如何受进程资源限制(如RLIMIT_MEMLOCK)。同时,文中提到了mlockall在QEMU中的应用,并解释了为何在某些情况下,即使超过默认的锁定内存限制,仍能分配并锁定更多内存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

demo:

//mmc.cpp 以下两个例子都是可以使用,只是逻辑处理有点差异
#if 1 //**************示例1*************
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>

#define handle_error(msg) \
   do { perror(msg); exit(EXIT_FAILURE); } while (0)

static char *buffer;

static void handler(int sig, siginfo_t *si, void *unused)
{
   printf("Got SIGSEGV at address: 0x%lx\n",
           (long) si->si_addr);
   exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
	char *p;
	int pagesize;
	struct sigaction sa;

	sa.sa_flags = SA_SIGINFO;
	sigemptyset(&sa.sa_mask);
	sa.sa_sigaction = handler;
	if (sigaction(SIGSEGV, &sa, NULL) == -1)
	   handle_error("sigaction");

	pagesize = sysconf(_SC_PAGE_SIZE);
	if (pagesize == -1)
	   handle_error("sysconf");

	/* Allocate a buffer aligned on a page boundary;
	  initial protection is PROT_READ | PROT_WRITE */

	buffer = (char *)memalign(pagesize, 4 * pagesize);
	if (buffer == NULL)
	   handle_error("memalign");

	printf("Start of region:        0x%lx,pgsz %d.\n", (long) buffer, pagesize);

	for (p = buffer ; p < buffer + 4 * pagesize; p ++)
               *(p++) = 'a';

	printf("Write success with no protect.\n");
	if (mprotect(buffer + pagesize * 2, pagesize,PROT_READ) == -1)
	    handle_error("mprotect");

	for (p = buffer ; p < buffer + 4 * pagesize; p ++)
               *(p++) = 'b';
	
#if 0
	/* 测试发现len需要为页大小倍数,如果不为页大小倍数情况下,系统会匹配最大页大小倍数,
	   比如页大小为4k,len小于4k,修改范围[addr, addr+4k-1],如果大于4k小于8k,修改范围[addr, addr+8k-1],
	   以此类推。
	*/
	if (mprotect(buffer, 4097,PROT_READ) == -1)
		  handle_error("mprotect");
	int i = 0; 
	/* i<0x4000 (4 * pagesize) */
	for (p = buffer ;i<0x4000 ;p++)
	{
		if (i++ < 8192)
			continue;
		*p = 'a';
	}
	printf("End of region:        0x%lx\n", (long) buffer+i);
#endif
	printf("Loop completed\n");     /* Should never happen */
	exit(EXIT_SUCCESS);
}

#else //**************示例2*************
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>

int *g_ps32Result;

void add(int a, int b)
{
    *g_ps32Result = a + b;
}

void subtract(int a, int b)
{
    *g_ps32Result = a - b;
}

int main()
{
    int ret;
    int l_s32PageSize;
    
    /* 获取操作系统一个页的大小, 一般是 4KB == 4096 */
    l_s32PageSize = sysconf(_SC_PAGE_SIZE);
    if (l_s32PageSize == -1) {
        perror("sysconf fail");
        return -1;
    }
    printf("One Page Size is:%d Byte\r\n", l_s32PageSize);

    /* 按页对齐来申请一页内存, g_ps32Result会是一个可以被页(0x1000 == 4096)整除的地址 */
    ret = posix_memalign((void**)&g_ps32Result, l_s32PageSize, l_s32PageSize);
    if (ret != 0) {
        /* posix_memalign 返回失败不会设置系统的errno, 不能用perror输出错误 */
        printf("posix_memalign fail, ret %u\r\n", ret);
        return -1;
    }
    printf("posix_memalign mem %p\r\n", g_ps32Result);

    add(1, 1); // 结果写入 *g_ps32Result
    printf("the g_ps32Result is %d\n", *g_ps32Result);

    /* 保护g_ps32Result指向的内存, 权限设为只读 mprotect区间开始的地址start
       必须是一个内存页的起始地址,并且区间长度len必须是页大小的整数倍 */
    ret = mprotect(g_ps32Result, l_s32PageSize, PROT_READ);
    if (ret == -1) {
        perror("mprotect");
        return -1;
    }

    subtract(1, 1); // 结果写入 *g_ps32Result, 但 *g_ps32Result 的内存地址已设为只读, 所以会引发segment fault
    printf("the g_ps32Result is %d\n", *g_ps32Result);

    /* 申请一定记得释放 */
    free(g_ps32Result);
    return 0;
}

#endif

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

const int alloc_size = 32 * 1024 * 1024;//分配32M内存
int main()
{
        char *memory = malloc(alloc_size);
        if(mlock(memory,alloc_size) == -1) {
                perror("mlock");
                return (-1);
        }

        size_t i;
        size_t page_size = sysconf(_SC_PAGE_SIZE);
        for(i=0;i<alloc_size;i+=page_size) {
                printf("i=%zd\n",i);
                memory[i] = 0;
        }

        if(munlock(memory,alloc_size) == -1) {
                perror("munlock");
                return (-1);
        }

        return 0;
}

madvise:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

papaofdoudou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值