2012-04-07 1 views
1

나는 파일을 검색하고 결과를 파이프와 같은 다른 명령으로 전송하는 프로그램을 만들고있다. ls | sort 프로그램을 실행할 때 아무 일도 일어나지 않습니다. 내가 생각하는 문제는 자녀가 부모가 읽기를 시작하기 위해 SO 버퍼에서 쓰기를 중지 할 때까지 기다리는 것입니다. 이것은 stdout에 보내지는 것이고 파이프는 다른 명령으로 보내야하는 것입니다.파이프와 포크 문제

[email protected]:~/Escritorio/busca.2012$ ./busca . -n . -print 
    ./permisos.txt 
    ./busca2.c 
    ./mmap.pdf 
    ./busca3.c~ 
    ./cuadernoso4.2011b.pdf 
    ./busca.c~ 
    ./busca.c 
    ./busca2.c~ 
    ./busca3.c 

문제점이 무엇인지 이해가되지 않습니다.


 if(!strcmp(argv[4],"-pipe")) 
{ 
int pipefd[2]; 
int pid,dummi; 

if (pipe(pipefd)<0){ 
    perror("pipe"); 
    exit(1); 
} 

pid = fork(); 

if (pid<0){ 
    perror("fork"); 
    exit(1); 
} 
if (pid == 0){//Child process  
    close(pipefd[1]);//The child is only reading from the pipe 
    if(dup2(pipefd[0],0)!=0){perror("dup2");exit(1);} 
    close(pipefd[0]); 

     char *argumentos[argc-4]; 
    int j; 
    for (j=5;j<argc;j++){ 
     argumentos[j-5]=argv[j]; 
    }   
    argumentos[j-5]= NULL;  

    execvp(argv[5],argumentos); 
    perror("execve: "); 

}else{ //parent   
    close(pipefd[0]); 
    if(dup2(pipefd[1],1)!=1){perror("dup2");exit(1);} 
    close(pipefd[1]); 

    while(count--){ 
     if(strcmp(files[count]->d_name,".") && strcmp(files[count]->d_name,"..")){    
     printf("%s/%s\n",argv[1],files[count]->d_name);      
     free(files[count]); 
    } 

     wait(&dummi); 
} 

}//end pipe     
free(files); 
+0

을 할 수있는, 자식 프로세스는 ("중지")을 실행 계속 수행하거나 조기 종료합니까? 즉 : SIGCHLD 및/또는 SIGPIPE를 처리합니까? – wildplasser

+0

중단됩니다. 프로세스는 계속 실행됩니다. – beerLantern

+0

모두 인쇄 한 후 상위 표준 출력을 닫으면 작동합니다. 나중에 표준 출력에 쓰고 싶다면 다시 열려면 어떻게해야합니까? – beerLantern

답변

1

은 BTW argv와 [] 배열을 복제 할 이유가 없다. 대신

char *argumentos[argc-4]; 
    int j; 
    for (j=5;j<argc;j++){ 
     argumentos[j-5]=argv[j]; 
    }   
    argumentos[j-5]= NULL;  

    execvp(argv[5],argumentos); 

의 당신은 단지뿐만 아니라 정확히 증상은 무엇

execvp(argv[5],argv+5); 
+0

와우 감사합니다. 나는 그 세부 사항을 좋아한다. 실제로 작동합니다. 나는 stdout을 닫기 전에 fflush를 만들어야 만했다. 이제 작동합니다! – beerLantern