#Shell Concepts
shell 셸이란 무엇일까? 셸이란 명령어를 해석하는 데 사용하는 프로그램 즉, Command Interpreter 이다. 셸은 사용자가 입력하는 명령어를 해석하고 수행하여 그 결과를 화면에 표시하여 준다. 셸을 터미널과 같다고 생각할 수도 있는데, 터미널은 셸의 프론트 엔드라고 생각하면 된다.
#Behind the Scenes of a shell
- FOUR STEPS for non-built-in
- 셸이 사용자 입력을 기다린다
- 셸이 command를 해석한다
- 프로세스를 fork한다
- Foreground일 경우 parent process는 child process가 끝날때까지 기다린다. Foreground가 아닐경우 parent process가 process를 반복한다 (child process가 command를 실행함).
#셸의 추가 기능
- Foreground / Background Processes
- Process Groups
- Built-in Commands
- I/O Piping
- I/O Redirection
- Signal Handling
#Foreground / Background Processes
셸은 foreground 또는 Background로 프로세스를 fork할 수 있다.
- Foreground: 한 번에 하나의 포그라운드 프로세스 그룹, 터미널에 액세스 가능
- Background: &'을 사용하면 실행할 명령이 백그라운드로 전송됨, 터미널 액세스 불가능
#Process Groups
- 각 작업(job)은 자체 프로세스 그룹이다
- Job 내의 각 명령은 동일한 PGID를 가져야 한다.
- 새 프로세스를 만드는 두 가지 방법:
- fork
- posix_spawn
- 작업이 완료되면 작업이 삭제됨.
#POSIX Spawn
- Posix spawn은 fork() + exec()을 완전히 대체한다
- Posix spawn의 코드는 if-else 문에서 여러 프로세스를 처리하는 것이 아니라 "linear(선형)" 이다.
- 예: https://man7.org/linux/man-pages/man3/posix_spawn.3.html
#fork() + exec() vs posix_spawn()
#POSIX Spawn Attributes
- Process Groups - posix_spawnattr_getpgroup ()
- Terminal Control - posix_spawnattr_tcsetpgrp_np()
- Piping - posix_spawn_file_actions_adddup2()
#Built-in Commands
- kill - kills a process
- jobs - displays a list of jobs
- stop - stops a process
- fg - sends a process to foreground
- bg - sends a process to background
- exit - exits the shell
#Built-ins Behind the Scenes
- FOUR STEPS for built-in
- 셸이 사용자 input을 기다림.
- 셸이 built-in command를 인식함
- 셸이 built-in 실행(포킹 없음)
- 실행후, 셸이 반복함
#I/O Piping
- 셸은 pipeline에서 각 명령을 실행하기 위해 하위 프로세스를 시작한다.
- 그러나 이것은 command의 Pipeline이기 때문에, 각 프로세스에 대해 STDIN과 STDOUT를 wire해야 한다.
- 프로세스가 이전 프로세스를 대기하고 최종 프로세스는 터미널로 출력됨.
- 프로세스의 STDIN 및 STDOUT을 결합하여 pipeline 생성
#I/O Redirection
- >: 쓰기 전에 원본 파일 내용을 덮어씀.
- >>: 파일의 내용 끝에 추가
- <: STDIN이 아닌 기존 파일에서 입력 읽기
#Signal Handling
- Shell은 Shell에게보내진 신호를 처리할 수 있다.
- SIGINT (Ctrl + C)
- SIGTSTP (Ctrl + Z)
- SIGCHLD (when a child process terminates)
#Handling SIGINT (Ctrl + C)
#Handling SIGTSTP (Ctrl + Z)
#Handling SIGCHLD
#Handling SIGCHLD: WIF* Macros
- wait*를 호출하면 상태를 변경하는 하위 프로세스에 대한 pid 및 status를 return한다. Macros를 사용하여 이 상태를 decode하여 프로세스가 어떤 상태로 변경되었는지, 어떻게 발생했는지 확인할 수 있다:
- WIDEXITED(status) - 하위 프로세스가 정상적으로 종료되었나?
- WIFSIGNALED(status) - 자식 프로세스가 종료되도록 신호를 받았나?
- WIFSTOPED(status) - 하위 프로세스가 중지되도록 신호를 받았나?
'Computer Science > Computer Systems' 카테고리의 다른 글
[Lecture 6] Linking and Loading - Part II (0) | 2022.09.22 |
---|---|
[Lecture 6] Linking and Loading - Part I (0) | 2022.09.22 |
[Lecture 4] Implementing Job Control Shells (0) | 2022.09.16 |
[Lecture 3] Unix Signals (0) | 2022.09.15 |
[Lecture 2] Unix File Descriptors and Pipes (0) | 2022.09.10 |