4

If one program, for example grep, is curretly running, and a user executes another instance, do the two instances share the read-only .text sections between them to save memory? Would the sharing of the main executable text sharing be done similarly to shared libraries?

Is this behavior exhibited in Linux? If so, do other Unices do so as well?

If this is not done in Linux, would any benefit come from implementing executables that often run multiple instances in parallel as shared libraries, with the invoked executable simply calling a main function in the library?

3
  • 1
    Strictly speaking, it doesn't have to be shared. But in the real-life they are always shared because they're mmaping the same file region – 炸鱼薯条德里克 Dec 31 '18 at 4:45
  • @炸鱼薯条德里克 Does the characteristic of mapping the same file region always lead to sharing in memory? – novice Jan 1 '19 at 18:21
  • 1
    @novice According to the man page, yes and no. Private mmap areas are mapped as copy-on-write. So read-only will be shared. see: man7.org/linux/man-pages/man2/mmap.2.html . Note this is done at the file level. There is a step for the program loaded to link shared libraries together before running. I suspect the answer to your last question is that it depends on the application. most likely it won't be more efficient, or the savings will be tiny. – Philip Couling Jan 12 '19 at 21:28
2

Unix shares executables, and shared libraries are called shared (duh...) because their in-memory images are shared between all users.

I.e., if I run two instances of bash(1), and in one of them run, say, vim(1), I'll have one copy each of the bash and the vim executables in memory, and (as both programs use the C library) one copy of libc.

But even better: Linux pages from the disk copies of the above executables/libraries (files). So what stays in memory is just those pages that have been used recently. So, code for rarely used vim commands or bash error handling, not used functions in libc, and so on just use up disk space, not memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.