컴퓨터/언어,프로그래밍

간단한 pipe()에 대한 소스

스노우볼^^ 2009. 5. 27. 08:04
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

main()
{
	int fds[2];
	
	pipe(fds);		/* Create the pipe */
	/* First child reconnects stdin to downstream end of pipe
	   and closes the upstream end
	*/
	if (fork() == 0) {
		dup2(fds[0], 0);
		close(fds[1]);
		execlp("sort", "sort", 0);
	}

	/* Second child reconnects stdout to upstream end of pipe
	   and closes the downstream end
	*/

	else if (fork() == 0) {
		dup2(fds[1], 1);
		close(fds[0]);
		execlp("who", "who", 0);
	}
	
	/* Parent closes both ends of pipe and waits for both children
	   to finish
	*/
	else {
		close(fds[0]);
		close(fds[1]);
		wait(0);
		wait(0);
	}
}