Using named pipes

If the z/OS® UNIX XL C/C++ application program you are developing requires its active processes to communicate with other processes that are active but may not be from the same program, code your application program to create a named pipe (FIFO file). Named pipes allow transfer of data between processes in a FIFO manner and synchronization of process execution. Use of a named pipe allows processes to communicate even though they do not know what processes are on the other end of the pipe. Named pipes differ from standard unnamed pipes, created using the pipe() function, in that they involve the creation of a real file that is available for I/O operations to properly authorized processes.

Within the application program, you create a named pipe by coding a mkfifo() or mknod() function. You give the FIFO a name and an access mode when you create it. If the access mode allows all users read and write access to the named pipe, any process that knows its name can use it to send or receive data.

Processes can use the open() function to access named pipes and then use the regular I/O functions for files, such as read(), write(), and close(), when manipulating named pipes. Buffered I/O functions can also be used to access and manipulate named pipes. For more information on the mkfifo() and mknod() functions and the file I/O functions, see z/OS XL C/C++ Runtime Library Reference.

Restriction: If fopen() is used to open named pipes in a multi-threaded environment, a deadlock will occur. This deadlock is caused by a named pipe waiting for the other end of the pipe to be opened, while still holding the fopen() multi-thread mutex. To prevent this deadlock, use open() to open the named pipe, instead of fopen().

z/OS UNIX does security checks on named pipes.

The following steps outline how to use a named pipe from z/OS UNIX XL C/C++ application programs:

  1. Create a named pipe using the mkfifo() function. Only one of the processes that use the named pipe needs to do this.
  2. Access the named pipe using the appropriate I/O method.
  3. Communicate through the pipe with another process using file I/O functions:
    1. Write data to the named pipe.
    2. Read data from the named pipe.
  4. Close the named pipe.
  5. If the process created the named pipe and the named pipe is no longer needed, remove that named pipe using the unlink() function.

A process running the following simple example program creates a new named pipe with the file pathname pointed to by the path value coded in the mkfifo() function. The access mode of the new named pipe is initialized from the mode value coded in the mkfifo() function. The file permission bits of the mode argument are modified by the process file creation mask.

As an example, a process running the program code (CCNGHF2) in Figure 1 creates a child process and then creates a named pipe called fifo.test. The child process then writes a data string to the pipe file. The parent process reads from the pipe file and verifies that the data string it reads is the expected one.
Note: The two processes are related and have agreed to communicate through the named pipe. They need not be related, however. Other authorized users can run the same program and participate in (or interfere with) the process communication.