Another way to pass data between COBOL and C programs is through named pipes. Named pipes are a method for exchanging information between two unrelated processes.
To communicate via named pipes, the COBOL and C programs must be on the same host machine.
Technically, named pipes are files with known pathnames. Because a named pipe is associated with a pathname, unrelated processes can open the file to begin communications with one another. Because a C program can open a named pipe just as it would a normal file, no special code is required. By opening the file for reading, a process has access to the reading end of the pipe, and by opening the file for writing, a process has access to the writing end of the pipe. In effect, named pipes allow independent processes to "rendezvous" their I/O streams.
Named pipes can be created in two ways: via the command line or from within a program.
In UNIX, to create a named pipe with the file named "npipe" you can use the following command on the command line:
% mkfifo npipe
Alternatively, you could create the named pipe from within your program using:
int mkfifo(const char *path, mode_t mode)
where path is the path of the file and mode_t is the mode (permissions) with which the file should be created.
A named pipe can be opened using the open( ) system call or the fopen( ) standard C library function.
As with normal files, if the call succeeds, you get either a file descriptor or a "FILE" structure pointer, depending on how you opened the file. You can then use this information for reading or writing, depending on the parameters you passed to open( ) or fopen( ).
Reading from and writing to a named pipe are very similar to reading from and writing to a normal file. You can use the standard C library function calls read( ) and write( ).
Named pipes can also be used on Windows systems. You create Windows pipes with the CreateNamedPipe( ) API. You can then use the CreateFile( ) API to access the other end of the newly created named pipe.
Although named pipes can be very effective for communicating between COBOL and C applications, bear in mind the following: