2016-06-16 3 views
-2

1 명의 부모와 2 명의 자녀가있는 코드를 만들려고합니다. 상기 방법은 3 개 개의 매개 변수를 받아 봐 : 라인이 쌍은누군가 내 코드를 C로 도울 수 있습니까?

  • 경우, 방법 proccess_pair 및 단어 1에 광고를 보내 original_file의 단어 1 상위는 파일을 줄을 판독

    word2. 행에 word1이 포함되어 있으면 file_1.txt에 행을 저장하십시오.

  • 행이 홀수 인 경우 process_odd 및 word2 행을 보냅니다. 될 것입니다 (chil1 및 chil2에 대한)

    int p_h1[2] // pipe from parent to child1 
    int p_h2[2];// pipe from parent to child2 
    
    int main(int argc, char **argv){ 
        pid_t pdi1, pdi2; 
        FILE *fd; // for original file 
        FILE *p_h1f, *p_h2f; //file create for child1 and child2 respectively 
        char buffer[1024];//buffer 
        if (pid1<0){ 
         fprintf(stderr,"Error fork \n %s \n",strerror(errno)); 
         exit(EXIT_FAILURE); 
         } 
        else if (pid1==0){//Im the child1 
         //proccess for child 1 
         proccess_pair(arg[2]); 
         exit(EXIT_SUCCESS); 
         } 
    pid2 = fork(); 
    if (pid2<0){ 
        fprintf(stderr,"Error fork \n %s \n",strerror(errno)); 
        exit(EXIT_FAILURE); 
        } 
    else if (pid2==0){//Im the child2 
         //proccess for child 2 
         proccess_odd(arg[2]); 
         exit(EXIT_SUCCESS); 
        } 
    
    //Parent dont read from pipe 
    
    close(p_h1[0]); 
    close(p_h2[0]); 
    
    fd = fopen(argv[1],"r"); //I openthe file for read it; 
    
    p_h1f = fdopen(p_h1[1],"w") 
    p_h2f = fdopen(p_h2[1],"w") 
    int i = 1; 
    
    while(fgets(buffer,1024,fd) != NULL){ 
        if (i % 2 ==0){ //check if the lines is pairs 
         fputs(buffer,p_h1f); 
         fflush(p_h1f); 
        }else{ 
         fputs(buffer,p_h2f); 
         fflush(p_h2f);   
        } 
        i++; 
    } 
    close(p_h1[1]); 
    close(p_h2[1]); 
    fclose(fd); 
    wait(NULL); 
    wait(NULL); 
    } 
    

    두 가지 방법 : 선이 단어 1을 포함 경우, file_2.txt에서 C에서

임 초보자 라인을 저장하고, 나는이 함께 노력

void proccess_pair(char *word1){ 
    FILE *fd; 
    fd = fopen("file_1.txt","w"); 
    //closing the not used 
    close(p_h1[1]); 
    close(p_h2[1]); 
    close(p_h2[0]); 

    int nsto = dup(1)//duplicate the stdout 
    dup2(fd,1);//changing stdout->file_1.txt 
    execlp("grep","grep",word1,NULL);//execution of grep 
    exit(EXIT_SUCCESS); 
    } 

임 학습과 내가 내가 도움을 필요로 이런 이유로, 많은 많은 오류가 있음을 알고, 이런 이유로 나는 단지 그들 중 하나를 구현 같은 (그러나 파이프의 올바른 측면을 폐쇄).

감사

전 C에서 배열에 많은 파이프를 만드는 방법을
+0

어린이 한 명과 함께 파이프를 사용하는 방법을 알고 있다면 어린이 2 명을 위해 뭔가 쓰고 코드를 게시하십시오. –

+0

@ Jean-BaptisteYunès 편집! 감사 ejeje – randall

답변

0

? 는 POSIX를 준수하는 시스템에서

, 당신은 할 수 당신이 선물로, int의 2 차원 배열의 요소에 pipe() 여러 번 호출하여.

나는 서로 다른 두 개의 파이프 (parent-child1, parent-child2)를 사용할 수 있습니까? 파이프 배열을 사용할 수 있습니까?

파이프 자체는 커널에서만 존재합니다. 파이프를 나타내는 사용자 공간 데이터 구조가 없으므로 파이프 배열을 가질 수 없습니다.

그러나 파이프 의 파일 설명자는으로 끝나며 단지 int입니다. pipe() 함수는 적어도 두 개의 배열이있는 두 번째 요소에 대한 포인터를 인수로 취하고 (성공시) 적절한 파일 설명자를 배열에 씁니다.

C 관점에서 파이프 끝을 반환 할 배열에 특별한 것은 없습니다. 특히 원하는 경우 다차원 배열의 요소가 될 수 있습니다. 또는 지역 변수가 될 수 있습니다. 또는 struct 또는 union 일 수 있습니다. 또는 동적으로 할당 된 공간의 충분히 큰 블록 일 수 있습니다. 그것은 특별하지 않습니다. 이 같은

0

뭔가 작업을해야합니다 :

int new_process(char *word){ // return the writing part of a pipe to a newly created child 
    int p[2]; 
    pipe(p); // get a new pipe 
    if (fork()==0) { // create a new child 
    dup2(p[0],0); // child's in is pipe's entry 
    close(p[1]); // close writing part 
    execlp("grep","grep",word,NULL); // exec 
    } 
    close(p[0]); // parent don't need the reading part 
    return p[1]; // send back the writing part to caller 
} 

int main(int argc, char **argv) { 
    int f1 = new_process(argv[1]); // get the pipe to first child 
    int f2 = new_process(argv[1]); // ...second... 
    char buffer[1024];//buffer 

    FILE *fd = fopen(argv[1],"r"); // open the file for reading; 

    while(fgets(buffer,1024,fd) != NULL) { // read aline 
    if (i % 2 ==0) { //check if the line no is pair 
     fputs(buffer,f1); // send it to first child 
     fflush(f1); 
    } else{ 
     fputs(buffer,f2); // send it to second child 
     fflush(f2);   
    } 
    i++; 
    } 
    close(f1); 
    close(f2); 
    fclose(fd); 
    wait(NULL); 
    wait(NULL); 
} 

이 실패에 필요한 컨트롤을 추가하는 것을 잊지 마십시오.

관련 문제