0
나는 fork(), wait() 및 execvp()를 더 잘 이해할 수있는 간단한 프로그램을 작성하고있다. 내 문제는 내가 프로그램을 실행 한 후, 컨트롤이 다시 쉘로 전달되지 않으며 나는 이유가 없다는 것이다. 내가 원하는 것은 코드가 끝나면 쉘에 다른 명령을 입력 할 수 있다는 것입니다. this을 살펴 보았지만 제 경우에는 해당되지 않는다고 생각합니다. 기본적으로 복사 된 코드는 here에서 찾았습니다.fork() 후 제어가 끊어짐
shell> # gcc test.c -o test
shell> # ./test
input program (ls)
# ls
input arg (.)
# .
test test.c extra.txt
# a;dlghasdf
# go back
# :(
내 코드 :
int main(void) {
//just taking and cleaning input
printf("input program (ls)\n");
char inputprogram [5] = {0,0,0,0,0};
fgets(inputprogram,5,stdin); //read in user command
int i;
for(i = 0; i < 5; i++) {
if(inputprogram [i] == '\n'){
inputprogram[i] = 0;
}
}
printf("input arg (.)\n");
char inputarg [5] = {0,0,0,0,0};
fgets(inputarg,5,stdin); //read in user command
for(i = 0; i < 5; i++) {
if(inputarg [i] == '\n'){
inputarg[i] = 0;
}
}
char per []= {inputarg[0], 0};
char *arg [] = {inputprogram, per , NULL};
int status = 0;
pid_t child;
//the fork(), execvp(), wait()
//////////////////////////////////
if ((child = fork()) < 0) {
/* fork a child process */
printf("*** ERROR: forking child process failed\n");
exit(1);
} else if(child == 0){
execvp(inputprogram, arg);
exit(1);
} else {
while(wait(&status != child));
}
return EXIT_SUCCESS;
}
좋은이 일을 참조하십시오. 왜 루프가 필요하지 않았습니까? 나는 자식 프로세스가 하나 뿐이니까? 나는 이것 [http://stackoverflow.com/questions/2708477/fork-and-wait-with-two-child-processes]를보고 왜 내가 혼란 스럽습니까 – Daniel
부모가되는 것보다 훨씬 쉽습니다. 돌볼 한 아이. 두 개가 있다면 루프가 필요합니다 (머리 뒤쪽에 눈이 있음). –