views:

24

answers:

1

Hi, guys. I am wondering how do unix-like OS implement shared memory? What is difference between accessing a normal user-space memory between accessing a memory unix in sytem IPC shared memory?

+1  A: 

Process memory is protected: outside of your program, normally no one can access it. This involves "important" gimmicks: your program has to believe it has the whole addressable space usable for himself, which is not the case. As I understand it, the address space of a process is split into pages (4k blocks I think), and the kernel has some sort of index for those pages, which maps them to physical memory or other devices (like your hard drive, that's how you do memory-mapped files). Whenever your process tries to access a memory address, it first goes to that map to see where the address actually points, and then does the accesses as requested. And whenever the process tries to access a page the kernel hasn't mapped anywhere, you get a segmentation fault.

So since memory is somewhat abstracted away, the kernel can do all kinds of tricks with it. Shared memory gotta be a sort of special case, where the kernel is asked to map pages from different processes' address space to the same physical location.

zneak
One way of doing is having a GLOBAL bit in the page table / TLB, marking those pages as shared. Of course, valid when talking of paged memory.
Tom
en.... It seems that mapped shared memory segments and process-private memory segments are almost the same from the view of the program. Does this imply that accessing a shared memory unit is no more time-consuming than accessing a normal memory unit?
ppan
@ppan: No. It doesn't imply any difference between accessing a page of private memory and accessing a page of shared memory. It's just that for shared memory, the kernel knows that in certain circumstances it's okay that two programs have a mapping to the same physical memory location.
zneak