2

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?

  • 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 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 at 21:28

Your Answer

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

Browse other questions tagged or ask your own question.