[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] ๋ฐ˜ํšจ๊ฒฝ ๊ต์ˆ˜๋‹˜, ์ตœ๋ฆฐ ๊ต์ˆ˜๋‹˜์˜ ์šด์˜์ฒด์ œ ๊ฐ•์˜๋ฅผ ์ˆ˜๊ฐ•ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ์ž˜๋ชป๋œ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿ˜Š

profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๐ŸŽฎ

0๊ฐœ์˜ ๋Œ“๊ธ€