마이쉘(myshell) 리눅스에서 구현 소스코드
- 최초 등록일
- 2010.11.12
- 최종 저작일
- 2010.11
- C언어
- 가격 3,000원
소개글
컴퓨터공학과에서 당골 과제로 나오는 마이쉘(myshell)을 리눅스에서 구현 하였습니다. 소스코드에 상세한 주석처리를 해놓았기 때문에 이해도 쉽게 되실겁니다.
파이프 라인과 모든 명령어가 실행이 가능합니다.
int main()
{
pid_t fork_result;
int in_pipes[2];
int out_pipes[2];
//set_path();
pipe(in_pipes);
pipe(out_pipes);
while(1){
type = read_arg();
if (type == COMMAND) {
fork_result = fork();
if (fork_result == -1) {
fprintf(stderr, "fork error\n");
exit(EXIT_FAILURE);
}
//child
if (fork_result == 0) {
execvp(args_p[0], args_p);
printf("Command not found!!\n");
exit(EXIT_FAILURE);
}
//parent
else
wait(0);
} else if (type == PIPE) {
fork_result = fork();
if (fork_result == -1) {
fprintf(stderr, "fork error\n");
exit(EXIT_FAILURE);
}
//child
if (fork_result == 0) {
if (first_pipe_arg) {
close(1);
dup(out_pipes[1]);
close(out_pipes[1]);
execvp(args_p[0], args_p);
exit(EXIT_FAILURE);
}
else if(!last_pipe_arg) {
close(0);
dup(in_pipes[0]);
close(in_pipes[0]);
close(in_pipes[1]);
close(1);
dup(out_pipes[1]);
close(out_pipes[1]);
execvp(args_p[0], args_p);
exit(EXIT_FAILURE);
}
else {
close(0);
dup(in_pipes[0]);
close(in_pipes[0]);
close(in_pipes[1]);
execvp(args_p[0], args_p);
exit(EXIT_FAILURE);
}
//parent
} else {
char buf[BUFSIZ];
int n;
if (!first_pipe_arg) {
close(in_pipes[0]);
close(in_pipes[1]);
}
wait(0);
if (!first_pipe_arg)
pipe(in_pipes);
close(out_pipes[1]);
if (!last_pipe_arg)
while ((n = read(out_pipes[0], buf, BUFSIZ)) > 0) {
write(in_pipes[1], buf, n);
}
close(out_pipes[0]);
pipe(out_pipes);
}
} else if (type == EXIT)
break;
}
return 0;
}
컴파일 실행환경
리눅스, 유닉스, 우분투
참고 자료
없음