OpenVMS Source-Code Demos

PIPE_DEMO_PARENT.C

//===============================================================================
// title   : PIPE_DEMO_PARENT.C
// platform: DECC on OpenVMS
// source  : http://h71000.www7.hp.com/doc/732final/5763/5763pro_026.html
//
// ver who when     what
// --- --- -------- -------------------------------------------------------------
// 100 DEC ???????? original code
// 101 CPQ ???????? maintenance
// 102 HP  ???????? maintenance
// 103 NSR 20120331 extensive modifications to learn more
//===============================================================================

#include <stdio.h>						//
#include <string.h>						//
#include <unistd.h>						// pipe, execl

int decc$set_child_standard_streams(int, int, int);		// forward declaration

main() {							//
    int  fdin[2], fdout[2], fderr[2];				//
    char msg[] = "parent: says hello";				//
    char buf[80];						//
    int  nbytes;						//
    int  yada;							//

    printf("parent: main 1\n");					//
    printf("channel numbers:\n");				//
    printf(" stdin      %d \n",fileno(stdin ));			//
    printf(" stdout     %d \n",fileno(stdout));			//
    printf(" stderr     %d \n",fileno(stderr));			//
    //
    // pipe docs:
    // http://h71000.www7.hp.com/commercial/c/docs/5763p040.html#index_x_1189
    //
    pipe(fdin);							//
    pipe(fdout);						//
    pipe(fderr);						//
    printf(" pipe fdin  %d %d \n",fdin[0] ,fdin[1]);		//
    printf(" pipe fdout %d %d \n",fdout[0],fdout[1]);		//
    printf(" pipe fderr %d %d \n",fderr[0],fderr[1]);		//
    printf("parent: main 2\n");					//
    //==========================================================
    // vfork docs:
    //   http://h71000.www7.hp.com/commercial/c/docs/5763p055.html#index_x_1566
    // vfork vs. fork:
    //   http://www.unixguide.net/unix/programming/1.1.2.shtml
    //
    // notes: this next statment will spit the execution path in two
    //        the path-2 doesn't start until execl() in path-1
    //==========================================================
    if ( vfork() == 0 ) {					//
	printf("parent: vfork path: a\n");			// vfork was successful
	//
	//	this next statement is specific to DECC on OpenVMS
	//	doc: http://h71000.www7.hp.com/doc/83final/5763/5763pro_029.html
	//
	decc$set_child_standard_streams(fdin[0], fdout[1], fderr[1]);
	//
	yada = 0;						// 0=run program, 1=execute script
	if (yada==0)
	{
	    execl( "pipe_demo_child", "yada" );			// this is the last executable statement
	}
	else
	{
	    execl( "pipe_demo_child.com", "yada" );		// this is the last executable statement
	}
	printf("parent: vfork path: b\n");			// this line will never execute
    }
    else
    {
	//
	//	this path doesn't start until execl() is executed above
	//
	printf("parent: path 2a\n");				// trace
	nbytes = read(fderr[0], buf, sizeof(buf));		// read from child via pipe
	if (nbytes>=0) {					//
	    buf[nbytes] = '\0';					// ensure data is null terminated
	    puts(buf);						// now send it to stdout
	}
	write(fdin[1], msg, sizeof(msg));			// write to child via pipe
	nbytes = read(fdout[0], buf, sizeof(buf));		// read from child via pipe
	if (nbytes>=0) {					//
	    buf[nbytes] = '\0';					// ensure data is null terminated
            puts(buf);						// now send it to stdout
	}
	nbytes = read(fderr[0], buf, sizeof(buf));		// read from child via pipe
	if (nbytes>=0) {					//
	    buf[nbytes] = '\0';					// ensure data is null terminated
	    puts(buf);						// now send it to stdout
	}
	printf("parent: path 2b\n");				// trace
    }
    printf("parent: main 3\n");					// all done
}