리눅스 PIPE프로그래밍

여리왕자의 이미지

리눅스에서 파이프 프로그래밍을 하던중 잘 되지 않아 질문합니다.

문제는 ls -l | sort -n | wc 와 같이 파이프를 이용하여 다중실행을 하여야한다는 겁니다.
ls -l | sort -n 까지는 돌아가는데 두번째 파이프를 재지향 하는 부분이 이상한것같네요...

왜 3번째 놈은 실행이 안되는 것이며 커맨드라인조차 나타나지 않는것일까요....
고수님들이 좀 갈쳐주세요~~ 참고로 까만색 화면에 아무것도 나타나지 않는 현상도 좀 가르쳐
주세요... fork()로 프로그래밍을 하다보니 한번씩 이런 현상이 있던데...
왜 그런지 이유를 모르겠네요... 무슨 명령어가 들어올때까지 계속 대기하는거 같기도하구... ㅜㅜ

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
 
int main(void) {
   pid_t childpid;
   int fd[2], fd2[2];
 
   if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
      perror("Failed to setup pipeline");
      return 1;
   }
 
   if (childpid == 0) {                                  /* ls is the child */
      if (dup2(fd[1], STDOUT_FILENO) == -1) 
         perror("Failed to redirect stdout of ls");
      else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) 
         perror("Failed to close extra pipe descriptors on ls");
      else { 
         execl("/bin/ls", "ls", "-l", NULL);
         perror("Failed to exec ls");
      }
      return 1; 
   }
 
   pipe( fd2 );
 
   if ( (childpid = fork()) == 0 ) {
	   if (dup2(fd[0], STDIN_FILENO) == -1)               /* sort is the parent */
 	        perror("Failed to redirect stdin of sort");
 	   else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) 
    	 	perror("Failed to close extra pipe file descriptors on sort");
	   else {
		dup2( fd2[1], fd[1] );
      		execl("/bin/sort", "sort", "-n", NULL, NULL);
      		perror("Failed to exec sort");
   	  }
   }
 
   if ( dup2(fd2[0], fd[0]) == -1 ) 
	perror("Failed to redirect!");
   else if ((close(fd2[1]) == -1) || (close(fd[1]) == -1))
	perror("Failed to close extra pipe filed descriptors on sort");
   else {
	execl("/usr/bin/wc", "wc", NULL, NULL);
	perror("Failed to exec wc");
   }
   return 1; 
}