More on how to do a pipe (was re: system() problem)

L. Scott Emmons emmonsl at athena.ecs.csus.edu
Wed Mar 6 12:50:34 AEST 1991


After some thinking about my pipe(), fork(), exec() implementation in my
last post, I knew there was something funky about the way I did it...

Here is another version of my pipe program, about as efficient as my
previous version, but a bit cleaner (unless use of SIGCHLD is used for
something other than just closing the pipe() down.)

Here it is, hope you find it useful:

---CUT HERE begin pipe2.c---
/*
	This program shows how to (or one way to) implement fork() and exec()
	on a pipeline, redirecting the stdout of the exec()d program into
	the pipeline.

	This code was written by L. Scott Emmons, emmons at csus.csus.edu.
*/

#include <stdio.h>
#include <signal.h>

int	fd[2];

main()
{
	char	ch=0;

	pipe(fd);

	if (fork()) {
		close(fd[1]);
		while(read(fd[0],&ch,1))
			putchar(ch);
		close(fd[0]);
	} else {
		dup2(fd[1],1);
		execl("/usr/ucb/last","last","emmonsl",(char *)0);
	}
}
---CUT HERE end pipe2.c---

			L. Scott Emmons
			---------------
	emmons at csus.csus.edu  <or>  ...[ucbvax]!ucdavis!csus!emmons
		Packet: kc6nfp at kg6xx.#nocal.ca.usa.na



More information about the Comp.unix.programmer mailing list