2011-04-10 3 views
1

나는 '$ ls -l |'을 시뮬레이트하는 프로그램을 작성했다. wc -c '는 파이프를 사용하는 명령과 같습니다. 이제이 코드에서 어디에서 wait 또는 waitpid를 사용해야하는지 알 수 없습니다.이 코드에서 wait 및 waitpid는 어디에 사용해야합니까?

또한 어디에서 파이프를 닫아야합니까? 내 코드를 검토하고 제안하십시오.

while (wait(&status) != -1); 

특정 상황에서 당신이 waitpid을 방지 할 수 있습니다

int main ( int argc , char *argv[]) 
{ 

char *cmd_one_tokens[MAX]; /* Array of pointer to hold tokens of first command */ 
char *cmd_two_tokens[MAX]; /* Array of pointer to hold tokens of second command */ 

pid_t pid_one = -1 , /* variable to hold process id of first sub process */ 
     pid_two = -1 ; /* variable to hold process id of second sub process */ 
int status = -1 ; /* variable to read the status of the child process */ 
int pipe_fd[2] = {-1,-1}; /* Array to hold descriptors returned by pipe sys call */ 
int ret_val =-1 ; /* variable to hold return values by system calls */ 

/*Validate Number of command line arguments */ 
if(3 != argc) 
{ 
    printf("Provide Appropriate Arguments \n <lab3> < \"arg1\" > < \"arg2\" > \n"); 
    _exit(FAILURE); 

} 
/*Parse first command and get the tokens */ 
parse_command(argv[1],cmd_one_tokens); 
/*Parse second command and get the tokens */ 
parse_command(argv[2],cmd_two_tokens); 


/* Create pipe */ 
ret_val=pipe(pipe_fd); 
/*Error check */ 
if(ERROR==ret_val) 
{ 
    perror("Pipe creation error \n"); 
    _exit(FAILURE); 
} 
/*Fork First Child */ 
pid_one = fork() ; 
//Error check 
if(0 == pid_one) /*child process block */ 
{ 

    /* redirect stdout to pipe's write end for sub process one*/ 
    dup2(pipe_fd[1],1); 

    /*close pipe read end */ 
    close(pipe_fd[0]); 

    execvp(cmd_one_tokens[0],cmd_one_tokens); 

    /* if execvp returns then if must have failed */ 
    //Error check 

} 
else /*main process block */ 
{ 

    /*Wait for first sub process to finish */ 
    waitpid (pid_two , &status ,0); // <-------changes 

    /*Fork second subprocess */ 
    pid_two = fork(); 
      //Error check 
    if(0 == pid_two) /*second child process block */ 
    { 
     /* redirect stdin to pipe's read end for sub process two */ 
     dup2(pipe_fd[0],0); 

     /*close pipe write end */ 
     close(pipe_fd[1]); 

     execvp(cmd_two_tokens[0] , cmd_two_tokens); 
     /* if execvp returns then if must have failed */ 
     //Error check 

    } 
    else  /*main process block */ 
    { 
     status=-1; /*reset status */ 

     /*Waiting for the second sub process to finish in No hang fashion */ 
      waitpid (pid_two , &status ,0); // <-------changes 

    } 
} 
return(SUCCESS);   

}/*End of main */ 
+1

나는 정말로 [지난 시간의 조언] (http://stackoverflow.com/questions/5610184/input-redirection-problem-while-using-execvp/5610320#5610320)을 가져 와서 반환 값을 확인했으면 좋겠습니다. 다시 post하기 전에'fork (2)'에서 에러를 찾는다. – sarnold

+0

그래, 해냈어. 내가 제대로 기다려야하고 때로는 작품을 잘하고 ps에 pipe_w를 제공하는 시간이 좀 걸 렸어요 .... – Muse

답변

1

당신은 (아이들이 산란 후) 부모 프로세스에서 while 루프 두 아이들이 종료 될 때까지 지속적으로 wait를 호출을 배치 할 수 있습니다.

+0

나는 두 하위 프로세스에 대한 대기를 사용하지만, 응답하지 않습니다. 내가 ps를 사용하여 그것을 보았을 때 pipe_w를 준다 ........... – Muse

+0

hanging은 wait이다 :-) 더 구체적 일 수 있습니까? –

+0

프로그램이 영원히 정지합니다 .......... – Muse