argv ==> stdin, got it

Lawrence W. McVoy mcvoy at rsch.WISC.EDU
Fri Nov 21 09:11:44 AEST 1986


This works very well.  My previous fears about speed were unfounded.
Now, here's a question:  what happen if I used vfork() instead?  I
thought that the parent sleeps until the child dies, but what if
the child blocks?  Like on I/O?  Would that work even better?


# include	<stdio.h>
# define	child_stdin	output[1]

yyerror(s)
    char* s;
{
    fprintf(stderr, "%s\n");
}

/*
 * code to fork a child and have control of the child's stdin/out
 * from usenet.  Works.  Fast, too.  The idea is that the command line
 * is fed to the stdin of the child.  This is so that you don't have
 * to f*ck with the stupid code in y.tab.c or lex.yy.c.  It should work
 * for anything that wants stdin.
 */
main(argc, argv)
    char** argv;
{
    int output[2];
    int i;

    pipe(output);		/* parent writes 1, child reads 0 */
    if (fork() == 0) {		/* child */
	close(0);
	dup(output[0]);
	return yyparse();
    }
    else {			/* parent */
	close(output[0]);	/* write only */
	for (i=1; i<argc; i++) 
	    write(child_stdin, argv[i], strlen(argv[i]));
	write(child_stdin, "\n", strlen("\n"));
	close(child_stdin);
	wait(0);		/* ASSUME: child doesn't fork */
    }
}
-- 
Larry McVoy 	        mcvoy at rsch.wisc.edu, 
      		        {seismo, topaz, harvard, ihnp4, etc}!uwvax!mcvoy

"They're coming soon!  Quad-stated guru-gates!"



More information about the Comp.unix.questions mailing list