STREAMS-UX Programmer's Guide (February 2007)

STREAMS Mechanism and System Calls
Closing a Stream
Chapter 2
31
Closing a Stream
The last close() to a STREAMS file dismantles the stream as follows:
It pops modules (if any) from the stream, allowing any messages on the write queue of the module to be
drained by the module processing.
It closes the device, allowing any messages on the write queue of the driver to be drained by the driver
processing.
It dismantles the stream by freeing any messages left on the driver’s queues, and freeing the queues and
stream head structures.
close(2)
If O_NDELAY (or O_NONBLOCK) is clear, close will wait up to 15 seconds for each module to drain and up to 15
seconds for the driver to drain. If O_NDELAY (or O_NONBLOCK) is set, the pop is performed immediately and the
driver is closed without any delay.
Synopsis
#include <unistd.h>
int close(int fileds);
Return Value
The close (2) call returns a 0 upon successful completion, and a -1 in case of failure. The corresponding errno
is appropriately set, as with all standard UNIX File System I/O.
NOTE The close (2) frees structures allocated by STREAMS to the different components in the stream.
Modules or drivers must free any internal data structure or messages.
HP recommends that all files opened by the application, including STREAMS devices and pipes, should be
explicitly closed. In the case of pipes, both file descriptors should be closed.