미니쉘 만드는과정중에요 ..ㅠㅠ
청식
질문 제목 : 오류좀 봐주세요 ㅠㅠ질문 요약 :cd명령어 exit 명령어 관련질문 내용 :
#include sys/types.h
#include stdio.h
#include string.h
#include unistd.h
#include stdlib.h
int main()
{
pid_t pid;
char linestring[1000];
char command_string[1000];
char* strptr;
int i=0;
char* strarg[100];
while(1) {
i=0;
printf(basicshell);
gets(linestring);
strptr=(char*)strtok(linestring, \n);
if (strptr==null)
{
//prompt();
continue;
}
else if ((strcmp(strptr,exit))==0) { //////////////////////////여기부분실행시키자마자 exit 치면 베이직쉘 종료가 되거든여? 근데 다른명령어 치다가 exit치면
////////////두번 세번 눌러야 종료되요 ㅠㅠ
exit(1);
}
else if ((strcmp(strptr,cd))==0) { ///cd명령어는 아예 안먹어요 ㅠㅠ
chdir(command_string);
//prompt();
continue;
}
/* fork a child process */
pid = fork();
if (pid == 0) { /* child process */
strcpy(command_string,strptr);
while (strptr!=null) {
strarg[i++]=strdup(strptr);
strptr=(char*)strtok(null, \n);
}
strarg[i]=null;
execvp(command_string,strarg);
if (execvp(command_string,strarg)==-1) {
printf(not exist command\n);
}
} else { /* parent process */
/* parent will wait for the child to complete */
wait(null);
}
}
}
-
매력돋는 유진님
근데 질문과는 다른 이야기이긴 하지만 자식 프로세스 exec함수 쓰는데서 실패여부는 if문으로 검사안해도 됩니다. exec가 성공하면 그밑 코드는 이제 실행이 안되죠.
그리고 exec함수 실패후 오류메세지 띄우고 exit함수나 return함수 등으로 프로세스를 종료시켜야 합니다. 안그러면 반복문이 계속 실행되서 의도하지 않는 프로세스가 자꾸 생성되버려요