OpenVMS Source Code Demos

PIPE_DEMO_ORIGINAL.C

//
// parent.c
// ========
// http://h41379.www4.hpe.com/doc/732final/5763/5763pro_026.html
//
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int decc$set_child_standard_streams(int, int, int);

main()
{
    int fdin[2], fdout[2], fderr[2];
    char msg[] = "parent writing to child's stdin";
    char buf[80];
    int nbytes;

    pipe(fdin);
    pipe(fdout);
    pipe(fderr);

    if ( vfork() == 0 ) {
	decc$set_child_standard_streams(fdin[0], fdout[1], fderr[1]);
	execl( "child", "child" );
    }
    else {
	write(fdin[1], msg, sizeof(msg));
	nbytes = read(fdout[0], buf, sizeof(buf));
	buf[nbytes] = '\0';
	puts(buf);
	nbytes = read(fderr[0], buf, sizeof(buf));
	buf[nbytes] = '\0';
	puts(buf);
    }
}

//
// child.c
// =======
//

#include <stdio.h>
#include <unistd.h>

main()
{
    char msg[] = "child writing to stderr";
    char buf[80];
    int nbytes;

    nbytes = read(0, buf, sizeof(buf));
    write(1, buf, nbytes);
    write(2, msg, sizeof(msg));
}

$!
$! child.com
$! =========
$!
$ read sys$command s
$ write sys$output s
$ write sys$error "child writing to stderr"

Notes:

This example program returns the following for both child.c and child.com :

sample run
==========
$ run parent
parent writing to child's stdin
child writing to stderr