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
βοΈ λ€μ€ μ€λ λλ‘ κ΅¬μ±λ νμ€ν¬ ꡬ쑰μμλ νλμ μλ² μ€λ λκ° blocked(waiting) μνμΈ λμμλ λμΌν νμ€ν¬ λ΄μ λ€λ₯Έ μ€λ λκ° μ€ν(running)λμ΄ λΉ λ₯Έ μ²λ¦¬λ₯Ό ν μ μλ€.
βοΈ λμΌν μΌμ μννλ λ€μ€ μ€λ λκ° νλ ₯νμ¬ λμ μ²λ¦¬μ¨ (throughput)κ³Ό μ±λ₯ ν₯μμ μ»μ μ μλ€.
βοΈ μ€λ λλ₯Ό μ¬μ©νλ©΄ λ³λ ¬μ±μ λμΌ μ μλ€.
βοΈ μλ΅μ±μ΄ λΉ λ₯΄λ€
βοΈ μμ 곡μ λΌλ ν¨μ¨μ±
βοΈ creating & CPU switching thread (rather than a process) λ©΄μμ κ²½μ μ μ΄λ€
βοΈ Benefits
βοΈ In OS with multithreading, scheduling and execution state is maintained at the thread-level, however some actions affect all the threads in the process.
Ready
, Run
, Blocked
Suspended
does not make sense since it is process-level stateμ°λ λκ° μλ μμ , λΌμ΄λΈλ¬λ¦¬λ‘ μ§μλλλ‘ νλ λ²μ μ΄λ€. μ€μΌμ€λ§ μ€μμΉ λ±λ± λͺ¨λ λΌμ΄λΈλ¬λ¦¬λ‘ μ§μνλ€. λΉ λ₯΄κ³ ν¨μ¨μ μ΄μ§λ§, μ¬μ€ μ±κΈ νλ‘μΈμ€ μ±κΈ CPU λΌμ λ³λ ¬ μ²λ¦¬κ° λΆκ°λ₯νλ€. Uμμ€ν μ½μ΄ μκΈ°λ©΄ μ 체 νλ‘μΈμ€κ° λΈλ‘λλ€. κ·Έλλ 컀λ μ§μ μ΄ μμ΄μ κ°λ³κ³ , μ ν리μΌμ΄μ μ λ§κ² μ€μΌμ€λ§ μκ³ λ¦¬μ¦μ μ΅μ νν΄μ κ°λ°ν μ μλ€λ μ΄λ° μ₯λ¨μ μ΄ μλ€.
μ μ λ 벨μ κ³ΌλκΈ°λ₯Ό κ±°μ³μ OSκ° μ°λ λλ₯Ό μμ±ν΄μ£ΌκΈ° μμνλ€. λ³λ ¬ μ²λ¦¬λ κ°λ₯ν λ©ν°μ€λ λμ΄λ€! λ¨μ μ΄ μλ€λ©΄ μ€μμΉ μ€λ²ν€λ, μλκ° λμμ§λ€λ κ²μ΄λ€. (μ€λ²ν€λκ° μ μ λ 벨μ 10λ°° μ΄μμ΄λ€.)
κ·Έλμ μ μΌ μ’μ κ²μ? λμ μ₯μ μ λͺ¨λ μ¬μ©νλ κ²μ΄λ€!
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
λ€μ κ·Έλ¦Όμ λ©ν°μ€λ λ νκ²½μμ μ€λ λ μ¬μ΄μ λ©λͺ¨λ¦¬κ° 곡μ λλ κ²μ λνλΈλ€.
[KUOCW] λ°ν¨κ²½ κ΅μλ, μ΅λ¦° κ΅μλμ μ΄μ체μ κ°μλ₯Ό μκ°νκ³ μ 리ν λ΄μ©μ λλ€. μλͺ»λ λ΄μ©μ΄ μλ€λ©΄ λκΈλ‘ μλ €μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€ π