在Linux中分配特定地址 [英] Allocating specific address in Linux

查看:60
本文介绍了在Linux中分配特定地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Linux的特定地址分配一个正在处理的内存. 其实我想做些类似的事情: 我将有一些过程. 每个进程都将在库(由我编写)中调用初始化函数,该函数将在进程的地址空间中分配一些内存(该内存将存储与进程相关的信息).这将由每个过程完成

I would like to allocate a memory in Linux in process at a specific address. Actually I would like to do something like : I will have number of process. Each process will call an initialization function in a library (written by me) which will allocate some memory in address space of the process (which will store process related information). This will be done by each process

一旦分配了此内存,后面的程序将调用库中的其他函数.现在,这些功能想访问第一个功能分配的内存(包含与过程相关的信息).

Once this memory is allocated, latter the program will call other function in the library. Now these function would like to access the memory allocated (containing process related information) by the first function.

问题是我无法在库中的进程地址空间中存储分配的内存地址(因为存在多个进程,甚至不能存储在静态指针中),而且我甚至都不希望用户程序存储该地址.我只是不希望用户程序知道库在其地址空间中分配了内存.库函数对他们来说是抽象的,他们只能使用它们.

The problem is that i cannot store the address of the memory allocated in the process address space in library (not even in static pointer as there are number of process) and i don't even want user program to store that address. I just don't want user program to know that there is memory allocated by library in their address space. Library function will be abstraction for them and they have to just use them.

是否有可能克服这个问题. 我在想,每当有任何进程调用分配内存的库的初始化函数时,无论其他所有事物如何,总是在所有进程中的同一地址(例如10000)处分配内存.

Is it possible to to over come this problem. I was thinking like, whenever any process calls initialization function of library which allocates memory , the memory always gets allocated at same address(say 10000) in all the process irrespective of all other things .

因此,想要访问该内存的任何库函数都可以通过以下方式轻松实现: char *p=10000;

So that any library function which wants to access that memory can easily do by : char *p=10000;

然后进行访问,这将访问称为库函数的进程的地址空间.

and then access, which will be access into the address space of the process which called the library function.

推荐答案

不是100%达到了您的目标,但是如果您想将内存映射到特定的设置地址,则可以使用MAP_FIXED标志来映射mmap() :

Not 100% I got what you are aiming for, but if you want to map memory into a specific set address you can use the MAP_FIXED flag to mmap():

当在flags参数中设置MAP_FIXED时,将通知实现,准确地应将pa的值添加为addr.如果设置了MAP_FIXED,mmap()可能会返回MAP_FAILED并将errno设置为[EINVAL].如果是MAP_FIXED请求成功后,由mmap()建立的映射将替换[pa,pa + len)范围内该进程页面的所有先前映射."

"When MAP_FIXED is set in the flags argument, the implementation is informed that the value of pa shall be addr, exactly. If MAP_FIXED is set, mmap() may return MAP_FAILED and set errno to [EINVAL]. If a MAP_FIXED request is successful, the mapping established by mmap() replaces any previous mappings for the process' pages in the range [pa,pa+len)."

请参见mmap手册页: http://linux.die.net/man/3/mmap

See mmap man page: http://linux.die.net/man/3/mmap

这篇关于在Linux中分配特定地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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