update: userprog (tee, tsh, sleep, hex2bin)

This commit is contained in:
2026-01-01 04:22:30 +00:00
parent cf722d75ff
commit 79f46aa971
22 changed files with 1196 additions and 47 deletions
+2 -15
View File
@@ -60,7 +60,6 @@ int parse_cmd(char *cmd, char *argv[]) {
int handle_pipe(char *argv[]) {
int pipe_idx = -1;
// 1. 첫 번째 파이프 기호(|) 찾기
for (int i = 0; argv[i] != NULL; i++) {
if (strcmp(argv[i], "|") == 0) {
pipe_idx = i;
@@ -68,28 +67,23 @@ int handle_pipe(char *argv[]) {
}
}
// 파이프가 없으면 0 반환 (호출자가 일반 execvp 실행)
if (pipe_idx == -1) return 0;
// 2. 명령어 쪼개기
argv[pipe_idx] = NULL; // 파이프 기호를 NULL로 바꿔서 왼쪽 끊음
argv[pipe_idx] = NULL;
char **left_cmd = &argv[0];
char **right_cmd = &argv[pipe_idx + 1];
// 오른쪽 명령어가 없으면 에러 (예: "ls |")
if (right_cmd[0] == NULL) {
printf("gmsh: syntax error near unexpected token `|'\n");
return 1;
}
// 3. 파이프 생성
int fds[2];
if (pipe(fds) == -1) {
perror("pipe");
return 1;
}
// 4. 왼쪽 자식 (Writer)
pid_t pid1 = fork();
if (pid1 == 0) {
close(fds[0]);
@@ -102,22 +96,16 @@ int handle_pipe(char *argv[]) {
exit(1);
}
// 5. 오른쪽 자식 (Reader + Recursion Manager)
pid_t pid2 = fork();
if (pid2 == 0) {
close(fds[1]);
dup2(fds[0], STDIN_FILENO); // 입력 연결
dup2(fds[0], STDIN_FILENO);
close(fds[0]);
// [핵심] 오른쪽 명령어에 또 파이프가 있는지 확인!
// 재귀 호출: 만약 파이프가 또 있다면 handle_pipe가 다시 fork를 뜨고 처리함
if (handle_pipe(right_cmd) == 1) {
// 재귀 호출 내부에서 자식들을 다 만들고 기다린 후 리턴했음.
// 이 중간 관리자 프로세스는 할 일을 다 했으므로 종료.
exit(0);
}
else {
// 파이프가 더 이상 없으면 그냥 실행
handle_redirection((const char**) right_cmd);
execvp(right_cmd[0], right_cmd);
perror("execvp right");
@@ -125,7 +113,6 @@ int handle_pipe(char *argv[]) {
}
}
// 6. 부모 프로세스
close(fds[0]);
close(fds[1]);