2013-05-06 2 views
0

나는이 리눅스 명령을 C를 사용하여 구현하려하고있다. ls -l |부모와 자식 프로세스를 사용하는 리눅스에서 파이프 사용

(파일로 작성) -b 1

내가 그것을 -l 파일에 LS의 출력을 넣어 부모 프로세스 에서 ls -l 명령을 호출

이다하는 것을 시도하고있는 방법을 잘라 자식 프로세스 파일이 출력하는 것이다

인쇄 파일 에 컷을인가 (상기 부모 프로세스 작성된 것)을 읽고 절단 부르심 지금까지 내가 한 일

/* pipe.c */ 
#include <sys/wait.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 

void main() 
{ 
    int filedes[2]; 
    int p; 
    pid_t pid, pid1; 
    p=pipe(filedes); 
    FILE *stream; 
    char buff[20]; 

    printf("pipe command returns %d, %d ,%d\n",p, filedes[0],filedes[1]); 

    if(pipe(filedes) == -1) /* Create the pipe */ 
     printf("error pipe"); 
     pid1=fork(); 
     pid=getpid(); 
     switch (pid1) { /* Create a child process */ 
     case -1: 
     printf("error fork"); 
     case 0: /* Child */ 
     /* Close unused write end */ 
     /* Child can now read from pipe */ 
     if (close(filedes[1]) == -1) 
      printf("error close"); 
     printf("I am a child process pid %d, and will read from pipe\n",pid); 

     while (read(filedes[0], &buff, 1) > 0) 
      write(STDOUT_FILENO, &buff, 1); 

     write(STDOUT_FILENO, "\n", 1); 
     close(filedes[0]); 
     _exit(EXIT_SUCCESS); 

     break; 

     default: /* Parent */ 
     /* Close unused read end */ 
     /* Parent can now write to pipe */ 
     if (close(filedes[0]) == -1) 
      printf("error close"); 
     printf("I am the parent process pid %d, and will write to pipe\n", pid); 
     stream = fdopen(filedes[1], "w"); 
     strcpy(buff, "This is a test\n"); 
     write(filedes[1], buff, strlen(buff)); 

     char *args[80]; 
     args[0] = "ls"; 
     args[1] = "-l"; 
     args[2] = NULL; 
     execvp(args[0],args); 

     int bak, new; 
     bak = dup(1); 
     new = open("/home/urwa/abc.txt", O_WRONLY); 
     dup2(new, 1); 
     close(new); 



     close(filedes[1]);   /* Reader will see EOF */ 
     wait(NULL);    /* Wait for child */ 
     exit(EXIT_SUCCESS); 

     break; 
    } 
} 

이 코드 조각은 완벽하게 작동합니다. 테스트 결과를 스탠드 출력에 출력합니다. ls -l 출력뿐입니다. 파일은 비어 있습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까. 나는 또한 다음과 같이 freopen을 시도했다. 아직 빈 파일. :/

FILE *fp; 
fp = freopen ("/temp/abc.txt", "a+", stdout); 

답변

0

어린이의 컷을 호출하지 않았으며 파일 설명자도 여기에서 엉망입니다. 당신이 에이 작업을 수행하기 위해

부모의 표준 출력을 닫고 execvp는 전에 부모 에서 쓰기 최종 표준 출력을합니다. 자녀가있는 경우 자식의 stdin을 닫고 execvp 전에 자녀의 으로 stdin으로 읽음을 읽어야합니다. 그런 식으로 your parent's stdout is stdin of your child (파이프 B/W 두 개 생성).

int main() 
{ 
    int filedes[2]; 
    int p; 
    pid_t pid = 0, pid1 = 0; 
    p=pipe(filedes); 
    FILE *stream; 
    char buff[20]; 
    char *args[80]; 

    printf("pipe command returns %d, %d ,%d\n",p, filedes[0],filedes[1]); 

    if(pipe(filedes) == -1) /* Create the pipe */ 
     printf("error pipe"); 
     pid1=fork(); 
     pid=getpid(); 
     switch (pid1) { /* Create a child process */ 
     case -1: 
     printf("error fork"); break; 
     case 0: /* Child */ 
     /* Close unused write end */ 
     /* Child can now read from pipe */ 
     if (close(filedes[1]) == -1) 
      printf("error close"); 
     printf("I am a child process pid %d, and will read from pipe\n",pid); 

     close(0); //close stdin of child 
     dup(filedes[0]); //make pipes read end stdin of child 

     args[0] = "cut"; 
     args[1] = "-b"; 
     args[2] = "1"; 
     args[3] = NULL; 
     execvp(args[0],args); 
     break; 

     default: /* Parent */ 
     /* Close unused read end */ 
     /* Parent can now write to pipe */ 
     if (close(filedes[0]) == -1) 
      printf("error close"); 
     printf("I am the parent process pid %d, and will write to pipe\n", pid); 

     close(1); //close stdout 
     dup(filedes[1]); //make write end of pipe stdout of parent 
     args[0] = "ls"; 
     args[1] = "-l"; 
     args[2] = NULL; 
     execvp(args[0],args); 
     break; 
    } 
} 
+0

도움 주셔서 감사합니다. – urwaCFC