Skip to content

Commit

Permalink
feat: [Linux] Signal์ •๋ฆฌ(1) (#1)
Browse files Browse the repository at this point in the history
* feat: [Linux] Signal์ •๋ฆฌ(1)

* fix : review ๋ณด์™„
  • Loading branch information
JjungminLee authored Jan 2, 2025
1 parent a3afd15 commit 0a9708f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Empty file removed Linux/.gitkeep
Empty file.
125 changes: 125 additions & 0 deletions Linux/Signal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
## Signal

### โœ…ย ๊ฐœ์š”

- ํ”„๋กœ์„ธ์Šค ๋˜๋Š” ์‹œ์Šคํ…œ์—๊ฒŒ ์ด๋ฒคํŠธ ๋ฐœ์ƒ์„ ์ „๋‹ฌํ•˜๋Š” ์‹ ํ˜ธ๋ฅผ ๋งํ•จ.
- ๋งŽ์€ ๋ถ€๋ถ„์ด ๋น„๋™๊ธฐ์ด์ง€๋งŒ ์ผ๋ถ€๋Š” ๋™๊ธฐ์ ์ž„.
- ๋™๊ธฐ : ์ด๋ฒคํŠธ๊ฐ€ ์žˆ์–ด์•ผ ๋ฐœ์ƒ, ๋น„๋™๊ธฐ : ์˜ˆ์ธก๋ถˆ๊ฐ€
- ์˜ˆ) ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ„๋ฅด๋Š”๊ฒƒ (ctrl+c์™€ ๊ฐ™์ด)
- divide zero๋Š” ์ธํ„ฐ๋ŸฝํŠธ์ผ์ˆ˜๋„ ์žˆ๊ณ  ์‹œ๊ทธ๋„์ผ์ˆ˜๋„ ์žˆ์Œ.

### ๐Ÿคฟ ์ •๋ฆฌ

์‹œ๊ทธ๋„์˜ ๋ฐœ์ƒ์€ ๋‘๊ฐ€์ง€๋กœ ๋‚˜๋‰จ. -> ๋น„๋™๊ธฐ(์˜ˆ์ธก๋ถˆ๊ฐ€), ๋™๊ธฐ(์˜ˆ์ธก๊ฐ€๋Šฅ)
์‹œ๊ทธ๋„์˜ ์ฒ˜๋ฆฌ๋Š” ์˜ค๋กœ์ง€ ๋น„๋™๊ธฐ์ž„.

### ๐Ÿ˜ซย ์™œ ์“ธ๊นŒ?

- ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Œ์„ ์•Œ๋ ค์ฃผ๋Š” ์•Œ๋ฆผ๋ฉ”์„ธ์ง€์˜ ๊ธฐ๋Šฅ์„ ํ•˜๊ธฐ ์œ„ํ•ด ์“ด๋‹ค.
- ์˜ˆ๋ฅผ ๋“ค์–ด ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ๋Œ๊ณ  ์žˆ๋Š”๋ฐ ์ด๊ฑธ ๊บผ์•ผํ•œ๋‹ค. โ†’ ๊ทธ๋•Œ sigint๋ฅผ ๋ณด๋‚ด์„œ ๊ฐ•์ œ ์ข…๋ฃŒ๋ฅผ ์‹œ์ผœ๋ฒ„๋ฆฐ๋‹ค.
- ํŠน์ • ํ”„๋กœ์„ธ์Šค๋ฅผ ๊ฐ•์ œ์ข…๋ฃŒ ์‹œํ‚ค๊ณ  ์‹ถ๋‹ค โ†’ ํ„ฐ๋ฏธ๋„์— sigkill -9 {ํ”„๋กœ์„ธ์Šค ์ด๋ฆ„}์„ ๋ช…๋ น์–ด๋กœ ์ž…๋ ฅํ•ด ๊ฐ•์ œ ์ข…๋ฃŒ ์‹œ์ผœ๋ฒ„๋ฆฐ๋‹ค.

### ๐Ÿ“ย Signal์ด ๋™์ ์ธ ์ƒํ™ฉ์€?

- ํ”„๋กœ์„ธ์Šค๊ฐ€ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ์‹œ๊ทธ๋„์„ ๋ณด๋‚ด๊ฑฐ๋‚˜ ์‚ฌ์šฉ์ž๊ฐ€ ์‹œ๊ทธ๋„์„ ๋ณด๋‚ด๋Š” ๊ฒฝ์šฐ.
- ์ด๋ฒคํŠธ๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์‹œ๊ทธ๋„์„ ์ „๋‹ฌ๋ฐ›์€ ํ”„๋กœ๊ทธ๋žจ์€ ์‹œ๊ทธ๋„์„ `์‹œ๊ทธ๋„ ํ•ธ๋“ค๋Ÿฌ`๋ฅผ ํ†ตํ•ด ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ.
โœ… ์ค‘๋‹จ๋œ ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ๋Š” ๋ˆ„๊ฐ€, ์–ด๋–ป๊ฒŒ ๊ด€๋ฆฌํ• ๊นŒ?
- ์ปค๋„์ด ๊ด€๋ฆฌํ•จ. ํ”„๋กœ๊ทธ๋žจ ์นด์šดํ„ฐ, ๋ ˆ์ง€์Šคํ„ฐ ๊ฐ’, ์Šคํƒ ํฌ์ธํ„ฐ ๊ฐ’์„ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋‹ค๊ฐ€ ๋‚˜์ค‘์— ์‹œ๊ทธ๋„ ๋ฐœ์ƒ์œผ๋กœ ์ธํ•ด ์ค‘๋‹จ๋œ ์ง€์ ์—์„œ ์žฌ๊ฐœ

### ๐Ÿ“ย ๋ฆฌ๋ˆ…์Šค์˜ ์ฃผ์š” ์‹œ๊ทธ๋„

| ์ด๋ฆ„ | ์„ค๋ช… |
| ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| SIGALRM | alram()์œผ๋กœ ์„ค์ •ํ•œ ํƒ€์ด๋จธ๊ฐ€ ๋งŒ๋ฃŒ๋˜์—ˆ์„๋•Œ ๋ฐœ์ƒ. |
| SIGBUS | ํ•˜๋“œ์›จ์–ด ๊ณ ์žฅ์— ํ•ด๋‹นํ•˜๋Š” ์‹œ๊ทธ๋„. |
| SIGCHILD | ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ ๋˜๋Š” ์ค‘๋‹จ๋˜๋ฉด ๋ถ€๋ชจ์—๊ฒŒ ์ด ์‹œ๊ทธ๋„ ์ „๋‹ฌ. ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌด์‹œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ž์‹์˜ ์ƒํƒœ๊ฐ€ ๋ณ€ํ•  ๋•Œ ๋งˆ๋‹ค ๊ทธ ์‚ฌ์‹ค์„ ์•Œ์•„์•ผ ํ•œ๋‹ค๋ฉด ๋ถ€๋ชจ๋Š” ๋ฐ˜๋“œ์‹œ ์ด ์‹œ๊ทธ๋„์„ ์บ์น˜ํ•ด์•ผํ•จ. |
| SIGINT | ์‚ฌ์šฉ์ž๊ฐ€ DELETE ๋˜๋Š” Ctrl+C๋ฅผ ๋ˆŒ๋ €์„๋•Œ ํ„ฐ๋ฏธ๋„ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ด ์‹œ๊ทธ๋„ ๋ฐœ์ƒ. |
| SIGKILL | ๋ฌด์‹œํ•˜๊ฑฐ๋‚˜ ์บ์น˜ ๋ถˆ๊ฐ€. sigkill -9 {ํ”„๋กœ์„ธ์Šค ๋ฒˆํ˜ธ} ์ด๋ฉด ๊ฐ•์ ์กฐํ•ฉ. |
| SIGSTOP | ๋ฌด์‹œํ•˜๊ฑฐ๋‚˜ ์บ์น˜ํ•  ์ˆ˜ ์—†๋Š” ๋‘ ์‹œ๊ทธ๋„ ์ค‘ ํ•˜๋‚˜๋กœ ํ”„๋กœ์„ธ์Šค ์ค‘๋‹จ |
| SIGTSTP | ์‚ฌ์šฉ์ž๊ฐ€ ํ„ฐ๋ฏธ๋„์—์„œ Ctrl+Z(์ผ์‹œ์ •์ง€ ํ‚ค) ๋ฅผ ๋ˆŒ๋ €์„๋•Œ ํ„ฐ๋ฏธ๋„ ๋“œ๋ผ์ด๋ฒ„๊ฐ€ ์ด ์‹œ๊ทธ๋„ ๋ฐœ์ƒ |
| SIGUSR1, SIGUSR2 | ์‚ฌ์šฉ์ž ์ •์˜ ์‹œ๊ทธ๋„ |

### ๐Ÿ“ย ์ปค๋„์˜ ์‹œ๊ทธ๋„ ์ฒ˜๋ฆฌ ๋ฐฉ์‹

1. ์‹œ๊ทธ๋„ ๋ฌด์‹œ
1. ์–ด๋–ค ์•ก์…˜๋„ ์ทจํ•˜์ง€ ์•Š๋Š”๋‹ค.
2. ๋””ํดํŠธ ์•ก์…˜ ์‹คํ–‰
1. ์‚ฌ์šฉ์ž ์ง€์ • ํ•ธ๋“ค๋Ÿฌ
2. ๋Œ€๋ถ€๋ถ„ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ
3. ์‹œ๊ทธ๋„ ์บ์น˜ํ•˜์—ฌ ์‹คํ–‰
1. ํ•จ์ˆ˜๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.
2. ์ปค๋„์€ ํ”„๋กœ์„ธ์Šค์˜ ํ˜„์žฌ ์ฝ”๋“œ์˜ ์‹คํ–‰์„ ์ค‘๋‹จํ•˜๊ณ  ์ด์ „์— ๋“ฑ๋กํ–ˆ๋˜ ์‹œ๊ทธ๋„ ์ฒ˜๋ฆฌํ•จ์ˆ˜๋กœ ๋„˜์–ด๊ฐ€์„œ ๊ทธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰.
3. ์‹œ๊ทธ๋„ ์ฒ˜๋ฆฌํ•จ์ˆ˜๊ฐ€ ๋ฆฌํ„ด๋˜๋ฉด ์‹œ๊ทธ๋„์ด ๋ฐœ์ƒํ•˜๊ธฐ ์ „ ์œ„์น˜๋กœ ๋Œ์•„๊ฐ

### ๐Ÿ“ย Signal(2)

```cpp
#include <signal.h>
sighandler_t signal(int signum , sighandler_t handler);
```
- signum ์ž๋ฆฌ์—๋Š” ์‹œ๊ทธ๋„์˜ ์ด๋ฆ„์„ ์“ด๋‹ค
- handler ์ธ์ž์—๋Š” ์‹œ๊ทธ๋„ ๋ฐœ์ƒ์‹œ ํ˜ธ์ถœ๋  ํ•จ์ˆ˜์˜ ์ฃผ์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค.
### ๐Ÿ“ย task_struct
- ์•„๋ž˜๋Š” ๋ฆฌ๋ˆ…์Šค์˜ task_struct์˜ ์˜ˆ์‹œ.
- ํ”„๋กœ์„ธ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.
![image](https://github.com/user-attachments/assets/74115877-5b12-4cdf-8fcb-ec5878a6e0b8)
- pending์€ bit๋ณ„๋กœ ์‹œ๊ทธ๋„์„ ๊ตฌ๋ถ„ํ•œ๋‹ค.
- handler๋Š” ์‹œ๊ทธ๋„์˜ ๋ถˆ๋ ค์ง€๋ฉด ๋””ํดํŠธ๋กœ ํ˜ธ์ถœ๋œ๋‹ค.
- blocked๋Š” masked์™€ ๊ฐ™์€ ์˜๋ฏธ๋กœ ํ”„๋กœ์„ธ์Šค์™€ ๊ด€๋ จ๋œ block์€ ์ „๋ถ€ mask์‹œํ‚จ๋‹ค.
### โœ…ย PCB์™€ task_struct์˜ ๊ด€๊ณ„
- ์šด์˜์ฒด์ œ๋ฅผ ๋ฐฐ์šฐ๋ฉด `PCB`๋ผ๋Š” ๋ง์„ ์ตํžˆ ๋“ฃ๋Š”๋‹ค.
- Process Control Block์˜ ์•ฝ์ž๋กœ, ํ”„๋กœ์„ธ๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋ฉฐ ์ปค๋„์•ˆ์— ์กด์žฌํ•œ๋‹ค.
![image](https://github.com/user-attachments/assets/4996300d-933a-4868-96ca-11253f9d49ac)
[๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์ง์ ‘ ์ฐพ์•„๊ฐ€๋ณด๋„๋ก ํ•˜์ž](https://github.com/torvalds/linux/blob/master/include/linux/sched.h)
- ๋ฆฌ๋ˆ…์Šค์—์„œ PCB๋ฅผ task_struct๋ผ๋Š” ์นœ๊ตฌ๊ฐ€ ๋Œ€์ฒดํ•˜๊ณ  ์žˆ๋Š”๊ฒƒ์ด๋‹ค
![image](https://github.com/user-attachments/assets/d6a9190d-1638-42be-8dad-a352421f2cd2)
- task_struct์•ˆ์— ์‹œ๊ทธ๋„ ํ•ธ๋“ค๋Ÿฌ๋„ ๋“ค์–ด๊ฐ„๋‹ค!
### ๐Ÿงฉย ์ธํ„ฐ๋ŸฝํŠธ์™€ ์‹œ๊ทธ๋„์˜ ์ฐจ์ด?
- ์ด์ฏค์ด๋ฉด ๊ถ๊ธˆํ•ด์งˆ๊ฒƒ์ด๋‹ค. ์ธํ„ฐ๋ŸฝํŠธ์™€ ์‹œ๊ทธ๋„์˜ ์ฐจ์ด๊ฐ€ ๋ฌด์—‡์ธ์ง€!
- ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ์ธํ„ฐ๋ŸฝํŠธ๋Š” ๋ฐ”๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์‹œ๊ทธ๋„์€ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด๊ธฐ์— ๋ฐ”๋กœ ์‹คํ–‰๋˜์ง€ ๋ชปํ•œ๋‹ค!
- ํ›„์ˆ ํ•˜๊ฒ ์ง€๋งŒ ์‹œ๊ทธ๋„์€ Signal์€ pending์ด ๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ด์ผํ•˜๋Š๋ผ ์•ˆ๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค.
**์ธํ„ฐ๋ŸฝํŠธ**
- ์ธํ„ฐ๋ŸฝํŠธ ํฌ๊ฒŒ ํ•˜๋“œ์›จ์–ด ์ธํ„ฐ๋ŸฝํŠธ์™€ ์†Œํ”„ํŠธ์›จ์–ด ์ธํ„ฐ๋ŸฝํŠธ๋กœ ๋‚˜๋‰œ๋‹ค.
- ํ•˜๋“œ์›จ์–ด ์ธํ„ฐ๋ŸฝํŠธ
- ์™ธ๋ถ€ ๋””๋ฐ”์ด์Šค์—์„œ ๋ฐœ์ƒ
- CPU๊ฐ€ ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์ž‘์—… ๋ฉˆ์ถ”๊ณ  โ†’ ์ธํ„ฐ๋ŸฝํŠธ ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰
- ์†Œํ”„ํŠธ์›จ์–ด ์ธํ„ฐ๋ŸฝํŠธ
- ๋™๊ธฐ์  : ์‹œ์Šคํ…œ์ฝœ
- ๋น„๋™๊ธฐ์  : trap
- divide in zero
- page fault
**์‹œ๊ทธ๋„**
- os๋‚˜ ํ”„๋กœ์„ธ์Šค๊ฐ€ ํŠน์ • ํ”„๋กœ์„ธ์Šค์—๊ฒŒ ๋ณด๋‚ด๋Š” ๋น„๋™๊ธฐ์  ์ด๋ฒคํŠธ
- ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‹คํ–‰์ค‘์ผ ๋•Œ๋งŒ ์ฒ˜๋ฆฌ
**๐ŸคRecap**
์ธํ„ฐ๋ŸฝํŠธ๋Š” CPU์™€ ์ปค๋„ ์ค‘์‹ฌ, ์‹œ๊ทธ๋„์€ ์‹คํ–‰์ค‘์ธ ํ”„๋กœ์„ธ์Šค ์ค‘์‹ฌ
</aside>
### ๐Ÿ“ย kill(2)
```cpp
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
```

- pid์—๊ฒŒ signal์„ ๋ณด๋‚ธ๋‹ค
- kill()์€ ํ”„๋กœ์„ธ์Šค ๊ทธ๋ฃน์— ์‹œ๊ทธ๋„ ๋ณด๋‚ด๋Š” ์‹œ์Šคํ…œ์ฝœ ํ•จ์ˆ˜
- % kill -9 1093 โ‡’ 1093๋ฒˆ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ SIGKILL์„ ๋ณด๋‚ธ๋‹ค
- % kill 1093 โ‡’ 1093๋ฒˆ ํ”„๋กœ์„ธ์Šค์—๊ฒŒ SIGTERM์„ ๋ณด๋‚ธ๋‹ค (๋ฌด์‹œ,catch๊ฐ€๋Šฅ)

0 comments on commit 0a9708f

Please sign in to comment.