[OS] Threads

seunghyunΒ·2023λ…„ 3μ›” 1일
0

πŸ’»

λͺ©λ‘ 보기
2/16

Introduction

thread (ν˜Ήμ€ lightweight process)λŠ” CPU μˆ˜ν–‰λ‹¨μœ„λ‘œ,
program counter, register set, stack space λ₯Ό thread λ§ˆλ‹€ λ³„λ„λ‘œ 가지고 μžˆμ–΄μ•Ό ν•œλ‹€.

κ·Έμ™Έμ˜ λͺ¨λ“  것은 곡유 κ°€λŠ₯ν•˜λ‹€.

thread κ°€ λ™λ£Œ thread 와 κ³΅μœ ν•˜λŠ” λΆ€λΆ„ (=task) λŠ”,
code section, data section, OS resources κ°€ μžˆλ‹€.

전톡적인 κ°œλ…μ˜ heavyweight processλŠ” ν•˜λ‚˜μ˜ threadλ₯Ό 가지고 μžˆλŠ” task둜 λ³Ό 수 μžˆλ‹€.

Single-threaded approach
Traditional approach of a single thread of execution per process has no concept of thread
ex) MS_DOS, old UNIX

Multi-threaded approach
Virtually all modern operating systems provide features enabling a process to contain multiple threads of control.

One process with multiple threads of execution
ex) Java run-time environment

Multiple processes with each of which supports multiple threads
ex) Windows, Solaris, modern UNIX

βœ”οΈ Process vs. Threads
Process κ°€ λ¦¬μ†ŒμŠ€λ₯Ό κ°€μ§€λŠ” μ£Όμ²΄μ΄λ‚˜, CPU 할당인 Scheduling 은 Thread λ³„λ‘œ ν•΄μ£Όκ² λ‹€.

  • The unit of resource ownership is referred to as a process or task.
    • Virtual address space (process image on memory)
    • Protected access to processors, files, and I/O devices
  • In OS that supports threads, the sheduling unit is usually referred to as a thread or lightweight process.

βœ”οΈ Multi Threading
The ability of an OS to support multiple, concurrent paths(threads) of execution within a single process

  • Process is the unit of resource allocation and protection
  • Thread is the unit of dispatching with the following state
    • Thread execution state (Ready, Run)
    • Thread context : register values (PC, stack pointers)
    • Thread execution stack (user stack, kernel stack)

All the threads of a process share the same address space and share the resources of that process

  • when on thread alters the data item in memory, other threads see the results when they access the item.
  • If one thread opens a file with read privileges, other threads can also read from that file.

advantages

βœ”οΈ 닀쀑 μŠ€λ ˆλ“œλ‘œ κ΅¬μ„±λœ νƒœμŠ€ν¬ κ΅¬μ‘°μ—μ„œλŠ” ν•˜λ‚˜μ˜ μ„œλ²„ μŠ€λ ˆλ“œκ°€ blocked(waiting) μƒνƒœμΈ λ™μ•ˆμ—λ„ λ™μΌν•œ νƒœμŠ€ν¬ λ‚΄μ˜ λ‹€λ₯Έ μŠ€λ ˆλ“œκ°€ μ‹€ν–‰(running)λ˜μ–΄ λΉ λ₯Έ 처리λ₯Ό ν•  수 μžˆλ‹€.

βœ”οΈ λ™μΌν•œ 일을 μˆ˜ν–‰ν•˜λŠ” 닀쀑 μŠ€λ ˆλ“œκ°€ ν˜‘λ ₯ν•˜μ—¬ 높은 처리율 (throughput)κ³Ό μ„±λŠ₯ ν–₯상을 얻을 수 μžˆλ‹€.

βœ”οΈ μŠ€λ ˆλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 병렬성을 높일 수 μžˆλ‹€.

βœ”οΈ 응닡성이 λΉ λ₯΄λ‹€

βœ”οΈ μžμ› κ³΅μœ λΌλŠ” νš¨μœ¨μ„±

βœ”οΈ creating & CPU switching thread (rather than a process) λ©΄μ—μ„œ κ²½μ œμ μ΄λ‹€


Multithreading

βœ”οΈ Benefits

  • Faster Response
    • A process can continue running even if part of it is blocked or takes a long time
  • Parallel Processing
    • Even greater in a multicore system since threads can run in parallel on different processor cores
  • Efficient sharing of memory and resources
    • Threads can share code and data since they are in the same address space
    • Threads can communicate through shared-memory rather then kernel-supported signals
    • Threads can also share resources of the process to which they belong
  • Economy
    • Thread creation, switching, termination, and communication cost much less than process counterparts

βœ”οΈ In OS with multithreading, scheduling and execution state is maintained at the thread-level, however some actions affect all the threads in the process.

Thread State

  • Ready, Run, Blocked
    Suspended does not make sense since it is process-level state

User-Level Threads

μ“°λ ˆλ“œκ°€ μ—†λ˜ μ‹œμ ˆ, 라이브러리둜 μ§€μ›λ˜λ„λ‘ ν•˜λŠ” 버전이닀. μŠ€μΌ€μ€„λ§ μŠ€μœ„μΉ­ λ“±λ“± λͺ¨λ‘ 라이브러리둜 μ§€μ›ν•œλ‹€. λΉ λ₯΄κ³  νš¨μœ¨μ μ΄μ§€λ§Œ, 사싀 μ‹±κΈ€ ν”„λ‘œμ„ΈμŠ€ μ‹±κΈ€ CPU λΌμ„œ 병렬 μ²˜λ¦¬κ°€ λΆˆκ°€λŠ₯ν•˜λ‹€. Uμ‹œμŠ€ν…œμ½œμ΄ 생기면 전체 ν”„λ‘œμ„ΈμŠ€κ°€ λΈ”λ‘λœλ‹€. κ·Έλž˜λ„ 컀널 μ§„μž…μ΄ μ—†μ–΄μ„œ 가볍고, μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ— 맞게 μŠ€μΌ€μ€„λ§ μ•Œκ³ λ¦¬μ¦˜μ„ μ΅œμ ν™”ν•΄μ„œ κ°œλ°œν•  수 μžˆλ‹€λŠ” 이런 μž₯단점이 μžˆλ‹€.

Kervel-Level Threads

μœ μ € 레벨의 과도기λ₯Ό κ±°μ³μ„œ OSκ°€ μ“°λ ˆλ“œλ₯Ό 생성해주기 μ‹œμž‘ν–ˆλ‹€. 병렬 μ²˜λ¦¬λ„ κ°€λŠ₯ν•œ λ©€ν‹°μŠ€λ ˆλ“œμ΄λ‹€! 단점이 μžˆλ‹€λ©΄ μŠ€μœ„μΉ­ μ˜€λ²„ν—€λ“œ, 속도가 λ†’μ•„μ§„λ‹€λŠ” 것이닀. (μ˜€λ²„ν—€λ“œκ°€ μœ μ €λ ˆλ²¨μ˜ 10λ°° 이상이닀.)

κ·Έλž˜μ„œ 제일 쒋은 것은? λ‘˜μ˜ μž₯점을 λͺ¨λ‘ μ‚¬μš©ν•˜λŠ” 것이닀!

Combined Approach

Thread creation is done completely in the user space.
Multiple threads within the same process can run in parallel on multiple processors πŸ‘‰ A blocking system call need not block the entire process

λ‹€μŒ 그림은 λ©€ν‹°μŠ€λ ˆλ“œ ν™˜κ²½μ—μ„œ μŠ€λ ˆλ“œ μ‚¬μ΄μ˜ λ©”λͺ¨λ¦¬κ°€ κ³΅μœ λ˜λŠ” 것을 λ‚˜νƒ€λ‚Έλ‹€.


πŸ”— Reference

[KUOCW] 반효경 κ΅μˆ˜λ‹˜, 졜린 κ΅μˆ˜λ‹˜μ˜ 운영체제 κ°•μ˜λ₯Ό μˆ˜κ°•ν•˜κ³  μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€. 잘λͺ»λœ λ‚΄μš©μ΄ μžˆλ‹€λ©΄ λŒ“κΈ€λ‘œ μ•Œλ €μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€ 😊

0개의 λŒ“κΈ€