I know for DLL, the executable is shared among processes. But for a user defined program, let's call it "test", when I run "test" on two terminals. Is the program executable going to be shared for these two processes, or does each one get a separate copy? Thanks.
In linux, is there sharing of executable binary between two processes running the same user program?
Asked
Active
Viewed 1,047 times
0
-
If both of them have different process id's, I think the executable code will be different too. – Anudeep Bulla Nov 05 '16 at 01:47
-
1Your question is vague. The moment you mentioned `DLL`, shared object comes to mind. In Linux it is equivalent to `*.so` files. These shared objects get loaded in memory when a user program is compiled against it or the user program loads it. But you followed up with two programs getting executed at the same time. This is not shared at all, in the context of userspace. The binary sitting on the disk is shared because it is not exclusively locked by the first execution. Both executions will have their own copy in memory and both will have separate program counter, stack pointer, variables, etc. – alvits Nov 05 '16 at 02:03
2 Answers
1
First the file is copied to kernel's page cache. When it is in already, then the second run will use this cached one. One cache per one file.

Ipor Sircer
- 3,069
- 3
- 10
- 15
-
I think you are referring to data cache. Programs are loaded differently. Each instance will have its own program counter, stack pointer, etc. In fact, calling `fork()` or `clone()` will duplicate the process, everything will be copied, but moving forward each will have its own copy. – alvits Nov 05 '16 at 02:05
0
The Text Section is shared (the code), the heap is not shared.
Whatever is loaded from disk, will share the disk cache.
@alvits is right in saying the heap will be duplicated on fork but it is done with Copy On Write (COW), meaning it is duplicated when needed, if data does not change, it does not occupy new memory.
This answer is similar: how is a shared library file called by two different processes in Linux?

Javier
- 2,752
- 15
- 30