What is shared memory linux

Memory sharing in Linux with MMAP

MMAP is a UNIX system call that maps files into memory. It’s a method used for memory-mapped file I/O. It brings in the optimization of lazy loading or demand paging such that the I/O or reading file doesn’t happen when the memory allocation is done, but when the memory is accessed. After the memory is no longer needed it can be cleared with munmap call. MMAP supports certain flags or argument which makes it suitable for allocating memory without file mapping as well. In Linux kernel, the malloc call uses mmap with MAP_ANONYMOUS flag for large allocations.

In this article, I’ll be explaining how what mmap is and how it can be used for sharing memory in Linux. It kind of is the backbone of shared memory in Android.

Arguments and flags

mmap() creates a new mapping in the virtual address space of the calling process. If you check out the Linux kernel page for mmap you’ll see several arguments and flags. On the other hand, munmap() is used to free the allocated memory.

MMAP definition

  • The addr specifies the starting address of the allocation and if it’s passed as NULL the kernel chooses the starting address.
  • The length argument specifies the length of allocation in bytes and should be > 0 .
  • The prot argument describes the protection level
    • PROT_EXEC Pages may be executed.
    • PROT_READ Pages may be read.
    • PROT_WRITE Pages may be written.
    • PROT_NONE Pages may be not be accessed.

    The flags can be passed with bitwise OR operator and the default protection level is

    MUNMAP definition

    The munmap() system call deletes the mappings for the specified address range and causes further references to addresses within the range to generate invalid memory references. The region is also automatically unmapped when the process is terminated. On the other hand, closing the file descriptor does not unmap the region.

    • The addr is the address of allocation to free, essentially what you got from calling the mmap() . After calling munmap() , any access on the memory address shall raise SIGSEV errors.
    • The length determines the area of memory to clear. The area of memory from addr to addr + length would be freed on this call.

    Sharing memory with MMAP

    MMAP can be thought of as the core memory allocation API in Linux and several high-level constructs take advantage of this for providing various features. Linux kernel is the core of Android OS and components like ASHMEM uses MMAP in its core. ASHMEM is used for sharing memory in Android in different components like ContentProviders or Binder IPC.

    Sharing between parent and child

    This is fairly simple to visualize. A mmap allocation with MAP_SHARED flag can be accessed directly by the child process.

    This is very helpful in sharing the memory of core components in Android. All applications in Android are forked from a bare-bone process called Zygote which loads the core libraries and code required by all applications with mmap . Zygote is loaded into memory on device boot and when a user attempts to open an application for the first time the system forks Zygote and then the application logic is initialized.

    Sharing between siblings

    While it’s easy to visualize how memory can be shared in ancestry between a parent and child. The logic is very similar but involves Inter-Process Communication (IPC). Two common ways to achieve this could be:

    Without extra management layer

    The concept is similar, the two processes say Process 1 and Process 2 can communicate with each other via certain IPC technology.

    • Process 1 creates a file and allocates memory on that with MAP_SHARED flag and appropriate protection level and length. This process can write some data in the allocated memory space.
    • Process 1 shares this file descriptor with Process 2 via a certain IPC method.
    • Process 2 receives this file descriptor and calls mmap on that. So the system returns the virtual address of the same memory allocation and based on the protection levels set by Process 1 , Process 2 can read, write or execute the shared memory pages.

    However, these processes are responsible for explicitly deallocating memory, otherwise, it cannot be reused by another process in need of memory.

    With an extra management layer

    In this case, another process acts as the manager of shared memory and exposes interface or methods to allocate or retrieve memory allocations. Let’s say there is a memory manager called XMAN and exposes APIs like this:

    • Process 1 could allocate a chunk of memory using Xman_allocate() and share the Xman_allocation.fd with another process via a certain IPC mechanism.
    • Process 2 could use Xman_get() to get the same allocation and act on it.
    • Any of these processes could use the Xman_free() to explicitly free the memory.

    While the way of dealing with shared memory seems very similar with or without a manager instance, a centralized manager can abstract some memory freeing techniques thus taking away the expectation of being good citizens from the calling processes like:

    • Freeing memory after use, the Manager can take care of freeing when the calling processes die.
    • Some components like ASHMEM, support features like PINNING and UNPINNING section of memory which allows the consumer process to set which part of memory can be cleared when the system is out of free memory. This protects the consumer apps from being killed by the Low Memory Killer (LMK) when it’s reclaiming memory. ASHMEM has its process on deciding which UNPINNED memory to clear when available memory is system goes below a certain threshold.

    MISC: MMAP is faster than reading a file in blocks

    While exploring these concepts I was wondering how file-backed memory manages to be performant while file IO operation like read() is generally considered much slower than memory operations. There are a few interesting StackOverflow questions like Why mmap() is faster than sequential IO? and mmap() vs. reading blocks which answer this questions pretty well.

    But they won’t give you a short answer like — Because MMAP is magic! They are long reads.

    I wish I could add a TL;DR; answer to this question here but there isn’t one. Both mmap() and read() have their pros and cons and could be more performant in different situations. While mmap() seems like magic, it’s simply not.

    Future readings

    In the future, I intend to write about what ASHMEM is, how it works, why it was brought when MMAP existed and examples of how it’s been used in Android. Another interesting memory manager in Android is the ION memory manager which was added to Linux kernel in 2011 by a patch from Google to solve issues around large memory allocations needed by components like GPU, display, camera, etc.

    Источник

    ЋЎй п (б®ў¬Ґбв­® ЁбЇ®«м§гҐ¬ п) Ї ¬пвм

    Ћ¤Ё­ Ё§ б ¬ле Їа®бвле ¬Ґв®¤®ў ¬Ґ¦Їа®жҐбб®ў®Ј® ў§ Ё¬®¤Ґ©бвўЁп — ЁбЇ®«м§®ў вм ®Ўйго Ї ¬пвм. ЋЎй п Ї ¬пвм Ї®§ў®«пҐв ¤ўг¬ Ё«Ё Ў®«ҐҐ Їа®жҐбб ¬ ®Ўа й вмбп Є ®¤­®© Ё в®© ¦Ґ ®Ў« бвЁ Ї ¬пвЁ, Є Є Ўг¤в® ®­Ё ўбҐ ўл§лў «Ё malloc Ё Ё¬ Ўл«Ё ў®§ўа 饭л гЄ § ⥫Ё ­ ®¤­г Ё вг ¦Ґ дЁ§ЁзҐбЄго Ї ¬пвм. Љ®Ј¤ ®¤Ё­ Їа®жҐбб Ё§¬Ґ­пҐв Ї ¬пвм, ўбҐ ¤агЈЁҐ Їа®жҐббл «ўЁ¤пв» ¬®¤ЁдЁЄ жЁо.

    Ѓлбв஥ «®Є «м­®Ґ ў§ Ё¬®¤Ґ©бвўЁҐ

    ЋЎй п Ї ¬пвм — б ¬ п Ўлбва п д®а¬ ¬Ґ¦Їа®жҐбб®ў®Ј® ў§ Ё¬®¤Ґ©бвўЁп, Ї®в®¬г зв® ўбҐ Їа®жҐббл б®ў¬Ґбв­® ЁбЇ®«м§гов ®¤­г Ёвг ¦Ґ з бвм Ї ¬пвЁ. „®бвгЇ Є нв®© ®ЎйҐ© Ї ¬пвЁ ®бгйҐбвў«пҐвбп б в®© ¦Ґ бЄ®а®бвмо, зв® Ё ЇаЁ ®Ўа 饭ЁЁ Є ­Ґб®ў¬Ґбв­® ЁбЇ®«м§гҐ¬®© Ї ¬пвЁ, Ё нв® ­Ґ вॡгҐв бЁб⥬­®Ј® ўл§®ў Ё«Ё ўе®¤ ў п¤а®. ќв® в Є¦Ґ ­Ґ вॡгҐв Ё§«Ёи­ҐЈ® Є®ЇЁа®ў ­Ёп ¤ ­­ле.

    Џ®бЄ®«мЄг п¤а® ­Ґ бЁ­еа®­Ё§ЁагҐв ¤®бвгЇл Є б®ў¬Ґбв­® ЁбЇ®«м§гҐ¬®© Ї ¬пвЁ, ўл ¤®«¦­л б ¬Ё ®ЎҐбЇҐзЁвм бЁ­еа®­Ё§ жЁо. Ќ ЇаЁ¬Ґа, Їа®жҐбб ­Ґ ¤®«¦Ґ­ зЁв вм Ё§ Ї ¬пвЁ, Ї®Є ¤ ­­лҐ ­Ґ § ЇЁб ­л вг¤ , Ё ¤ў Їа®жҐбб ­Ґ ¤®«¦­л ­ ЇЁб вм Ї® ®¤­®¬г Ё ⮬㠦Ґ ¤аҐбг Ї ¬пвЁ ў ®¤­® Ё в® ¦Ґ ўаҐ¬п. ЋЎй п бва ⥣Ёп Ё§ЎҐ¦ ­Ёп гб«®ўЁ© Ј®­ЄЁ б®бв®Ёв ў ⮬, зв®Ўл ЁбЇ®«м§®ў вм ᥬ д®ал.

    Њ®¤Ґ«м Ї ¬пвЁ

    —в®Ўл ЁбЇ®«м§®ў вм ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ, ®¤Ё­ Їа®жҐбб ¤®«¦Ґ­ ўл¤Ґ«Ёвм ᥣ¬Ґ­в. ’®Ј¤ Є ¦¤л© Їа®жҐбб, ¦Ґ« ойЁ© ®Ўа й вмбп Є ᥣ¬Ґ­вг ¤®«¦Ґ­ Ї®¤Є«озЁвм ᥣ¬Ґ­в. Џ®б«Ґ ®Є®­з ­Ёп ҐЈ® ЁбЇ®«м§®ў ­Ёп ᥣ¬Ґ­в , Є ¦¤л© Їа®жҐбб ®вЄ«оз Ґв ᥣ¬Ґ­в. ‚ ­ҐЄ®в®ал© ¬®¬Ґ­в, ®¤Ё­ Їа®жҐбб ¤®«¦Ґ­ ®бў®Ў®¤Ёвм ᥣ¬Ґ­в.

    Џ®­Ё¬ ­ЁҐ ¬®¤Ґ«Ё Ї ¬пвЁ Linux Ї®¬®Ј Ґв ®Ўкпб­Ёвм Їа®жҐбб ўл¤Ґ«Ґ­Ёп Ё Ї®¤Є«о祭Ёп. Џ®¤ Linux , ўЁавг «м­ п Ї ¬пвм Є ¦¤®Ј® Їа®жҐбб а §ЎЁв ­ бва ­Ёжл. Љ ¦¤л© Їа®жҐбб Ї®¤¤Ґа¦Ёў Ґв ®в®Ўа ¦Ґ­ЁҐ ҐЈ® ¤аҐб®ў Ї ¬пвЁ ­ нвЁ бва ­Ёжл ўЁавг «м­®© Ї ¬пвЁ, Є®в®алҐ б®¤Ґа¦ в д ЄвЁзҐбЄЁҐ ¤ ­­лҐ. € е®вп Є ¦¤л© Їа®жҐбб Ё¬ҐҐв б®Ўб⢥­­лҐ ¤аҐб , ®в®Ўа ¦Ґ­Ёп ¬­®ЈЁе Їа®жҐбб®ў ¬®Јгв гЄ §лў вм ­ ®¤­г Ё вг ¦Ґ бва ­Ёжг, а §аҐи п б®ў¬Ґбв­®Ґ ЁбЇ®«м§®ў ­ЁҐ Ї ¬пвЁ.

    ‚뤥«Ґ­ЁҐ ­®ў®Ј® ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ ЇаЁў®¤Ёв Є б®§¤ ­Ёо бва ­Ёжл ўЁавг «м­®© Ї ¬пвЁ. Џ®бЄ®«мЄг ўбҐ Їа®жҐббл ¦Ґ« ов ®Ўа вЁвмбп Є ®¤­®¬г Ё ⮬㠦Ґ ®ЎйҐ¬г ᥣ¬Ґ­вг, в® в®«мЄ® ®¤Ё­ Їа®жҐбб ¤®«¦Ґ­ ўл¤Ґ«Ёвм ­®ўл© ®ЎйЁ© ᥣ¬Ґ­в. ‚뤥«Ґ­ЁҐ бгйҐбвўго饣® ᥣ¬Ґ­в ­Ґ б®§¤ Ґв ­®ўле бва ­Ёж, ў®§ўа й Ґв Ё¤Ґ­вЁдЁЄ в®а ¤«п бгйҐбвўгойЁе. —в®Ўл а §аҐиЁвм Їа®жҐббг ЁбЇ®«м§®ў вм ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ, Їа®жҐбб Ї®¤Є«оз Ґв ᥣ¬Ґ­в, Є®в®ал© ¤®Ў ў«пҐв ®в®Ўа ¦Ґ­ЁҐ ҐЈ® ўЁавг «м­®© Ї ¬пвЁ ­ ®ЎйҐ¤®бвгЇ­лҐ бва ­Ёжл ᥣ¬Ґ­в . Љ®Ј¤ а Ў®в б ᥣ¬Ґ­в®¬ § ўҐа襭 , нвЁ ®в®Ўа ¦Ґ­Ёп г¤ «повбп. Љ®Ј¤ ­Ё ®¤­Ё Ё§ Їа®жҐбб®ў ­Ґ е®зҐв ®Ўа й вмбп Є ᥣ¬Ґ­в ¬ ®ЎйҐ© Ї ¬пвЁ, Є Є®©-в® ®¤Ё­ Їа®жҐбб ¤®«¦Ґ­ ®бў®Ў®¤Ёвм бва ­Ёжл ўЁавг «м­®© Ї ¬пвЁ. ‚ᥠᥣ¬Ґ­вл ®ЎйҐ© Ї ¬пвЁ ўл¤Ґ«повбп Ї®бва ­Ёз­® Ё ®ЄагЈ«повбп ¤® а §¬Ґа бва ­Ёжл бЁб⥬л, Є®в®ал© пў«пҐвбп зЁб«®¬ Ў ©в®ў ў бва ­ЁжҐ Ї ¬пвЁ. Ќ бЁб⥬ е Linux , а §¬Ґа бва ­Ёжл а ўҐ­ 4 ЉЃ, ­® ўл ¤®«¦­л Ї®«гзЁвм нв® §­ 祭ЁҐ, ўл§лў п дг­ЄжЁо getpagesize .

    ‚뤥«Ґ­ЁҐ

    Џа®жҐбб ўл¤Ґ«пҐв ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ, ЁбЇ®«м§гп shmget (» SHared Memory GET «). …Ј® ЇҐаўл© Ї а ¬Ґва — 楫®зЁб«Ґ­­л© Є«оз, Є®в®ал© ®ЇаҐ¤Ґ«пҐв, Є Є®© ᥣ¬Ґ­в б®§¤ вм. ЌҐбўп§ ­­лҐ Їа®жҐббл ¬®Јгв ®Ўа й вмбп Є ®¤­®¬г Ё ⮬㠦Ґ ᥣ¬Ґ­вг, ЁбЇ®«м§гп ®¤­® Ё в® ¦Ґ Є«о祢®Ґ §­ 祭ЁҐ. Љ ᮦ «Ґ­Ёо, ¤агЈЁҐ Їа®жҐббл, ў®§¬®¦­®, в Є¦Ґ ўлЎа «Ё в®в ¦Ґ б ¬л© Є«оз, зв® ¬®¦Ґв ЇаЁўҐбвЁ Є Є®­д«ЁЄвг. €бЇ®«м§гп бЇҐжЁ «м­го Є®­бв ­вг IPC_PRIVATE Є Є Є«о祢®Ґ §­ 祭ЁҐ, Ј а ­вЁагҐвбп, зв® б®§¤ бвбп б®ўҐа襭­® ­®ўл© ᥣ¬Ґ­в Ї ¬пвЁ.

    …Ј® ўв®а®© Ї а ¬Ґва ®ЇаҐ¤Ґ«пҐв зЁб«® Ў ©в®ў ў ᥣ¬Ґ­вҐ. Џ®бЄ®«мЄг ᥣ¬Ґ­вл ўл¤Ґ«повбп Ї®бва ­Ёз­®, зЁб«® д ЄвЁзҐбЄЁ ўл¤Ґ«Ґ­­ле Ў ©в ®ЄагЈ«пҐвбп ¤® а §¬Ґа бва ­Ёжл.

    ’аҐвЁ© Ї а ¬Ґва — Ї®а §ап¤­®Ґ ¤ў®Ёз­®Ґ Ё«Ё §­ 祭Ё© д« ¦Є , Є®в®алҐ ®ЇаҐ¤Ґ«пов ®ЇжЁЁ Є shmget . ‡­ 祭Ёп д« ¦Є ўЄ«оз ов в ЄЁҐ Ї а ¬Ґвал:

    • IPC_CREAT — нв®в д« ¦®Є гЄ §лў Ґв, зв® ¤®«¦Ґ­ Ўлвм б®§¤ ­ ­®ўл© ᥣ¬Ґ­в. ќв® а §аҐи Ґв б®§¤ ў вм ­®ўл© ᥣ¬Ґ­в, ®ЇаҐ¤Ґ«пп Є«оз.
    • IPC_EXCL — нв®в д« ¦®Є, Є®в®ал© ўбҐЈ¤ ЁбЇ®«м§гҐвбп б IPC_CREAT ,§ бв ў«пҐв shmget ў®§ўа й вм ®иЁЎЄг, Ґб«Ё ᥣ¬Ґ­в­л© Є«оз ®ЇаҐ¤Ґ«Ґ­, Є Є 㦥 бгйҐбвўгойЁ©. ќв® ЁбЇ®«м§гҐвбп ¤«п ўл¤Ґ«Ґ­Ёп «нЄбЄ«о§Ёў­®Ј®» ᥣ¬Ґ­в . …б«Ё нв®в д« ¦®Є ­Ґ ¤ Ґвбп, Ё Є«оз бгйҐбвўго饣® ᥣ¬Ґ­в ЁбЇ®«м§гҐвбп, shmget ў®§ўа й Ґв бгйҐбвўгойЁ© ᥣ¬Ґ­в ў¬Ґбв® в®Ј®, зв®Ўл б®§¤ вм ­®ўл©.
    • Mode flags — нв® §­ 祭ЁҐ Ё§ 9 ЎЁв®ў, гЄ §лў ойЁе Їа ў , ЇаҐ¤®бв ў«Ґ­­лҐ ў« ¤Ґ«мжг, ЈагЇЇҐ, Ё ¬Ёаг(®бв «м­л¬), ­ гЇа ў«Ґ­ЁҐ ¤®бвгЇ®¬ Є ᥣ¬Ґ­вг. ЃЁвл ўлЇ®«­Ґ­Ёп ЁЈ­®аЁаговбп. Џа®бв®© бЇ®б®Ў ®ЇаҐ¤Ґ«Ёвм Їа ў б®бв®Ёв ў ⮬, зв®Ўл ЁбЇ®«м§®ў вм Є®­бв ­вл, ®ЇаҐ¤Ґ«Ґ­­лҐ ў Ё ®ЇЁб ­лҐ ў а §¤Ґ«Ґ 2 stat man-бва ­Ёж . Ќ ЇаЁ¬Ґа, S_IRUSR Ё S_IWUSR ®ЇаҐ¤Ґ«пов Їа ў ­ з⥭ЁҐ Ё § ЇЁбм ¤«п ў« ¤Ґ«мж ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ, S_IROTH Ё S_IWOTH ®ЇаҐ¤Ґ«пов Їа ў ­ з⥭ЁҐ Ё § ЇЁбм ¤«п ¤агЈЁе.

    Ќ ЇаЁ¬Ґа, нв®в ўл§®ў shmget б®§¤ Ґв ­®ўл© ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ (Ё«Ё ®Ўа й Ґвбп Є бгйҐбвўго饬г, Ґб«Ё shm_key 㦥 ЁбЇ®«м§гҐвбп), б Їа ў ¬Ё ­ зЁвҐ­ЁҐ Ё § ЇЁбм ў« ¤Ґ«м楬, ­® ­Ґ ¤агЈЁ¬Ё Ї®«м§®ў ⥫ﬨ.

    …б«Ё ўл§®ў гᯥ襭, shmget ў®§ўа й Ґв Ё¤Ґ­вЁдЁЄ в®а ᥣ¬Ґ­в . …б«Ё ᥣ¬Ґ­в ®ЎйҐ© Ї ¬п⨠㦥 бгйҐбвўгҐв, в® Їа ў ­ ¤®бвгЇ Їа®ўҐаҐ­л, Ё Їа®ўҐаЄ Ј а ­вЁагҐв, з⮠ᥣ¬Ґ­в ­Ґ ®в¬ҐзҐ­ ¤«п г¤ «Ґ­Ёп.

    Џ®¤Є«о祭ЁҐ Ё ®вЄ«о祭ЁҐ

    —в®Ўл ᤥ« вм ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ ¤®бвгЇ­л¬, Їа®жҐбб ¤®«¦Ґ­ ЁбЇ®«м§®ў вм shmat , » SHared Memory ATtach » ЏҐаҐ¤ ©вҐ Ґ¬г Ё¤Ґ­вЁдЁЄ в®а ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ SHMID , ў®§ўа 饭­л© shmget . ‚в®а®© Ї а ¬Ґва — гЄ § ⥫м, Є®в®ал© ®ЇаҐ¤Ґ«пҐв, Ј¤Ґ ў ¤аҐб­®¬ Їа®бва ­б⢥ ў 襣® Їа®жҐбб ўл е®вЁвҐ ®в®Ўа §Ёвм ®Ўйго Ї ¬пвм; Ґб«Ё ўл ЇҐаҐ¤ ¤ЁвҐ NULL , в® Linux ўлЎҐаҐв «оЎ®© ¤®бвгЇ­л© ¤аҐб. ’аҐвЁ© Ї а ¬Ґва — д« ¦®Є, Є®в®ал© ¬®¦Ґв ўЄ«озЁвм б«Ґ¤гойЁҐ Ї а ¬Ґвал:

    • SHM_RND гЄ §лў Ґв, зв® ¤аҐб, ®ЇаҐ¤Ґ«Ґ­­л© ¤«п ўв®а®Ј® Ї а ¬Ґва , ¤®«¦Ґ­ Ўлвм ®ЄагЈ«Ґ­ ­ § ¤ Є ¬­®¦ЁвҐ«о а §¬Ґа бва ­Ёжл. …б«Ё ‚л ­Ґ ®ЇаҐ¤Ґ«пҐвҐ нв®в д« ¦®Є, ‚л ¤®«¦­л ўла®ў­пвм ­ Ја ­Ёжг бва ­Ёжл ўв®а®© Ї а ¬Ґва ЇҐаҐ¤ ў Ґ¬л© shmat б ¬®бв®п⥫쭮.
    • SHM_RDONLY гЄ §лў Ґв, з⮠ᥣ¬Ґ­в Ўг¤Ґв ¤®бвгЇҐ­ в®«мЄ® ¤«п з⥭Ёп.
    • ЃЁвл Їа ў ¤®бв Ї в ЄЁҐ ¦Ґ Є Є Ё ¤«п д ©«®ў.

    …б«Ё ўл§®ў гᯥ襭, ®­ ўҐа­Ґв ¤аҐб Ї®¤Є«о祭­®Ј® ®ЎйҐЈ® ᥣ¬Ґ­в . Џ®в®¬ЄЁ, б®§¤ ­­лҐ ўл§®ў ¬Ё fork , ­ б«Ґ¤гов Ї®¤Є«о祭­лҐ ®ЎйЁҐ ᥣ¬Ґ­вл; ®­Ё ¬®Јгв ®вЄ«озЁвм ᥣ¬Ґ­вл ®ЎйҐ© Ї ¬пвЁ, Ґб«Ё § е®впв.

    Љ®Ј¤ ўл § Є®­зЁ«Ё а Ў®вг б ᥣ¬Ґ­в®¬ ®ЎйҐ© Ї ¬пвЁ, ᥣ¬Ґ­в ¤®«¦Ґ­ Ўлвм ®вЄ«о祭, ЁбЇ®«м§гп shmdt (» SHared Memory DeTach «). ЏҐаҐ¤ ©вҐ Ґ¬г ¤аҐб, ў®§ўа 饭­л© shmat . …б«Ё ᥣ¬Ґ­в Ўл« ®бў®Ў®¦¤Ґ­, Ё Ў®«миҐ ­Ґ ®бв «®бм Їа®жҐбб®ў, ЁбЇ®«м§гойЁе ҐЈ®, ®­ Ўг¤Ґв г¤ «Ґ­. ‚л§®ўл exit Ё exec ўв®¬ вЁзҐбЄЁ ®вЄ«оз ов ᥣ¬Ґ­вл.

    “Їа ў«Ґ­ЁҐ Ё ®бў®Ў®¦¤Ґ­ЁҐ ®ЎйҐ© Ї ¬пвЁ

    Shmctl (» SHared Memory ConTrol «) ўл§®ў ў®§ўа й Ґв Ё­д®а¬ жЁо ®Ў ᥣ¬Ґ­вҐ ®ЎйҐ© Ї ¬пвЁ Ё ¬®¦Ґв Ё§¬Ґ­Ёвм ҐЈ®.ЏҐаўл© Ї а ¬Ґва — Ё¤Ґ­вЁдЁЄ в®а ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ.

    —в®Ўл Ї®«гзЁвм Ё­д®а¬ жЁо ® ᥣ¬Ґ­вҐ ®ЎйҐ© Ї ¬пвЁ, ЇҐаҐ¤ ©вҐ IPC_STAT Є Є ўв®а®© Ї а ¬Ґва Ё гЄ § вҐ«м ­ struct shmid_ds .

    —в®Ўл г¤ «Ёвм ᥣ¬Ґ­в, ЇҐаҐ¤ ©вҐ IPC_RMID Є Є ўв®а®© Ї а ¬Ґва, Ё ЇҐаҐ¤ ©вҐ NULL Є Є ваҐвЁ© Ї а ¬Ґва. ‘ҐЈ¬Ґ­в г¤ «Ґ­, Є®Ј¤ Ї®б«Ґ¤­Ё© Їа®жҐбб, Є®в®ал© Ї®¤Є«озЁ« ҐЈ®, ®вЄ«озЁв ҐЈ®.

    Љ ¦¤л© ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ ¤®«¦Ґ­ Ўлвм пў­® ®бў®Ў®¦¤Ґ­, ЁбЇ®«м§гп shmctl , Є®Ј¤ ‚л § Є®­зЁ«Ё а Ў®вг б ­Ё¬, зв®Ўл Ё§ЎҐ¦ вм ­ аг襭ЁҐ бЁб⥬­®Ј® ЇаҐ¤Ґ« а §¬Ґа Є®«ЁзҐб⢠ᥣ¬Ґ­в®ў ®ЎйҐ© Ї ¬пвЁ. ‚л§®ўл exit Ё exec ®вЄ«оз в ᥣ¬Ґ­вл Ї ¬пвЁ, ­® ­Ґ ®бў®Ў®¦¤пв Ёе.

    ‘¬®ваЁ shmctl man-бва ­Ёжг ¤«п ®ЇЁб ­Ёп ¤агЈЁе ®ЇҐа жЁ©, Є®в®алҐ ¬®¦­® ўлЇ®«­пвм б ᥣ¬Ґ­в ¬Ё ®ЎйҐ© Ї ¬пвЁ.

    ЏаЁ¬Ґа Їа®Ја ¬¬л

    Џа®Ја ¬¬ «ЁбвЁ­Ј 5.1 Ё««обваЁагҐв ЁбЇ®«м§®ў ­ЁҐ ®ЎйҐ© Ї ¬пвЁ.

    Љ®¬ ­¤ ipcs ЇаҐ¤®бв ў«пҐв Ё­д®а¬ жЁо ®в­®бЁвҐ«м­® б।бвў ў§ Ё¬®¤Ґ©бвўЁп Їа®жҐбб®ў, ўЄ«оз п ®ЎйЁҐ ᥣ¬Ґ­вл Ї ¬пвЁ. €бЇ®«м§г©вҐ д« Ј -m , зв®Ўл Ї®«гзЁвм Ё­д®а¬ жЁо ®Ў ®ЎйҐ© Ї ¬пвЁ. Ќ ЇаЁ¬Ґа, нв®в Є®¤ Ё««обваЁагҐв з⮠ᥣ¬Ґ­в ®ЎйҐ© Ї ¬пвЁ, Їа®­г¬Ґа®ў ­­л© 1627649, ­ 室Ёвбп ў ЁбЇ®«м§®ў ­ЁЁ:

    …б«Ё нв®в ᥣ¬Ґ­в Ї ¬пвЁ Ўл« ®иЁЎ®з­® ®бв ў«Ґ­ Їа®Ја ¬¬®©, ўл ¬®¦ҐвҐ ЁбЇ®«м§®ў вм Є®¬ ­¤г ipcrm , зв®Ўл г¤ «Ёвм ҐЈ®.

    ‡ Ё Їа®вЁў

    CҐЈ¬Ґ­вл ®ЎйҐ© Ї ¬пвЁ Ї®§ў®«пов ®бгйҐбвў«пвм Ўлбваго ¤ўг­ Їа ў«Ґ­­го бўп§м б।Ё «оЎ®Ј® зЁб« Їа®жҐбб®ў. Љ ¦¤л© Ї®«м§®ў вҐ«м ¬®¦Ґв Ё зЁв вм Ё ЇЁб вм, ­® Їа®Ја ¬¬ ¤®«¦­ гбв ­®ўЁвм Ё б«Ґ¤®ў вм ­ҐЄ®в®а®¬г Їа®в®Є®«г ¤«п в®Ј®, зв®Ўл ЇаҐ¤®вўа вЁвм гб«®ўЁп Ј®­ЄЁ вЁЇ ЇҐаҐ§ ЇЁбЁ Ё­д®а¬ жЁЁ ЇаҐ¦¤Ґ, 祬 ®­ Їа®зЁв Ґвбп. Љ ᮦ «Ґ­Ёо, Linux бва®Ј® ­Ґ Ј а ­вЁагҐв нЄбЄ«о§Ёў­л© ¤®бвгЇ ¤ ¦Ґ Ґб«Ё ўл б®§¤ ¤ЁвҐ ­®ўл© ®ЎйЁ© ᥣ¬Ґ­в б IPC_PRIVATE .

    Ља®¬Ґ в®Ј®, ¤«п в®Ј® зв®Ў ­ҐбЄ®«мЄ® Їа®жҐбб®ў ¬®Ј«Ё ЁбЇ®«м§®ў вм ®Ўйго Ї ¬пвм, ®­Ё ¤®«¦­л ЇаЁ­пвм ¬Ґал, зв®Ўл ­Ґ ЁбЇ®«м§®ў вм ®¤Ё­ Ё в®в ¦Ґ Є«оз.

    Источник

    Читайте также:  Загрузить windows 10 команда
Оцените статью