ptg10805159
576 Interprocess Communication Chapter 15
stack
shared memory
heap
uninitialized data
(bss)
initialized data
text
command-line arguments
and environment variables
high address
low address
0x7fff957b146c
0x00000060bd00
0x0000006020c0
array[]of 40,000 bytes
0x0000009fb6b0
0x0000009e3010
mallocof 100,000 bytes
0x7fba578c36a0
0x7fba578ab000
shared memory of 100,000 bytes
Figure 15.32Memory layout on an Intel-based Linux system
Recall that themmapfunction (Section 14.8) can be used to map portions of a file
into the address space of a process. This is conceptually similar to attaching a shared
memory segment using theshmatXSI IPC function. The main difference is that the
memory segment mapped withmmapis backed by a file, whereas no file is associated
with an XSI shared memory segment.
Example — MemoryMapping of/dev/zero
Shared memory can be used between unrelated processes. But if the processes are
related, some implementations provide a different technique.
The following technique works on FreeBSD 8.0, Linux 3.2.0, and Solaris 10. Mac OS X 10.6.8
currently doesn’t support the mapping of character devices into the address space of a process.
The device/dev/zerois an infinite source of 0 bytes when read. This device also
accepts any data that is written to it, ignoring the data. Our interest in this device for
IPC arises from its special properties when it is memory mapped.
•Anunnamed memory region is created whose size is the second argument to
mmap,rounded up to the nearest page size on the system.
•The memory region is initialized to 0.
•Multiple processes can sharethis region if a common ancestor specifies the
MAP_SHAREDflag tommap.
The program in Figure15.33 is an example that uses this special device.