28-01-2013, 04:10 PM
Interprocess Communication
Interprocess.pdf (Size: 283.06 KB / Downloads: 14)
INTRODUCTION
CHAPTER 3,“PROCESSES,” DISCUSSED THE CREATION OF PROCESSES and showed
how one process can obtain the exit status of a child process.That’s the simplest form
of communication between two processes, but it’s by no means the most powerful.The
mechanisms of Chapter 3 don’t provide any way for the parent to communicate with
the child except via command-line arguments and environment variables, nor any way
for the child to communicate with the parent except via the child’s exit status. None
of these mechanisms provides any means for communicating with the child process
while it is actually running, nor do these mechanisms allow communication with a
process outside the parent-child relationship.
This chapter describes means for interprocess communication that circumvent these
limitations.We will present various ways for communicating between parents and children,
between “unrelated” processes, and even between processes on different
machines.CHAPTER 3,“PROCESSES,” DISCUSSED THE CREATION OF PROCESSES and showed
how one process can obtain the exit status of a child process.That’s the simplest form
of communication between two processes, but it’s by no means the most powerful.The
mechanisms of Chapter 3 don’t provide any way for the parent to communicate with
the child except via command-line arguments and environment variables, nor any way
for the child to communicate with the parent except via the child’s exit status. None
of these mechanisms provides any means for communicating with the child process
while it is actually running, nor do these mechanisms allow communication with a
process outside the parent-child relationship.
This chapter describes means for interprocess communication that circumvent these
limitations.We will present various ways for communicating between parents and children,
between “unrelated” processes, and even between processes on different
machines.
Shared Memory
One of the simplest interprocess communication methods is using shared memory.
Shared memory allows two or more processes to access the same memory as if they all
called malloc and were returned pointers to the same actual memory.When one
process changes the memory, all the other processes see the modification.
Fast Local Communication
Shared memory is the fastest form of interprocess communication because all
processes share the same piece of memory. Access to this shared memory is as fast as
accessing a process’s nonshared memory, and it does not require a system call or entry
to the kernel. It also avoids copying data unnecessarily.
The Memory Model
To use a shared memory segment, one process must allocate the segment.Then each
process desiring to access the segment must attach the segment. After finishing its use
of the segment, each process detaches the segment. At some point, one process must
deallocate the segment.
Understanding the Linux memory model helps explain the allocation and attachment
process. Under Linux, each process’s virtual memory is split into pages. Each
process maintains a mapping from its memory addresses to these virtual memory pages,
which contain the actual data. Even though each process has its own addresses,multiple
processes’ mappings can point to the same page, permitting sharing of memory.
Memory pages are discussed further in Section 8.8,“The mlock Family: Locking
Physical Memory,” of Chapter 8,“Linux System Calls.”
Allocating a new shared memory segment causes virtual memory pages to be created.
Because all processes desire to access the same shared segment, only one process
should allocate a new shared segment. Allocating an existing segment does not create
new pages, but it does return an identifier for the existing pages.To permit a process
to use the shared memory segment, a process attaches it, which adds entries mapping
from its virtual memory to the segment’s shared pages.When finished with the segment,
these mapping entries are removed.When no more processes want to access
these shared memory segments, exactly one process must deallocate the virtual
memory pages.
Allocation
A process allocates a shared memory segment using shmget (“SHared Memory
GET”). Its first parameter is an integer key that specifies which segment to create.
Unrelated processes can access the same shared segment by specifying the same key
value. Unfortunately, other processes may have also chosen the same fixed key, which
could lead to conflict. Using the special constant IPC_PRIVATE as the key value guarantees
that a brand new memory segment is created.
Controlling and Deallocating Shared Memory
The shmctl (“SHared Memory ConTroL”) call returns information about a shared
memory segment and can modify it.The first parameter is a shared memory segment
identifier.
To obtain information about a shared memory segment, pass IPC_STAT as the
second argument and a pointer to a struct shmid_ds.
To remove a segment, pass IPC_RMID as the second argument, and pass NULL as the
third argument.The segment is removed when the last process that has attached it
finally detaches it.
Each shared memory segment should be explicitly deallocated using shmctl when
you’re finished with it, to avoid violating the systemwide limit on the total number of
shared memory segments. Invoking exit and exec detaches memory segments but
does not deallocate them
Processes Semaphores
As noted in the previous section, processes must coordinate access to shared memory.
As we discussed in Section 4.4.5,“Semaphores for Threads,” in Chapter 4,“Threads,”
semaphores are counters that permit synchronizing multiple threads. Linux provides a
distinct alternate implementation of semaphores that can be used for synchronizing
processes (called process semaphores or sometimes System V semaphores). Process semaphores
are allocated, used, and deallocated like shared memory segments. Although a
single semaphore is sufficient for almost all uses, process semaphores come in sets.
Throughout this section, we present system calls for process semaphores, showing how
to implement single binary semaphores using them.
Allocation and Deallocation
The calls semget and semctl allocate and deallocate semaphores, which is analogous to
shmget and shmctl for shared memory. Invoke semget with a key specifying a semaphore
set, the number of semaphores in the set, and permission flags as for shmget; the
return value is a semaphore set identifier.You can obtain the identifier of an existing
semaphore set by specifying the right key value; in this case, the number of semaphores
can be zero.