Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add os_concepts system_call.md #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"editor.formatOnPaste": true
},
"editor.codeActionsOnSave": {
"source.fixAll.markdownlint": true
"source.fixAll.markdownlint": "explicit"
}
}
127 changes: 127 additions & 0 deletions study/os_concepts/1.system_call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@

# πŸ“– [System Call]

## 1. **κ°œλ… μš”μ•½**

System Callμ΄λž€ μ‚¬μš©μžκ°€ μ»€λ„μ˜μ—­μ—μ„œ ν•„μš”ν•œ μˆ˜ν–‰μ„ ν•˜κΈ° μœ„ν•΄ μœ μ €μ˜μ—­μ—μ„œ μ‚¬μš©ν•˜λŠ” ν”„λ‘œκ·Έλž˜λ° μΈν„°νŽ˜μ΄μŠ€μ΄λ‹€.

---

## 2. **μ„ΈλΆ€ μ„€λͺ…**

System Call
- μ‚¬μš©μžκ°€ 직접 μ‚¬μš©ν•˜μ§€ λͺ»ν•˜λŠ” 컀널 κΈ°λŠ₯듀을 μ‚¬μš©ν•  수 μžˆλ‹€.
- System Call을 ν˜ΈμΆœν•˜λŠ” 곳은 User Space, μˆ˜ν–‰λ˜λŠ” 곳은 Kernel Space 이닀.
- ν˜ΈμΆœν•˜λŠ” κ³³κ³Ό μˆ˜ν–‰λ˜λŠ” 곳의 μ˜μ—­μ΄ λ‹€λ₯΄κΈ° λ•Œλ¬Έμ—, μ ‘κ·Όν•  수 μžˆλŠ” μ£Όμ†Œμ˜ μ˜μ—­ λ˜ν•œ μ œν•œλ˜μ–΄ μžˆλ‹€.
a. System Call을 ν˜ΈμΆœν•˜λŠ” κ³³ -> User Space Memory
b. System Call이 μˆ˜ν–‰λ˜λŠ” κ³³ -> User, Kernel Space Memory


System call이 μ‹€μ œ λ¦¬λˆ…μŠ€μ—μ„œ μˆ˜ν–‰λ˜λŠ” 과정은 λ‹€μŒκ³Ό κ°™λ‹€.

1. User taskμ—μ„œμ˜ System Call ν•¨μˆ˜ 호좜
2. IDT(Interrupt Descriptor Table)의 0x80 offset의 system call κ΄€λ ¨ Interrupt λ°œμƒ (Trap Instruction)
3. Trap을 μ²˜λ¦¬ν•˜κΈ° μœ„ν•΄ handlerλŠ” μˆ˜ν–‰ν•˜κ³ μž ν•˜λŠ” system call 번호λ₯Ό sys_call_tableμ—μ„œ 찾은 λ’€ Kernel μ˜μ—­μ˜ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•œλ‹€.
4. ν•¨μˆ˜ 결과값을 User Process둜 return ν•œλ‹€.

---

## 3. **κ΄€λ ¨ μ½”λ“œ**

[이 κ°œλ…κ³Ό κ΄€λ ¨λœ μ½”λ“œ μœ„μΉ˜λ‚˜ κ΅¬ν˜„ 방법을 μ„€λͺ…ν•©λ‹ˆλ‹€.]

- 파일 μœ„μΉ˜: `src/threads/[파일λͺ…]`
- μ£Όμš” ν•¨μˆ˜:
- `function_name()`: [ν•¨μˆ˜μ˜ μ—­ν•  및 κ°„λ‹¨ν•œ μ„€λͺ…]
- `function_name_2()`: [ν•¨μˆ˜μ˜ μ—­ν•  및 κ°„λ‹¨ν•œ μ„€λͺ…]

- 파일 μœ„μΉ˜: `src/lib/syscall_nr.h`
- system call number λ₯Ό μ •μ˜ν•œ κ³³

- 파일 μœ„μΉ˜: `src/lib/user/syscall.c`
- `define syscall#(NUMBER, ...)`: μ–΄μ…ˆλΈ”λ¦¬μ™€ Cμ–Έμ–΄λ‘œ μž‘μ„± 된 system call을 μˆ˜ν–‰ν•˜λŠ” 맀크둜
- `void halt(void){syscall0(...)}, void exit(int status){syscall1(...)}`: 인자 κ°œμˆ˜μ™€ μš©λ„μ— 맞게 맀크둜 syscall#을 ν™œμš©ν•˜μ—¬ system call을 μˆ˜ν–‰ν•˜λ„λ‘ μ •μ˜ν•΄λ†“μ€ ν•¨μˆ˜

- 파일 μœ„μΉ˜: `src/userprog/syscall.c`
- μ£Όμš” ν•¨μˆ˜:
- `syscall_init`


예:

```c
/* threads/thread.c */
/* μŠ€λ ˆλ“œλ₯Ό μ΄ˆκΈ°ν™”ν•˜λŠ” ν•¨μˆ˜ */
void initialize_thread(struct thread *t, const char *name, int priority) {
ASSERT(t != NULL);
ASSERT(name != NULL);
t->priority = priority;
/* Additional initialization code here */
}
```

---

## 4. **Pintosμ—μ„œμ˜ μ—­ν• **

[ν•΄λ‹Ή κ°œλ…μ΄ Pintosμ—μ„œ μ–΄λ–»κ²Œ μ‚¬μš©λ˜κ³ , κ΅¬ν˜„ μ‹œ μ–΄λ–€ λΆ€λΆ„μ—μ„œ μ€‘μš”ν•œ 역할을 ν•˜λŠ”μ§€ μž‘μ„±ν•©λ‹ˆλ‹€.]

예:

- **μŠ€λ ˆλ“œ 관리**:
- μŠ€λ ˆλ“œλŠ” Pintosμ—μ„œ κΈ°λ³Έ μ‹€ν–‰ λ‹¨μœ„λ‘œ, CPU μŠ€μΌ€μ€„λ§ 및 λ™κΈ°ν™”μ˜ μ£Όμš” λŒ€μƒμž…λ‹ˆλ‹€.
- μŠ€λ ˆλ“œμ˜ μƒνƒœ(ready, running, blocked)λŠ” CPU μŠ€μΌ€μ€„λ§μ— 영ν–₯을 μ€λ‹ˆλ‹€.

---

## 5. **이해λ₯Ό λ•λŠ” 예제**

[κ°œλ…μ„ μ΄ν•΄ν•˜λŠ” 데 도움이 λ˜λŠ” κ°„λ‹¨ν•œ μ½”λ“œ μ˜ˆμ œλ‚˜ μ‹œλ‚˜λ¦¬μ˜€λ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.]

예:

```c
/* μŠ€λ ˆλ“œ 생성과 μ‹€ν–‰ */
struct thread *t = thread_create("worker", PRI_DEFAULT, worker_function, NULL);
/* μƒμ„±λœ μŠ€λ ˆλ“œ μ‹€ν–‰ */
thread_unblock(t);
```

---

## 6. **μ£Όμš” κ΅¬ν˜„ 단계**

[이 κ°œλ…μ„ κ΅¬ν˜„ν•˜κΈ° μœ„ν•΄ ν•„μš”ν•œ 단계 및 μ ‘κ·Ό 방식을 λ‚˜μ—΄ν•©λ‹ˆλ‹€.]

1. [단계 1: μ„€λͺ…]
2. [단계 2: μ„€λͺ…]
3. [단계 3: μ„€λͺ…]

---

## 7. **κ΄€λ ¨ κ°œλ…**

[이 κ°œλ…κ³Ό κ΄€λ ¨λœ λ‹€λ₯Έ κ°œλ…μ΄λ‚˜ κ΅¬ν˜„ μš”μ†Œλ₯Ό λ§ν¬ν•˜κ±°λ‚˜ κ°„λ‹¨νžˆ μ„€λͺ…ν•©λ‹ˆλ‹€.]

- [κ΄€λ ¨ κ°œλ… 1]
- [κ΄€λ ¨ κ°œλ… 2]

예:

- **μŠ€μΌ€μ€„λ§(Scheduling)**: μŠ€λ ˆλ“œμ˜ μ‹€ν–‰ μˆœμ„œλ₯Ό κ²°μ •ν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜. κΈ°λ³Έ μ•Œκ³ λ¦¬μ¦˜μ€ `priority scheduling`μž…λ‹ˆλ‹€.

---

## 8. **참고 자료**

λ³΄κ³ μ„œ 참고자료
- [ν•€ν† μŠ€_ν•œμ–‘λŒ€.pdf]
- [Trap] (https://autumnrain.tistory.com/entry/Kernel-Trap-%ED%8A%B8%EB%9E%A9)


[νŒ€μ›λ“€μ΄ 더 깊이 이해할 수 μžˆλ„λ‘ μ°Έκ³ ν•  자료(λ¬Έμ„œ, 링크 λ“±)λ₯Ό μ œκ³΅ν•˜μ„Έμš”.]

- [Pintos 곡식 λ¬Έμ„œ](http://web.stanford.edu/class/cs140/projects/pintos/pintos_1.html)
- [Operating Systems: Three Easy Pieces](https://pages.cs.wisc.edu/~remzi/OSTEP/)

---