IPC(一)

来源:互联网 发布:java根据ip获取经纬度 编辑:程序博客网 时间:2024/04/29 10:55

Communication Facilities

We can break the communication facilities into two categories:

Datatransfer facilities: The key factor distinguishing these facilities is the notion of writing and reading. Inorder to communicate, one process writes data to the IPC facility, and another process reads the data. These facilities require two data transfers between user memory and kernel memory: one transfer from user memory to kernel memory during writing, and another transfer from kernel memory to user memory during reading. 

Shared memory: Shared memory allows processes to exchange information byplacing it in a region of memory that is shared between the processes.  A process can make data available to other processes by placing it in the shared memory region. Because communication doesn’t require system calls or data transfer between user memory and kernel memory, sharedmemory can provide very fast communication.

Data transfer

We can further break data-transfer facilities into the following subcategories:

Byte stream: The data exchanged via pipes, FIFOs, and stream sockets is an undelimited byte stream. Each read operation may read an arbitrary number of bytes from the IPC facility, regardless of the size of blocks written by the writer.This model mirrors the traditional UNIX “file as a sequence of bytes” model.

Message: The data exchanged via System V message queues, POSIX messagequeues, and datagram sockets takes the form of  delimited messages. Each readoperation reads a whole message, as written by the writer process. It is not possible to read part of a message, leaving the remainder on the IPC facility; nor is it possible to read multiple messages in a single read operation.

Pseudoterminals: A pseudoterminal is a communication facility intended for use in specialized situations.

A few general features distinguish data-transfer facilities from shared memory:

1) Although a data transfer facility may have multiple readers, reads are destructive.A read operation consumes data,  and that data is not available to any other process.

2) Synchronization between the reader and writer processes is automatic. If a reader attempts to fetch data from a data-transfer facility that currently has no data, then (by default) the read operation will block until some process writes data to the facility.

3) Although shared memory provides fast communication, this speed advantageis offset by the need to synchronize operations on the shared memory. Forexample, one process should not attempt to access a data structure in theshared  memory while another process is updating it. A semaphore is the usualsynchronization method used with shared  memory.

4) Data placed in shared memory is visible to all of the processes that share that memory. (This contrasts with the destructive read semantics described above for data-transfer facilities.)

以上内容均来自:《The Linux Programming interface - A Linux and UNIX System Programming Handbook》


1 0