Dissimilar Program Interaction?

adm at cbneb.UUCP adm at cbneb.UUCP
Fri Oct 3 01:02:24 AEST 1986


/***** cbneb:net.unix / uiucdcsb!kadie /  4:53 am  Oct  2, 1986 */

 > Unfortunately I don't know how to
> connect the pipe(s) to standard input and standard output
> of the sub-program.
> Also this does not permit fscan and fprint.

	/* The following code does not do any error checking */

	int fildes[2];
	int pid;
	int c;

	pipe (fildes);

	if( (pid = fork()) == 0 )
	{
	/* CHILD */

	    close(0);
    	    dup (fildes[0]);		/* Redirect stdin of child from parent*/
	    close(1);
    	    dup (fildes[1]);		/* Redirect stdout of child to parent */

	    close (fildes[0]);		/* Don't need these anymore */
	    close (fildes[1]);

	    exec_ (the_child);
	    /* 
	     * Now when the child writes to its file descriptor 1, it is 
	     * actually going down the pipe's write end. Likewise its read
	     * on file descriptor 0 is actually reading from the read end of
	     * the pipe.
	     */
	}

	/* PARENT */

	while(read(fildes[0], &c, 1))
	{
		munch (c);
	}

	/*
	 * This entire sequence of code can be repeated in an intelligent
	 * fashion for multiple child processes. You can simply poll the 
	 * various pipes by setting N_DELAY on the read end of the pipes
	 * using fcntl(), and looping around them.
	 *
	 * Caveat : Don't use fscanf (buffered i/o) on pipes as the child's
	 *	    buffering mechanism might not be exactly what you want.
	 *	    Follow the low-level 'read' with 'sscanf', which might
	 *	    be more deterministic.
	 */
	 

--------------------------------------------------------------------------------
S. Srinivasan 					UUCP: {cbosgd,ihnp4}!cbneb!srini
	     [AT&T Bell Labs,MiddleOfTheRoadOhio]
--------------------------------------------------------------------------------
		Now stick that in your pipe and smoke it !



More information about the Comp.unix mailing list