[英]Does memory allocation show up when opening "/proc/self/maps"?
I have just started learning about virtual memory and I don't understand if I can see the memory that I have allocated with mmap() .我刚刚开始学习虚拟内存,我不知道是否可以看到我使用mmap()分配的内存。 The 2 show_maps() print the same text. 2 show_maps()打印相同的文本。 Shouldn't I also see the allocated memory from mmap() in the second show_maps() and if not is there a way to see it?我不应该在第二个show_maps()中看到mmap()分配的内存吗?如果没有,有没有办法看到它?
#define MAPS_PATH "/proc/self/maps"
#define LINELEN 256
void show_maps(void)
{
FILE *f;
char line[LINELEN];
f = fopen(MAPS_PATH, "r");
if (!f) {
printf("Cannot open " MAPS_PATH ": %s\n", strerror(errno));
return;
}
printf("\nVirtual Memory Map of process [%ld]:\n", (long)getpid());
while (fgets(line, LINELEN, f) != NULL) {
printf("%s", line);
}
printf("--------------------------------------------------------\n\n");
if (0 != fclose(f))
perror("fclose(" MAPS_PATH ")");
}
int main(void)
{
pid_t mypid;
int fd = -1;
uint64_t *pa;
mypid = getpid();
show_maps();
pa=mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
show_maps();
}
You did:你做了:
pa = mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
If you check the value of pa
, you'll find that it is MAP_FAILED
如果你检查pa
的值,你会发现它是MAP_FAILED
So, the actual mapping did not occur.所以,实际的映射并没有发生。
This is because you called mmap
with an fd
value of -1.这是因为您调用mmap
时fd
值为 -1。 So, the call had no backing store/file.因此,该调用没有后备存储/文件。
To fix this, add MAP_ANONYMOUS
:要解决此问题,请添加MAP_ANONYMOUS
:
pa = mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS,fd,0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.