๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš— Major Study (Bachelor)/๐ŸŸง Operating System

[OS / ์šด์˜์ฒด์ œ] TCB, Multicore Programming, Concurrency vs Parallelism, Amdahl's Law, Kernel Thread, Scheduler Activation

by UKHYUN22 2022. 4. 4.
728x90

Motivation

 

๋‘ ๊ฐœ์˜ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค๊ณ  ์‹ถ์„ ๋•Œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ์ •์„ฑ์ด ์ข‹์•„์ง„๋‹ค. ํ•˜๋‚˜์˜ ํ”„๋กœ์„ธ์Šค ์•ˆ์—์„œ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋‘ ๊ฐœ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ ํ•˜๋‚˜์˜ ์“ฐ๋ ˆ๋“œ๊ฐ€ ์ฃฝ์œผ๋ฉด ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ฃฝ์–ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ์ •์„ฑ ์ธก๋ฉด์—์„œ ์ข‹์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ ์“ฐ๋ ˆ๋“œ๋Š” ๋” ๊ฒฝ์ œ์ ์ด๊ณ  ์ •๋ณด๋ฅผ ๊ณต์œ ํ•  ๋•Œ ํŽธ๋ฆฌํ•˜๋‹ค. ํ”„๋กœ์„ธ์Šค๋Š” ์›๋ž˜ ๋…๋ฆฝ์ ์ธ Entity์ด๋ฏ€๋กœ ์‰ฝ์ง€ ์•Š๋‹ค. ์“ฐ๋ ˆ๋“œ๋ผ๋ฆฌ ํ†ต์‹ ์„ ํ•  ๋•Œ Shared Memory๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค. ๊ทธ์ € ์ „์—ญ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค. 

 

 

Overview

ํ”„๋กœ์„ธ์Šค๋Š” ์‹คํ–‰ ์ค‘์ธ ํ”„๋กœ๊ทธ๋žจ์„ ์˜๋ฏธํ•˜๊ณ  ๋…๋ฆฝ์ ์ธ ์ž์›์ด๋‹ค. ๋ฐ˜๋ฉด ์“ฐ๋ ˆ๋“œ๋Š” ํ”„๋กœ์„ธ์Šค ์•ˆ์˜ ํ”„๋กœ๊ทธ๋žจ์ด์—ฌ์„œ ์ž์›์„ ๊ณต์œ ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค. Thread ID์™€ PC, register set, stack๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. PC๋Š” ์“ฐ๋ ˆ๋“œ ๋งˆ๋‹ค ํ•˜๋‚˜์”ฉ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. Stack์€ ๋กœ์ปฌ ๋ณ€์ˆ˜๊ฐ€ ์ €์žฅ๋˜๋Š” ๊ณณ์ด๊ณ  Function call์„ ํ•  ๋•Œ ์ฃผ์†Œ๊ฐ€ ์ €์žฅ๋œ๋‹ค. 

 

 

Multithreaded Process

code data files๋Š” ์ „์—ญ์œผ๋กœ ๊ณต์œ ํ•˜๋Š” ๋ฐ์ดํ„ฐ์ด๊ณ  ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•˜๋‹ค. register์™€ stack์€ ์“ฐ๋ ˆ๋“œ๋ณ„๋กœ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.

 

TCB

Scheduling์˜ ๊ธฐ๋ณธ ๋‹จ์œ„๊ฐ€ Thread์ธ ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. Pointer to PCB, ํ”„๋กœ์„ธ์Šค์™€ ์“ฐ๋ ˆ๋“œ๋Š” Parent Child ๊ด€๊ณ„๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. ๋‚ด๊ฐ€ ์†ํ•œ ํ”„๋กœ์„ธ์Šค์˜ PCB์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๋„ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค. TCB ์†์—์„œ PCB์˜ Linked List๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” Header Pointer๊ฐ€ ์กด์žฌํ•œ๋‹ค. 

 

Why Thread

request๊ฐ€ ๋“ค์–ด์˜ค๋ฉด Thread๋ฅผ ๋งŒ๋“ค์–ด์„œ Client์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๊ฑฐ๋‚˜ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์“ฐ๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฒฝ์ œ์ ์ด์ง€๋งŒ ์•ˆ์ •์„ฑ์ด ๋–จ์–ด์ง„๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

 

 

Why Thread

๋ฉ€ํ‹ฐ ํ”„๋กœ์„ธ์„œ์˜ ํŒŒ์›Œ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋” ๋งŽ์€ ์ผ์„ ํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์œ ์ €์˜ Interface๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๊ณ  ๊ฐ„๋‹จํ•œ ์ž‘์—…์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์„ Thread๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์ž์›์„ ๊ณต์œ ํ•˜๊ธฐ๊ฐ€ ์‰ฝ๋‹ค. ์“ฐ๋ ˆ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ํ”„๋กœ์„ธ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค Overhead๊ฐ€ ๋œ ๋ฐœ์ƒํ•œ๋‹ค.

 

Multicore Programming

activity๋ฅผ ๋‚˜๋ˆ ์•ผ ํ”„๋กœ์„ธ์Šค๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. Data๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ถ„๋ฆฌ๋ฅผ ์‹œ์ผœ๋†”์•ผ ํ•œ๋‹ค. Data dependency, ๋…๋ฆฝ์ ์ธ ๋ฐ์ดํ„ฐ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ๋””๋ฒ„๊ทธํ•˜๋Š” ๊ฒƒ์ด ์–ด๋ ต๋‹ค. 

 

Concurrency vs Parallelism

์Šค์ผ€์ค„๋Ÿฌ์— ์˜ํ•ด์„œ ์ž‘์—…์„ ํ•  ๋•Œ Cocurrency๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. Parallelism CPU๊ฐ€ ๋™์‹œ์— ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. 

 

 

Multicore Programming

data P - ๋™์ผํ•œ ์ผ์„ ํ•˜์ง€๋งŒ ํ”„๋กœ์„ธ์Šค๋“ค์ด ๋งก์€ ์˜์—ญ์ด ๋‹ค๋ฅธ ๊ฒƒ. ๊ต‰์žฅํžˆ ๊ธด ๋ฐฐ์—ด์„ ๋”ํ•˜๋Š”๋ฐ ํ•˜๋‚˜๋Š” ์ง์ˆ˜ ํ•˜๋‚˜๋Š” ํ™€์ˆ˜๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜์…จ๋‹ค. ๋”ํ•˜๋Š” ์ž‘์—…์€ ๋™์ผํ•˜์ง€๋งŒ ํ•˜๋‚˜๋Š” ์ง์ˆ˜ ํ•˜๋‚˜๋Š” ํ™€์ˆ˜๋กœ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์–ด๋–ป๊ฒŒ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€. Thread ๋ณ„๋กœ์˜ Index๋ฅผ ๋‹ค๋ฅด๊ฒŒ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. Thread์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด architecture support๋ฅผ ๋Š˜์–ด๋‚˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์“ฐ๋ ˆ๋“œ๋ฅผ CPU๊ฐ€ ๋™์‹œ์— ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์“ฐ๋ ˆ๋“œ๋ฅผ ํ•˜๋“œ์›จ์–ด ์“ฐ๋ ˆ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. 

 

 

Amdahl's Law

๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ ํผํฌ๋จผ์Šค๊ฐ€ ๋ช‡ ๋ฐฐ ๋นจ๋ฆฌ์งˆ ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋ฅผ ๋ถ„์„ํ•œ ๋ฒ•์น™์ด๋‹ค. ์†๋„์— ๊ด€ํ•œ ์‹์ด๋ฏ€๋กœ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์„ ๊ตฌํ•ด์„œ ๋ถ„๋ชจ๋กœ ๋ณด๋‚ด์„œ ์—ญ์ˆ˜๋ฅผ ์ทจํ•ด์•ผ ํ•œ๋‹ค. 

 

 

Types of Threads

User thread์™€ Kernel thread ๋‘ ๊ฐœ๋กœ ๋‚˜๋‰œ๋‹ค. user level์—์„œ๋„ ์“ฐ๋ ˆ๋“œ๋ฅผ ๋ธŒ๋žœ์น˜ ํ•  ์ˆ˜ ์žˆ๋‹ค. kernel ์“ฐ๋ ˆ๋“œ๋Š” ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜๋Š” ๋ฐ Thread LIbrary๊ฐ€ ์ด๋ฅผ ๊ณต์œ ํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ Thread๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด User Thread๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์‹ค์ œ User Thread๋Š” Library function์„ ํ˜ธ์ถœํ•ด์„œ ์ƒ์„ฑํ•œ๋‹ค. ์ผ๋ฐ˜ Function call์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. kernel์€ User Thread์— ์‹ ๊ฒฝ์„ ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค. ๋˜ํ•œ user Thread๋Š” Creating ํ•˜๊ฑฐ๋‚˜ Switchingํ•˜๋Š” Overhead๊ฐ€ ์ ๊ธฐ ๋•Œ๋ฌธ์— ๋” ๊ฐ€๋ณ๋‹ค. 

 

 

Kernel Thread

Kernel ๋‚ด๋ถ€์— ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํ…Œ์ด๋ธ”์ด ์กด์žฌํ•œ๋‹ค. PCB์™€ TCB์— ํ•ด๋‹นํ•˜๋Š” ํ…Œ์ด๋ธ”๋“ค์ด๋‹ค. User Thread์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๊ฐ€ ์ „ํ˜€ ์—†๋‹ค.

 

 

Multithreading Models

User Thread๋Š” ํ•˜๋‚˜์˜ ๋žœ์„ ์œผ๋กœ ๊ณต์œ ๊ธฐ๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๋น„์Šทํ•˜๋‹ค๊ณ  ๋น„์œ ํ•˜์‹ฌ. ์‹ค์ œ๋กœ Kernel Thread์— ์—ฐ๊ฒฐ๋˜์–ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ๋Œ€์ผ ๋งค์นญ์ด ๋˜์ง€ ์•Š์ง€๋งŒ ๋‚จ์€ ํ•˜๋‚˜๋Š” Scheduling์˜ ๊ฐœ๋…์œผ๋กœ ์‹คํ–‰์ด ๋˜๊ฒŒ ๋œ๋‹ค. 

 

 

Multithreading Models

User Thread์™€ Kernel Thread๊ฐ€ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”๊ฐ€? User level Thread Library๊ฐ€ user Thread๋ฅผ ๋งŒ๋“ ๋‹ค. kernel scheduler๊ฐ€ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹˜. User level Thread LIbrary๊ฐ€ ํ•ด์ฃผ๋Š” ๊ฒƒ. Kernel์ด Thread๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•„๋„ Multi Thread๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. 

 

One to One model

kernel Thread์™€ User Thread๊ฐ€ ์ผ๋Œ€์ผ๋กœ ๋งค์นญ๋˜์–ด ์žˆ๋‹ค. Linux์—์„œ Thread๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์—ˆ๋‹ค๋ฉด ๊ทธ๊ฒƒ์€ User Thread๊ฐ€ ์•„๋‹ˆ๋ผ Kernel Thread์ด๋‹ค. 

 

 

Multithreading Models

3๊ฐœ์˜ Kernel thread์™€ User thread 4๋กœ ๊ตฌ์„ฑ๋จ. ๊ธฐ๋ณธ์ ์ธ Many to Many model์—์„œ ์•ˆ์ •์„ฑ์„ ๋”ํ•œ Two level model๋„ ์กด์žฌํ•จ. 

 

 

Scheduler Activation

LWP๋ž€?? Kernel Thread์™€ User Thread๋ฅผ ์–ด๋–ป๊ฒŒ Mapping ์‹œํ‚ค๋Š”์ง€์— ๊ด€ํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด๋‹ค. User level library๋Š” User์ชฝ์˜ Thread๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ๋Š”๋ฐ Kernel๊ณผ User๋ฅผ ์–ด๋–ป๊ฒŒ ์—ฐ๊ฒฐ์‹œํ‚ค๋Š”๊ฐ€. ์ค‘๊ตญ์ง‘ ์›จ์ดํ„ฐ์™€ ์ฃผ๋ฐฉ ์‚ฌ์ด์˜ ๊ด€๊ณ„. ์Œ์‹ ์ฃผ๋ฌธ์„œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์ด LWP์ด๋‹ค. ์ฑ…์— ๋”ฐ๋ผ LWP๋ฅผ Kernel Thread๋ผ๊ณ  ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค. 

 

Scheduler Activation

LWP๋Š” Virtual์— ํ•ด๋‹นํ•œ๋‹ค๊ณ  ํ‘œํ˜„. kernel Thread๊ฐ€ ๋Œ์•„๊ฐ€๋‹ค๊ฐ€ ๋ฉˆ์ถ”๋Š” Block ์ƒํƒœ์— ๋“ค์–ด์™”๋‹ค๊ณ  ํ•˜์ž. ๊ทธ๋•Œ์˜ LWP๋ฅผ ๋„˜๊ฒจ์•ผ ํ•œ๋‹ค. Block๋˜์—ˆ๋‹ค๋Š” Event๋ฅผ Scheduler๊ฐ€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๊ฒƒ์„ ์•„๋Š” ๊ฒƒ์€ OS์ด๊ณ  kernel์ด๋‹ค. ํ•˜์ง€๋งŒ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์€ User level Library์ด๋‹ค. ์ด๋Ÿฐ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ Up Call ์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ๋Š” User๊ฐ€ Kernel์„ ํ˜ธ์ถœํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ Kernel์ด User๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. Up Call Handler๋Š” User level Thread Library์— ์กด์žฌํ•œ๋‹ค.