Книга: Real-Time Concepts for Embedded Systems
8.2.4 Typical Pipe Operations
Разделы на этой странице:
8.2.4 Typical Pipe Operations
The following set of operations can be performed on a pipe:
· create and destroy a pipe,
· read from or write to a pipe,
· issue control commands on the pipe, and
· select on a pipe.
Create and Destroy
Create and destroy operations are available, as shown in Table 8.1.
Table 8.1: Create and destroy operations.
Operation | Description |
---|---|
Pipe | Creates a pipe |
Open | Opens a pipe |
Close | Deletes or closes a pipe |
The pipe operation creates an unnamed pipe. This operation returns two descriptors to the calling task, and subsequent calls reference these descriptors. One descriptor is used only for writing, and the other descriptor is used only for reading.
Creating a named pipe is similar to creating a file; the specific call is implementation-dependent. Some common names for such a call are mknod and mkfifo. Because a named pipe has a recognizable name in the file system after it is created, the pipe can be opened using the open operation. The calling task must specify whether it is opening the pipe for the read operation or for the write operation; it cannot be both.
The close operation is the counterpart of the open operation. Similar to open, the close operation can only be performed on a named pipe. Some implementations will delete the named pipe permanently once the close operation completes.
Read and Write
Read and write operations are available, as shown in Table 8.2.
Table 8.2: Read and write operations.
Operation | Description |
---|---|
Read | Reads from the pipe |
Write | Writes to a pipe |
The read operation returns data from the pipe to the calling task. The task specifies how much data to read. The task may choose to block waiting for the remaining data to arrive if the size specified exceeds what is available in the pipe. Remember that a read operation on a pipe is a destructive operation because data is removed from a pipe during this operation, making it unavailable to other readers. Therefore, unlike a message queue, a pipe cannot be used for broadcasting data to multiple reader tasks.
A task, however, can consume a block of data originating from multiple writers during one read operation.
The write operation appends new data to the existing byte stream in the pipe. The calling task specifies the amount of data to write into the pipe. The task may choose to block waiting for additional buffer space to become free when the amount to write exceeds the available space.
No message boundaries exist in a pipe because the data maintained in it is unstructured. This issue represents the main structural difference between a pipe and a message queue. Because there are no message headers, it is impossible to determine the original producer of the data bytes. As mentioned earlier, another important difference between message queues and pipes is that data written to a pipe cannot be prioritized. Because each byte of data in a pipe has the same priority, a pipe should not be used when urgent data must be exchanged between tasks.
Control
Control operations are available, as shown in Table 8.3.
Table 8.3: Control operations.
Operation | Description |
---|---|
Fcntl | Provides control over the pipe descriptor |
The Fcntl operation provides generic control over a pipe’s descriptor using various commands, which control the behavior of the pipe operation. For example, a commonly implemented command is the non-blocking command. The command controls whether the calling task is blocked if a read operation is performed on an empty pipe or when a write operation is performed on a full pipe.
Another common command that directly affects the pipe is the flush command. The flush command removes all data from the pipe and clears all other conditions in the pipe to the same state as when the pipe was created. Sometimes a task can be preempted for too long, and when it finally gets to read data from the pipe, the data might no longer be useful. Therefore, the task can flush the data from the pipe and reset its state.
Select
Select operations are available, as shown in Table 8.4.
Table 8.4: Select operations.
Operation | Description |
---|---|
Select | Waits for conditions to occur on a pipe |
The select operation allows a task to block and wait for a specified condition to occur on one or more pipes. The wait condition can be waiting for data to become available or waiting for data to be emptied from the pipe(s). Figure 8.5 illustrates a scenario in which a single task is waiting to read from two pipes and write to a third. In this case, the select call returns when data becomes available on either of the top two pipes. The same select call also returns when space for writing becomes available on the bottom pipe. In general, a task reading from multiple pipes can perform a select operation on those pipes, and the select call returns when any one of them has data available. Similarly, a task writing to multiple pipes can perform a select operation on the pipes, and the select call returns when space becomes available on any one of them.
Figure 8.5: The select operation on multiple pipes.
In contrast to pipes, message queues do not support the select operation. Thus, while a task can have access to multiple message queues, it cannot block-wait for data to arrive on any one of a group of empty message queues. The same restriction applies to a writer. In this case, a task can write to multiple message queues, but a task cannot block-wait on a group of full message queues, while waiting for space to become available on any one of them.
It becomes clear then that the main advantage of using a pipe over a message queue for intertask communication is that it allows for the select operation.
- 8.2.2 Pipe States
- 8.2.5 Typical Uses of Pipes
- Generalized identity operations
- Heap operations
- Arithmetic operations
- 8.5.2 Typical Condition Variable Operations
- Вызов pipe
- 11.7 Soft Timers and Timer Related Operations
- 2.2.1. Typical Embedded Linux Setup
- 7.3.3. Network Operations
- 8.3.1. Driver File System Operations
- 7.2.3. Standard Operations