Post

[OS] Operating System(2-2): Design, Implementation, Structure

[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) alt text

    Original UNIX ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ์ปค๋„ ๋‚ด์—์„œ ํ•˜๋‚˜์˜ ๋ ˆ๋ฒจ๋กœ ์ž‘๋™ํ•จ

alt text

๋ชจ๋†€๋ฆฌ์‹ ์ปค๋„์ด์ง€๋งŒ, ๋ชจ๋“ˆํ™”๋ฅผ ํ†ตํ•ด ๋ณด๋‹ค ์œ ์—ฐํ•˜๊ฒŒ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ ๋ฐ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Œ

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

alt text

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

  1. Easier to extend a microkernel: ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ์‹œ ์ปค๋„์„ ์ง์ ‘ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ์„œ๋น„์Šค ๋ชจ๋“ˆ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ™•์žฅ ๊ฐ€๋Šฅ
  2. Easier to port the operating system to new architectures: ํ•˜๋“œ์›จ์–ด ์˜์กด์„ฑ์ด ๋‚ฎ์•„ ์ƒˆ๋กœ์šด ์•„ํ‚คํ…์ฒ˜(CPU, ํ•˜๋“œ์›จ์–ด)์— ์‰ฝ๊ฒŒ port ๊ฐ€๋Šฅ
  3. More reliable: Kernel Mode์—์„œ ์‹คํ–‰๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ์ค„์–ด๋“ค์–ด ์šด์˜์ฒด์ œ๊ฐ€ ๋”์šฑ ์•ˆ์ •์ 
  4. More secure

โœ… Detriments of Microkernel

  • Performance overhead of user space to kernel space communication
    • Microkernel์—์„œ๋Š” ์—ฌ๋Ÿฌ ์‚ฌ์šฉ์ž ๋ชจ๋“ˆ ๊ฐ„ Message Passing ๋ฐฉ์‹์œผ๋กœ ํ†ต์‹ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ, ์„ฑ๋Šฅ ์ €ํ•˜(Overhead) ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ.

alt text

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)

MacOS and iOS Structure


alt text

macOS and iOS Structure

Darwin Structure


alt text

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

alt text

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
    1. BIOS / UEFI ์‹คํ–‰ (Executing BIOS / UEFI)
    • Small piece of code๊ฐ€ ROM or EEPROM์— ์ €์žฅ๋จ
    • ์ด๋ฅผ ํ†ตํ•ด ์ปค๋„์„ ์ฐพ๊ณ , ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œ ํ›„ ์‹คํ–‰
    • Modern systems replace BIOS with UEFI (Unified Extensible Firmware Interface)
      1. Boot Block ํ™œ์šฉ (Optional Two-step Process)
    • Boot Block์ด ROM ์ฝ”๋“œ์— ์˜ํ•ด ์‹คํ–‰๋จ
    • Boot Block์ด ๋””์Šคํฌ์—์„œ bootstrap loader๋ฅผ ๋กœ๋“œํ•˜์—ฌ ์‹คํ–‰
      1. Bootloader Execution - GRUB ์‚ฌ์šฉ
    • GRUB (Grand Unified Bootloader)๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ OS ์ปค๋„์„ ์„ ํƒ ๊ฐ€๋Šฅ
      1. 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: ๋ฆฌ๋ˆ…์Šค๋ฅผ ์œ„ํ•œ ๊ฐ•๋ ฅํ•œ ํŠธ๋ ˆ์ด์‹ฑ ๋„๊ตฌ ๋ชจ์Œ

  • ๊ธฐ๋ฐ˜ ๊ธฐ์ˆ ์€ eBPF (Extended Berkeley Packet Filter) alt text

    disk I/O ํ™œ๋™ ์ถ”์  ๋„๊ตฌ

alt text

Linux bcc/BPF Tracing Tools

This post is licensed under CC BY 4.0 by the author.