named_pipe |
Named pipes are often referred to as FIFOs (first in, first out). Named pipes are less restrictive than unnamed pipes and offer these advantages.
To create and open a named pipe, you must perform the following tasks during the construction sequence.
To create a named pipe without
opening it, use the static os_named_pipe::create()
function.
Writing to an opened named pipe adds data to the start of the FIFO queue and reads data from the end of the FIFO queue.
When a process finishes with a
named pipe, the process closes the pipe. When an os_named_pipe
is destroyed, the pipe is automatically closed.
A named pipe remains in the file
system until the pipe is explicitly removed. To remove a named pipe, use os_file_system::remove()
.
The primary behaviors of a named pipe are listed below.
SIGPIPE
signal.This section discusses uses of unnamed pipes.
The following example illustrates a simple client/server arrangement. The main program creates a server and two clients, waits for the clients to finish, and then terminates. The server opens the pipe twice: once for reading and once for writing. This prevents setting of the pipe's EOF flag in case a single client closes its end-of-pipe before the next client opens the pipe.
#include <iostream>
#include <ospace/file.h>
#include <ospace/unix.h>
int
server()
{
os_named_pipe pipe( "/tmp/pipe", O_RDONLY );
// Dummy prevents pipe from closing.
os_named_pipe dummy( "/tmp/pipe", O_WRONLY );
while ( pipe.ok() ) // Display input as it arrives.
{
char buffer[ 100 ];
// Read raw bytes.
int result = pipe.read( buffer, sizeof( buffer ) );
if ( pipe.ok() )
{
buffer[ result ] = 0; // Null terminate result.
cout << "Read: " << buffer << endl;
}
}
return 0;
}
int
client()
{
os_named_pipe pipe( "/tmp/pipe", O_WRONLY );
// Write raw bytes to pipe.
pipe.write( "hello there", 11 );
return 0;
}
int
main()
{
os_file_toolkit init_file;
os_unix_toolkit init_unix;
os_named_pipe::create( "/tmp/pipe", 0660 ); // Create named pipe.
// Create server and two client processes.
os_process child1( server );
os_process child2( client );
os_process child3( client );
os_this_process::wait_for_child( child2 );
os_this_process::wait_for_child( child3 );
child1.kill(); // Kill server.
os_file_system::remove( "/tmp/pipe" ); // Remove pipe.
return 0;
}
Read: hello there
Read: hello there
The os_named_pipe
class has an associated adapter for use with binary streams and text streams.
For convenience, an automatic conversion operator accepts an os_named_pipe
when an os_adapter is expected. For more
information about streams, refer to the Communications User Guide and
Reference Manual.
This section contains examples of using text streams and binary streams with a named pipe.
The following example creates a text stream on a named pipe. The parent process uses the text stream to send text to the child process.
#include <iostream>
#include <ospace/file.h>
#include <ospace/stream.h>
#include <ospace/unix.h>
// Child process to read the pipe.
int
reader()
{
os_named_pipe pipe( "tstream2.pip", O_RDONLY ); // Open named pipe.
os_tstream stream( pipe ); // Create a text stream on the pipe.
char buffer[ 32 ];
int num = -1;
// Read from the text stream.
stream.getline( buffer, sizeof( buffer ) );
stream >> num;
cout << "Read: " << buffer << endl << num << endl;
return 0;
}
int
main()
{
os_file_toolkit init_file;
os_streaming_toolkit init_streaming;
os_unix_toolkit init_unix;
// Create named pipe.
os_named_pipe::create( "tstream2.pip", 0660 );
// Spawn a reader.
os_process child( reader );
os_named_pipe pipe( "tstream2.pip", O_WRONLY ); // Open named pipe.
os_tstream stream( pipe ); // Create a text stream on the pipe.
char* txt = "This is a line of text.";
int num = 42;
cout << "Writing: " << txt << endl << num << endl;
stream << txt << endl << num << endl; // Write to the text stream.
// Wait for the child to finish.
os_this_process::wait_for_child( child );
os_file_system::remove( "tstream2.pip" );
return 0;
}
Writing: This is a line of text.
42
Read: This is a line of text.
42
The following example creates a binary stream on a named pipe. The parent process uses the binary stream to send objects to the child process.
#include <iostream>
#include <ospace/file.h>
#include <ospace/stream.h>
#include <ospace/time.h>
#include <ospace/unix.h>
// Child process to read the pipe.
int
reader()
{
os_named_pipe pipe( "bstream3.pip", O_RDONLY ); // Open named pipe.
os_bstream stream( pipe ); // Create binary stream on the pipe.
os_date d;
os_time t;
stream >> d >> t; // Read a date and time from the stream.
cout << "Read: " << d << " at " << t << endl;
return 0;
}
int
main()
{
os_file_toolkit init_file;
os_streaming_toolkit init_stream;
os_time_toolkit init_time;
os_unix_toolkit init_unix;
// Create named pipe.
os_named_pipe::create( "bstream3.pip", 0660 );
// Spawn a reader.
os_process child( reader );
os_named_pipe pipe( "bstream3.pip", O_WRONLY ); // Open named pipe.
os_bstream stream( pipe ); // Create binary stream on the pipe.
os_date today = os_date::today();
os_time now = os_time::now();
cout << "Writing: " << today << " at " << now << endl;
stream << today << now; // Write a date and time to the stream.
os_file_system::remove( "bstream3.pip" );
return 0;
}
Writing: 10/19/95 at 18:44:36
Read: 10/19/95 at 18:44:36
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.