Introduction


Shared memory APIs is the final topic of inter-process communication that we ll detail in this book. Shared memory allows two or more processes to share a chunk of memory (mapped to each of the process s individual address spaces) so that each can communicate with all others. Shared memory goes even further, as we ll see in this chapter.

Recall from Chapter 12, Introduction to Sockets Programming, that the address spaces for parent and child processes are independent. The parent process could create a chunk of memory (such as declaring an array), but once the fork completes, the parent and child see different memory. On GNU/Linux, all processes have unique virtual address spaces, but the shared memory API permits a process to attach to a common (shared) address segment.

With all this power comes some complexity. For example, when processes share memory segments, they must also provide a means to coordinate access to them.

This is commonly provided via a semaphore (by the developer), which can be contained within the shared memory segment itself. We ll look at this specific technique in this chapter.

If shared memory segments have this disadvantage , why not use an existing IPC mechanism that has built in coordination, such as message queues? The answer also lies in the simplicity of shared memory. Using a message queue, one process writes a message to a queue, which involves a copy from the user s address space to the kernel space. When another user reads from the message queue, another copy is performed from the kernel s address space to the new user s address space. The benefit of shared memory is that we minimize copying in its entirety. The segment is shared between the two processes in their own address spaces, so bulk copies of data are not necessary.

Note  

Because processes share the memory segment in each of their address spaces, copies are minimized in sharing data. For this reason, shared memory can be the fastest form of IPC available within GNU/Linux.




GNU/Linux Application Programming
GNU/Linux Application Programming (Programming Series)
ISBN: 1584505680
EAN: 2147483647
Year: 2006
Pages: 203
Authors: M. Tim Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net