Ipc System

Topics: Inter-process communication, Operating system, Remote procedure call Pages: 7 (2267 words) Published: July 13, 2013
Examples of IPC Systems

In this section, we explore three different IPC systems. We first cover the POSIX API for shared memory and then discuss message passing in the Mach operating system. We conclude with Windows, which interestingly uses shared memory as a mechanism for providing certain types of message passing.

3.5.1 An Example: POSIX Shared Memory
Several IPC mechanisms are available for POSIX systems, including shared memory and message passing. Here, we explore the POSIX API for shared memory. A process must first create a shared-memory segment using the shmget() system call (shmget() is derived from SHared Memory GET). The following example illustrates the use of shmget(): segment id = shmget(IPC PRIVATE, size, S IRUSR | S IWUSR); This first parameter specifies the key (or identifier) of the shared-memory segment. If this is set to IPC PRIVATE, a newshared-memory segment is created. The second parameter specifies the size (in bytes) of the shared-memory segment. Finally, the third parameter identifies the mode, which indicates how the shared-memory segment is to be used—that is, for reading, writing, or both. By setting the mode to S IRUSR | S IWUSR, we are indicating that the owner may read or write to the shared-memory segment. A successful call to shmget() returns an integer identifier for the shared-memory segment. Other processes that want to use this region of shared memory must specify this identifier. Processes that wish to access a shared-memory segment must attach it to their address space using the shmat() (SHared Memory ATtach) system call. The call to shmat() expects three parameters as well. The first is the integer identifier of the shared-memory segment being attached, and the second is a pointer location in memory indicating where the shared memory will be attached. If we pass a value of NULL, the operating system selects the location on the user’s behalf. The third parameter identifies a flag that allows the shared memory region to be attached in read-only or read-write mode; by passing a parameter of 0, we allow both reads and writes to the shared region. We attach a region of shared memory using shmat() as follows: shared memory = (char *) shmat(id, NULL, 0); If successful, shmat() returns a pointer to the beginning location in memory where the shared-memory region has been attached.

Once the region of sharedmemory is attached to a process’s address space, the process can access the shared memory as a routine memory access using the pointer returned from shmat(). In this example, shmat() returns a pointer to a character string. Thus, we could write to the shared-memory region as follows: sprintf(shared memory, "Writing to shared memory"); Other processes sharing this segment would see the updates to the shared memory segment. Typically, a process using an existing shared-memory segment first attaches the shared-memory region to its address space and then accesses (and possibly updates) the region of shared memory. When a process no longer requires access to the shared-memory segment, it detaches the segment from its address space. To detach a region of shared memory, the process can pass the pointer of the shared-memory region to the shmdt() system call, as follows: shmdt(shared memory);

Finally, a shared-memory segment can be removed from the system with the shmctl() system call, which is passed the identifier of the shared segment along with the flag IPC RMID.
The program shown in Figure 3.16 illustrates the POSIX shared-memory API just discussed. This program creates a 4,096-byte shared-memory segment. Once the region of shared memory is attached, the process writes the message Hi There! to shared memory. After outputting the contents of the updated memory, it detaches and removes the shared-memory region. We provide further exercises using the POSIX shared-memory API in the programming exercises at the end of this chapter.

3.5.2 An Example: Mach
As an example of a...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay about Introduction to Libray Information System
  • Essay on Hrm & Hr Sub-System
  • Essay about The Seven Phases of the Systems Development Life Cycle
  • SR rm 022 part3 Essay
  • Operating System Essay
  • Systems and Sub Systems Inter- Relationships Essay
  • Systems Thinking Essay
  • System Approach Essay

Become a StudyMode Member

Sign Up - It's Free