[OS] Operating System(2-2): Design, Implementation, Structure
๐ ์ด์์ฒด์ ์ ๊ณต ์์ ์ ๋ฆฌ
Operating system Design and Implementation
- OS Design is Not a Simple Problem(โSolvableโ)
- However, some approaches have been successful
- ์ด์์ฒด์ ์ ๋ด๋ถ ๊ตฌ์กฐ๋ ๋ค์ํจ
- ํ๋์จ์ด ๋ฐ ์์คํ ์ ํ์ ๋ฐ๋ผ ์ฐจ์ด๊ฐ ์์
- Start by Defining Goals and Specifications
- OS Goals:
- User Goals: OS should be easy to use, learn, reliable, safe, fast
- System Goals:OS should be easy to design, implement, maintain, flexible, reliable, error-free, efficient
โ Key Principle in OS Design
- Policy: โWhat will be done?โ
- Mechanism: โHow to do it?โ
- Mechanism Defines Methods, Policy Determines Decisions
- ์ด์์ฒด์ ์์ ์ ์ฑ ๊ณผ ๋ฉ์ปค๋์ฆ์ ๋ถ๋ฆฌํ๋ฉด ์ ์ฐ์ฑ์ ๋์ผ ์ ์์
- Importance of Separating Policy & Mechanism
- ์ ์ฑ ์ด ๋ฐ๋์ด๋ ๋ฉ์ปค๋์ฆ์ ์์ ํ์ง ์์๋ ๋จ
- Ex:
- Policy: Limit CPU usage time
- Mechanism: Use a timer to measure execution time
- CPU๋ฅผ ์๊ฐ ์ ํ์ ๋์ง ์์ผ๋ฉด ์ด๋ ํ ์ฌ๋์ด ๋ ์ ํด๋ฒ๋ฆฌ๋ ๋ฌธ์ ๊ฐ ์๊ธธ ์ ์๋ค
- ์๊ฐ ์ ํ์ ๋๋ ์์น์ Policy๋ก ์ ํ๋ค๋ฉด ์ด๋ป๊ฒ ๋ฌด์จ ๋ฐฉ๋ฒ์ผ๋ก ์๊ฐ์ ์ ํํ ๊ฑด์ง ์ ํ๋๊ฑด Mechanism
- OS Design is a Highly Creative Software Engineering Task
Implement
OS๋ ๋ค์ํ ์ธ์ด๋ก ๊ตฌํ๋จ
- ์ด์ฐฝ๊ธฐ: assembly language
- ์ดํ: Algol, PL/1 ๋ฑ์ system programming languages
- ํ์ฌ: C/C++
OS๋ ์ผ๋ฐ์ ์ผ๋ก ์ฌ๋ฌ ์ธ์ด์ mix์
- Lowest levels in assembly
- Main body in C
Systems programs: C, C++, Python, Perl, Shell Script
- Higher-Level Languages easier to **port** to other hardware, but slower(์ด์์ฑโ, ์ฑ๋ฅโ)
- Emulation enables OS to run on Non-Native Hardware(๋น์๋ณธ ํ๋์จ์ด)
OS Structure
Various ways to structure ones
- Simple structure -
MS-DOS
- More complex -
UNIX
- Layered -
an abstraction
- Microkernel -
Mach
Monolithic(๋จ์ผ) Structure - Original UNIX
๋จ์ผ ๊ณ์ธต์ผ๋ก ๋์ด์๋ ์์คํ
- ๋จ์ผ ๊ณ์ธต: ๊ณ์ธตํX, ํ ๊ฐ์ ์ธต์
file system
,CPU Scheduling
๋ฑ๋ฑ ๋ค ๋ฃ์ ๊ตฌ์กฐ - Limited by hardware functionality and had minimal structuring
- 2๊ฐ์ง ์ฃผ์ ๊ตฌ์ฑ ์์
- System Programs
- The Kernel:
- Operates below the system-call interface and above hardware
- Provides core OS functionalities(
File System
,CPU Scheduling
,Memory Management
)
- ๋ชจ๋ ๊ธฐ๋ฅ์ด ํ๋์ ๋ ๋ฒจ(one level)์์ ์ํ๋จ(Monolithic Structure)
Original UNIX ๋ชจ๋ ๊ธฐ๋ฅ์ด ์ปค๋ ๋ด์์ ํ๋์ ๋ ๋ฒจ๋ก ์๋ํจ
๋ชจ๋๋ฆฌ์ ์ปค๋์ด์ง๋ง, ๋ชจ๋ํ๋ฅผ ํตํด ๋ณด๋ค ์ ์ฐํ๊ฒ ๊ธฐ๋ฅ์ ์ถ๊ฐ ๋ฐ ์ ๊ฑฐํ ์ ์์
Layered OS Architecture
๐ The operating system is divided into a number of layers(levels)
- The bottom layer(layer 0) is the hardware; the highest (layer N) is the user interface
Layered
- With modularity, layers are selected such that each uses functions(operations) and services of only lower-level layers
- ์์ ๊ณ์ธต์์ ํ์ ๊ณ์ธต์ ํธ์ถํ ์๊ฐ ์์ง๋ง ๋ฐ๋๋ ์ ๋จ
Microkernels
- Moves as much from the kernel into user space
- Mach example of microkernel
- Mac OS X kernel(Darwin) partly based on Mach
- Communication takes place between user modules using message passing
- ๋ณดํต ์ฌ์ฉํ๋ monolithic kernel์์๋ API๋ฅผ ํตํด์(func call) ์๋น์ค๋ฅผ ์ ๊ณตํ์ง๋ง microkernel์ message passing์ ํตํด ์ ๊ณตํจ
- ๊ทธ๋ผ function call์ ๋จ์ํ messeage ํํ๋ก ๋ฐ๊พผ๊ฒ ์๋๊ฐ?
- function call์ ๋๊ธฐ์์ด์ง๋ง messeage passing์ ๋๊ธฐ์, ๋น๋๊ธฐ์ ๋๋ค ๊ฐ๋ฅํ๋ค
- ๋๊ธฐ์์ messeage๊ฐ ์ฌ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ์ง๋ง ๋น๋๊ธฐ์์ ๋ฉ์ธ์ง๊ฐ ์ค๋ฉด ๋์ค์ ํ์ธํ๊ณ ๊ทธ๋ ํ์ ํ๋ ๊ฐ๋
โ Benefits of Microkernel
- Easier to extend a microkernel: ์๋ก์ด ๊ธฐ๋ฅ ์ถ๊ฐ ์ ์ปค๋์ ์ง์ ์์ ํ์ง ์๊ณ ์๋ก์ด ์๋น์ค ๋ชจ๋์ ์ถ๊ฐํ๋ ๋ฐฉ์์ผ๋ก ํ์ฅ ๊ฐ๋ฅ
- Easier to port the operating system to new architectures: ํ๋์จ์ด ์์กด์ฑ์ด ๋ฎ์ ์๋ก์ด ์ํคํ ์ฒ(CPU, ํ๋์จ์ด)์ ์ฝ๊ฒ port ๊ฐ๋ฅ
- More reliable: Kernel Mode์์ ์คํ๋๋ ์ฝ๋๊ฐ ์ค์ด๋ค์ด ์ด์์ฒด์ ๊ฐ ๋์ฑ ์์ ์
- More secure
โ Detriments of Microkernel
- Performance overhead of user space to kernel space communication
- Microkernel์์๋ ์ฌ๋ฌ ์ฌ์ฉ์ ๋ชจ๋ ๊ฐ
Message Passing
๋ฐฉ์์ผ๋ก ํต์ ํด์ผ ํ๋ฏ๋ก, ์ฑ๋ฅ ์ ํ(Overhead) ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์.
- Microkernel์์๋ ์ฌ๋ฌ ์ฌ์ฉ์ ๋ชจ๋ ๊ฐ
Modules
- Many modern operating systems implement loadable kernel modules(LKMs)
- Uses object-oriented(๊ฐ์ฒด์งํฅ) approach
- Each core component is separate
- Each communicate to the others over known interfaces
- Each is loadable as needed within the kernel(ํ์ํ ๋๋ง ๋์ ์ผ๋ก ๋ก๋ ๊ฐ๋ฅ)
- Overall, similar to layers but with more flexibility
- Examples: Linux, Solaris, etc.
Hybrid Systems
- ๋๋ถ๋ถ์ ์ต์ ์ด์์ฒด์ ๋ ๋จ์ผํ ์์ํ ๋ชจ๋ธ์ด ์๋
- Hybrid combines multiple approaches to address performance, security, usability needs
- Linux and Solaris kernels:
Monolithic
+modular
โ dynamic loading of functionality(์๋ก์ด ๊ธฐ๋ฅ์ ์ฝ๊ฒ ์ถ๊ฐ ๊ฐ๋ฅ) - Windows:
Monolithic
+Microkernel
โ for different subsystem personalities - Apple Mac OS X:
- hybrid, layered, Aqua UI plus Cocoa programming environment
Mach microkernel
+BSD Unix
+ I/O kit and dynamically loadable modules(called kernel extensions)
- Linux and Solaris kernels:
MacOS and iOS Structure
macOS and iOS Structure
Darwin Structure
Darwin Structure
- Two System Call Interfaces
- Mach Traps: Mach ๋ง์ดํฌ๋ก์ปค๋์์ ์ ๊ณตํ๋ ์์คํ ํธ์ถ
- BSD(POSIX) System Calls: UNIX/POSIX ํ์ค์ ์ง์ํ๋ BSD ์ปค๋์ ์์คํ ํธ์ถ
Android
Developed by Open Handset Alliance(mostly Google)
- Open Source
- Similar stack to iOS
- Based on Linux kernel but modified
process
,memory
,device-driver management
์ ๊ณต- Adds power management
- Runtime Environment
- ํต์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ART(Android Runtime) virtual machine ํฌํจ
- Apps์ Java ๋ฐ Android API๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ๋ฐ๋จ
- Java class files โ Java bytecode ๋ณํ โ ART VM์์ ์คํ
- Libraries:
web browser(webkit)
,database(SQLite)
,multimedia
,smaller libc
- Dalvik VM: JIT(Just-In-Time) Compilation
- ART VM: AOT(Ahead-Of-Time) Compilation
Android Structure
- ART(Android Runtime) VM: ์๋๋ก์ด๋ ์ ํ๋ฆฌ์ผ์ด์ ์คํ ํ๊ฒฝ
- JNI(Java Native Interface): Java ์ฝ๋๊ฐ ๋ค์ดํฐ๋ธ C/C++ ์ฝ๋์ ํต์ ํ ์ ์๋๋ก ์ง์
- HAL(Hardware Abstraction Layer): Hardware์ Software ์ฌ์ด์์ Hardware ๊ธฐ๋ฅ์ ์ํํธ์จ์ด์ ์ผ๋ก ์ ๊ทผํ ์ ์๋๋ก ์ง์ํ๋ ๊ณ์ธต
System Boot
๐ When power is initialized on the system, execution starts at a fixed memory location
- OS๋ ํ๋์จ์ด์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋๋ก ๋ก๋๋์ด์ผ ํจ โ
Booting Process
- BIOS / UEFI ์คํ (Executing BIOS / UEFI)
Small piece of code
๊ฐ ROM or EEPROM์ ์ ์ฅ๋จ- ์ด๋ฅผ ํตํด ์ปค๋์ ์ฐพ๊ณ , ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ ํ ์คํ
- Modern systems replace BIOS with UEFI (Unified Extensible Firmware Interface)
- Boot Block ํ์ฉ (Optional Two-step Process)
- Boot Block์ด ROM ์ฝ๋์ ์ํด ์คํ๋จ
- Boot Block์ด ๋์คํฌ์์ bootstrap loader๋ฅผ ๋ก๋ํ์ฌ ์คํ
- Bootloader Execution - GRUB ์ฌ์ฉ
- GRUB (Grand Unified Bootloader)๋ฅผ ํตํด ์ฌ๋ฌ OS ์ปค๋์ ์ ํ ๊ฐ๋ฅ
- Kernel Loads and System Running
- GRUB(Grand Unified Bootloader) ์ญํ
- GRUB๋ Linux์์ ์ฃผ๋ก ์ฌ์ฉ๋๋ฉฐ, ๋ค์ํ ๋ถํ ์ต์ ์ ์ง์
- ๋ถํ ์ ์ฌ์ฉ์๊ฐ ์ํ๋ ์ปค๋์ ์ ํ ๊ฐ๋ฅ
- GRUB๋ open source๋ก ๊ฐ๋ฐ๋จ
Operating System Debugging
๐ Debugging is finding and fixing errors, or bugs
- OS generate log files containing error information
- Core Dump & Crash Dump
- Core Dump์ Crash Dump๋ ํ๋ก๊ทธ๋จ ๋ฐ OS ์ค๋ฅ ๋ฐ์ ์ ํ์์ ์ธ ๋๋ฒ๊น ๋๊ตฌ
- Core Dump: ํ๋ก๊ทธ๋จ์ด ๋น์ ์์ ์ผ๋ก ์ข ๋ฃ๋ ๋, ํ๋ก์ธ์ค์ ๋ฉ๋ชจ๋ฆฌ ์ํ๋ฅผ ์ ์ฅํ ํ์ผ
- Crash Dump: ์ด์์ฒด์ ๊ฐ ๋น์ ์์ ์ผ๋ก ์ข ๋ฃ๋ ๋, ์ปค๋ ๋ฉ๋ชจ๋ฆฌ ์ํ๋ฅผ ์ ์ฅํ ํ์ผ
- Performance Tuning(์ฑ๋ฅ ์ต์ ํ)
- Trace Listings(์ถ์ ๋ชฉ๋ก): ํ๋ก๊ทธ๋จ ์คํ ์ค์ ํ๋์ ๊ธฐ๋กํ์ฌ ๋ถ์ํ๋ ๊ธฐ๋ฒ
- Profiling: ์ฃผ๊ธฐ์ ์ผ๋ก Instruction Pointer๋ฅผ ์ํ๋งํ์ฌ ์ฑ๋ฅ ๋ถ์
- Improve performance by removing bottlenecks(๋ณ๋ชฉ ํ์)
- OS must provide means of computing and displaying measures of system behavior
- For example, โtopโ program(Linux) or Windows Task Manager
- Kernighanโs Law
- โ๋๋ฒ๊น ์ ์ฝ๋๋ฅผ ์์ฑํ๋ ๊ฒ๋ณด๋ค ๋ ๋ฐฐ ๋ ์ด๋ ต๋คโ
- โ์ฝ๋๋ฅผ ๊ฐ๋ฅํ ํ ์ค๋งํธํ๊ฒ ์์ฑํ๋ค๋ฉด, ๋๋ฒ๊น ์ ํ ๋งํผ ๋๋ํ์ง ์์ ๊ฐ๋ฅ์ฑ์ด ๋๋คโ
- ์ฆ, ์ง๋์น๊ฒ ๋ณต์กํ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋๋ฒ๊น ์ด ์ด๋ ค์์ง๋ค๋ ์๋ฏธ!!
Tracing
Tracing: Collects data for a specific event, such as steps involved in a system call invocation
โ ์์คํ ์ด ์ด๋ค ์์๋ก ์ด๋ค ์์์ ์ ๊ทผํ๋์ง ์ถ์ ๊ฐ๋ฅ- ๋ํ์ ์ธ ํธ๋ ์ด์ฑ ๋๊ตฌ๋ค
strace
: ํน์ ํ๋ก์ธ์ค๊ฐ ํธ์ถํ๋ system call์ ์ถ์ ํจ (ex: ํ์ผ ์ด๊ธฐ, ์ฝ๊ธฐ, ์ฐ๊ธฐ ๋ฑ)gdb
: GNU Debugger. ์์ค ์ฝ๋ ๋จ์์ ๋๋ฒ๊น ๋๊ตฌ (breakpoint ์ค์ , ๋ณ์ ํ์ธ ๋ฑ)perf
: Linux ๋ด์ฅ ์ฑ๋ฅ ๋ถ์ ํด ๋ชจ์. CPU ์ฌ์ฉ๋, ์บ์ ๋ฏธ์ค ๋ฑ ํ๋์จ์ด ์ฑ๋ฅ๋ ์ถ์ ๊ฐ๋ฅtcpdump
: collects network packets
- ๋ํ์ ์ธ ํธ๋ ์ด์ฑ ๋๊ตฌ๋ค
BCC
user-level code
์ kernel code
์ฌ์ด์ ์ํธ์์ฉ์ debuggingํ๋ ๊ฒ์ ์ด๋ฅผ ๋ชจ๋ ์ดํดํ๊ณ ๋ถ์ํ ์ ์๋ toolset ์์ด๋ ๊ฑฐ์ ๋ถ๊ฐ๋ฅํจ
๐BCC = BPF Compiler Collection: ๋ฆฌ๋ ์ค๋ฅผ ์ํ ๊ฐ๋ ฅํ ํธ๋ ์ด์ฑ ๋๊ตฌ ๋ชจ์
Linux bcc/BPF Tracing Tools