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");
}
}
oooooh ok。我不知道wait()。我會研究這一點。你是對的,它打印出「我完成了」,然後從ls打印出來。 – james