#include <unistd.h>
#include <fcntl.h>

int main(int argc, char* argv[]) {
	/**
	 * pipefd[0]: read end
	 * pipefd[1]: write end
	 */
	int pipefd[2];
	pid_t cat_pid;
	pid_t echo_pid;
	
	if (pipe(pipefd) < 0) {
		perror("pipe");
		exit(-1);
	}
	
	cat_pid = fork();
	if (cat_pid == 0) {
		close(pipefd[1]); /* close unused end */
		dup2(pipefd[0], 0);
		execl("/bin/cat", "/bin/cat", NULL);
		perror("execl /bin/echo");
		return -1;
	}
	
	echo_pid = fork();
	if (echo_pid == 0) {
		close(pipefd[0]); /* close unused end */
		dup2(pipefd[1], 1);
		execl("/bin/echo", "/bin/echo", argv[0], NULL);
		perror("execl /bin/echo");
		return -1;
	}
	
	close(pipefd[0]);
	close(pipefd[1]);
	waitpid(cat_pid, NULL, 0);
	waitpid(echo_pid, NULL, 0);
	return 0;
}
