Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find shared/copied memory pages in forked processes in C

Tags:

c

linux

fork

memory

I have a linux server process that load big resources on startup. This processes will fork on request. The resources that are loaded on startup are the bigest stuff and will not change while runtime. The folked child processes use read/write control structures to handle requests to the constant resources.

How do I find out how much memory is shared between the processes and how many is uinque for every process? Or what pages are duplicated because of write access from any of the processes?

like image 698
Martin Avatar asked Jul 28 '11 15:07

Martin


People also ask

Does a forked process share memory?

What fork() does is the following: It creates a new process which is a copy of the calling process. That means that it copies the caller's memory (code, globals, heap and stack), registers, and open files.

Does fork () Copy memory?

Address space copying latency: Traditional fork() creates a copy of the memory image of the calling process, potentially copying large amounts of memory to the child.

What is shared between forked processes?

Answer: Only the shared memory segments are shared between the parent process and the newly forked child process. Copies of the stack and the heap are made for the newly created process.

When we use fork () How is the memory address space changed?

When a fork() system call is issued, a copy of all the pages corresponding to the parent process is created, loaded into a separate memory location by the OS for the child process. But this is not needed in certain cases. Consider the case when a child executes an "exec" system call or exits very soon after the fork().


1 Answers

You can get this information from the /proc/$pid/pagemap and /proc/kpagecount and /proc/kpageflags virtual files in the proc filesystem. Access to the latter requires root because it could leak privileged information about process memory mappings you don't own. Read Documentation/vm/pagemap.txt from the kernel docs for details on the data format.

like image 135
R.. GitHub STOP HELPING ICE Avatar answered Sep 24 '22 18:09

R.. GitHub STOP HELPING ICE