简体   繁体   中英

Shared memory pages between two different independent program in linux and c

I read about Shared memory from here . As per the document, two different program generate two different virtual addresses and those virtual addresses map to same physical page in RAM.

So when program1 accesses data of shared memory then it will be loaded from main memory to cache and next time program2/process2 will get the data from cache so lower access time for the same data for program2/process2.

I have successfully written a program in C language for IPC using shared memory to communicate between two programs and to modify a variable in one program and read from other program.

Now, my questions are as follows:

  1. Can "Shared memory" be automatically created between two independent programs? Means is it possible that two independent programs automatically create share memory by themselves or by OS, without manually/programmatically creating shared memory like for IPC (shmget/shmat)?

  2. When two different virtual addresses map to same physical memory using shared memory, is it always true that those two programs' common data is present OR it can be possible different data may be present at that shared memory location (which will cause more cache miss)?

  3. Can we decide or create share memory between two independent programs without knowledge of other programs?

  4. Suppose, in program-1 I have declared an Array-A of size 1 MB and in program-2 I have declared Array-B size 16KB. Now while performing summation operation in both programs, will it still benefitted due to shared memory?

Means When an element is loaded in cache for summation, then other program will use that cached value.

OR

There is no possibilities of shared memory as we are using two different Array and there elements are not useful to other program/process.

I am using GCC under Linux.

Your text is confusing.

Firstly, shared memory has little to do itself with CPU caches (L1, L2...). The effects of CPU caches on accessing a memory region from multiple processes or multiple threads will always be the same regardless of if the memory is shared via shm* or some other way. This is a very advanced topic, but here are some documents to get you started:

In short, the CPU caches are more of a hinderance to a programmer than something you can rely on, and you need careful synchornization with memory barriers and mutexes to ensure your processes (or threads) see the version of the data they need to see.

For question #1: you can create threads, which are also sometimes called light-weight processes, and they will share the entire memory space of their process. If you fork a process and the process has shared memory (which is explicitly created by you), the new process will inherit the shared memory. Other than that, no, arbitrarily sharing memory between processes is not usually done.

For the others: Shared memory will (in general) neither help nor hinder performance, if that's what you're asking.

  1. The OS actually does this when you fork a process. In this situation, the memory area where the actual code resides is shared between the two process (until the child process wants to write on its memory, it also shares the region where data resides. This mechanism is called copy-on-write). When using using a thread api to create a process (because threads in Linux are implemented as processes), the two threads share the same memory area (each runs on its own call stack, though)

  2. To answer this, first you need to understand the difference between a cache miss and a page fault, as page faults are what you're asking about here. Shared memory can, and indeed will be swapped out to disk by the kernel if it needs to. Remember that the physical address the virtual address is pointing to may change over time and may not even be in physical memory at a point. If many processes are constantly using that page, the odds that it will get swapped out MIGHT diminish under certain conditions, but don't count on that.

  3. Userland programs usually don't know anything about memory they don't own. Each process has its own virtual address space and accesses outside that space raise a memory access violation.

  4. As a rule of thumb, if two processes don't need to access the same data, you don't share memory between them. Any increases or decreases in performance for this are negligible.

No, there is no automatic mechanism for unrelated programs.

What you want to do is to define a file path that both programs know to use for shared memory. Then you'll use mmap to map pages of the file to memory. I would suggest mapping multiples of 4096 byte blocks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM