2013-04-22 8 views
1

내 프로그램에서 2 개의 파이프를 설정하고 싶습니다. 나는 잘 작동하는 1 개의 파이프가 있지만 두 번째 파이프를 배치 할 위치를 알지 못합니다. 내 설정의 의사 코드는 다음과 같습니다C 프로그래밍 2 파이프

, 여기

//the first pipe: 
pipe(pipe1) 

//the second pipe: 
pipe(pipe2) 

pid = fork() 

if(pid == 0) { 

    dup2(pipe1[1], 1) 
    close(pipe1[0]) 
    execvp(beforepipe) 
    } 

if(pid > 0) { //everything below is in here 

    pid2 = fork() 

    if(pid2 == 0){ 

    //pipe1 
    dup2(pipe1[0],0) 
    dup2(out,1) 
    close(pipe1[1]) 
    execvp(afterpipe) 

    //pipe2 does not work might need to be placed in different area 
    dup2(pipe1[1],1) 
    close(pipe1[0]) 
    execvp(beforepipe1) 
    } 

    if(pid2 > 0){ 
    close(pipe[0]) 
    close(pipe[1]) 
    wait() //this is an infinite for loop 

    pid3 = fork() 

    if(pid3 == 0){ 
     dup2(pipe2[0],0) 
     dup2(out,1) 
     close(pipe2[1]) 
     execvp(afterpipe2) 
     } 

    if(pid3 > 0) { 
     close(pipe2[0]) 
     close(pipe2[1]) 
     wait() 
     } 
    } 

미안 중괄호와 함께있는 두 번째 파이프의 위치가 잘못된 위치에 있거나 코드가 전부입니다 잘못된.

제안 사항?

+0

무엇을하려고 하는가? 파이프 라인이 어떻게 보이는지 보여 줄 수 있습니까? (예 : ' -> pipe1-> -> pipe2->'파이프 1을 통해 프로그램 2에 쓰고, 파이프 2를 통해 프로그램 3에 쓰는 것) – ughoavgfhw

+0

your.outputc 이것은 작동 시키려고하는 명령입니다. 기본적으로 첫 번째 파이프는 input1에있는 "eschw obfuscation"을 뒤집어 "ESCHEW OBFUSCATION \"으로 변환합니다. 두 번째 파이프는 "\"를 q로 바꿔 "ESCHEW OBFUSCATIONq"을 얻습니다. 첫 번째 파이프는 작동하지만 두 번째 파이프는 작동하지 않습니다. –

+0

"tr : set1을 자르지 않을 때 string2는 출력에서 ​​비어 있지 않아야합니다"your.outputc의 "ESCHEW OBFUSCATION \"과 함께 –

답변

0

당신의 주된 문제는 당신이 충분한 파일 디스크립터 가까이 아무데도 닫히지 않는다는 것입니다. 문자열 "eschew obfuscation\"을 포함하는 현재 디렉토리의 input1 파일이 주어지면이 코드는 저에게 효과적입니다 (그러나 몇 개의 파일 설명자를 닫아야하는지주의하십시오!). 엄지

  • 규칙 : 파이프가 dup2() D 또는 표준 입력 또는 출력 dup() D 인 경우, 두 파일 파이프 파일 디스크립터를 닫는다.

예제 코드 (장소에 디버그 추적 포함) :

#include <unistd.h> 
#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 

/* command pipeline: cat input1 | tr a-z A-Z | tr \\ q */ 

int main(void) 
{ 
    int pipe1[2]; 
    int pipe2[2]; 
    pid_t pid1; 
    char *cmd1[] = { "cat", "input1",  0 }; 
    char *cmd2[] = { "tr", "a-z", "A-Z", 0 }; 
    char *cmd3[] = { "tr", "\\",  "q", 0 }; 

    if (pipe(pipe1) != 0 || pipe(pipe2) != 0) 
    { 
     perror("pipe failed"); 
     return 1; 
    } 

    pid1 = fork(); 

    if (pid1 < 0) 
    { 
     perror("fork 1 failed"); 
     return 1; 
    } 

    if (pid1 == 0) 
    { 
     /* Child 1 - cat */ 
     dup2(pipe1[1], 1); 
     close(pipe1[0]); 
     close(pipe1[1]); 
     close(pipe2[0]); 
     close(pipe2[1]); 
     execvp(cmd1[0], cmd1); 
     perror("failed to execute cmd1"); 
     return 1; 
    } 

    printf("pid 1 = %d\n", pid1); 
    fflush(stdout); 

    pid_t pid2 = fork(); 
    if (pid2 < 0) 
    { 
     perror("fork 2 failed"); 
     return 1; 
    } 

    if (pid2 == 0) 
    { 
     /* Child 2 - tr a-z A-Z */ 
     dup2(pipe1[0], 0); 
     dup2(pipe2[1], 1); 
     close(pipe1[0]); 
     close(pipe1[1]); 
     close(pipe2[0]); 
     close(pipe2[1]); 
     execvp(cmd2[0], cmd2); 
     perror("failed to execute cmd2"); 
     return 1; 
    } 

    printf("pid 2 = %d\n", pid2); 
    fflush(stdout); 

    pid_t pid3 = fork(); 
    if (pid3 < 0) 
    { 
     perror("fork 3 failed"); 
     return 1; 
    } 

    if (pid3 == 0) 
    { 
     /* Child 3 - tr \\ q */ 
     dup2(pipe2[0], 0); 
     close(pipe1[0]); 
     close(pipe1[1]); 
     close(pipe2[0]); 
     close(pipe2[1]); 
     execvp(cmd3[0], cmd3); 
     perror("failed to execute cmd3"); 
     return 1; 
    } 

    printf("pid 3 = %d\n", pid3); 
    fflush(stdout); 

    /* Parent - wait for the kids to all die */ 
    close(pipe1[0]); 
    close(pipe1[1]); 
    close(pipe2[0]); 
    close(pipe2[1]); 

    pid_t corpse; 
    int status; 
    while ((corpse = wait(&status)) > 0) 
     printf("Child %d died status 0x%.4X\n", corpse, status); 

    return 0; 
} 
+0

고마워요 @Jonathan Leffler –

0
execvp(afterpipe) 
//pipe2 does not work might need to be placed in different area 
dup2(pipe1[1],1) 
close(pipe1[0]) 
execvp(beforepipe1) 

나는 execvp()가 돌아 오지 않았다고 생각한다. 그래서 execvp() 아래의 코드는 무의미합니다.