2011-09-28 6 views
1

으로 돌아 가지 않습니다. 아래 코드를 실행하고 "ls"를 입력하면 터미널에 ls가 실행되지만 거기에 앉아서 내 프롬프트가 다시 인쇄됩니다. 상위 프로세스로 돌아갈 컨트롤을 얻으려면 어떻게해야합니까?fork/execvp 컨트롤을 수행 한 후 상위

감사

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

int main(int argc, char* argv[]){ 
    while(1){ 
     print_the_prompt(); 
     char user_text[100]; 
     if(fgets(user_text, sizeof(user_text), stdin) != NULL){ 
      char* nl_char = strchr(user_text, '\n'); 
      if(nl_char != NULL){ 
       *nl_char = '\0'; 
      } 
     } 

    //printf("user_text = \"%s\"\n", user_text); 

     if(is_command_built_in(user_text)){ 
      //run built in command 
     } 
     else{ 
      //run regular command 
      execute_new_command(user_text); 
     } 
    } 

    return 0; 
} 

void print_the_prompt(){ 
     printf("!: "); 
} 

int is_command_built_in(char* command){ 
    return 0; 
} 

void execute_new_command(char* command){ 
    pid_t pID = fork(); 
    if(pID == 0){ 
     //is child 
     char* execv_arguments[] = { command, (char*)0 }; 
     execvp(command, execv_arguments); 
    } 
    else{ 
     //is parent 
     printf("im done"); 
    } 
} 

답변

1

대답은 부모 인쇄 바로 은 (는 별도의 프로세스입니다 때문에 병렬로 실행되는 기억) 아이를 시작한 후 "메신저 다"다음에 루프 회귀하는 것이 아마 자식이 파일을 나열하기도 전에 프롬프트를 인쇄하십시오. 뒤로 스크롤하면 다음 프롬프트가 나타날 것입니다.

부모가 자식을 마칠 때까지 기다리려면 wait() 가족 함수 중 하나를 사용해야합니다.

+0

오케이. 나는 wait()에 대해 몰랐다. 나는 이것을 조사 할 것이다. 당신 말이 옳았어요. "내가 끝났어." – james

관련 문제