FundamentalConcepts of OperatingSystems -...

46
Real-Time Computing and Communications Lab. Hanyang University Fundamental Concepts of Operating Systems Minsoo Ryu Department of Computer Science and Engineering Hanyang University

Transcript of FundamentalConcepts of OperatingSystems -...

Real-Time Computing and Communications Lab.Hanyang University

Fundamental Concepts of Operating Systems

Minsoo Ryu

Department of Computer Science and EngineeringHanyang University

2

2

Real-Time Computing and Communications Lab.Hanyang University

OS, what and why?1 XPage

Dual mode operation2 XPage

OS is a reusable SW platform 3 XPage

What is RTOS?4 XPage

Real-Time Computing and Communications Lab.Hanyang University

OS, What and Why?

4

4

Real-Time Computing and Communications Lab.Hanyang University

What is the OS?

App developers(set of APIs and

services)

Users(GUI and windows,

file systems)

Device Manufacturers

(device drivers)OS developers(?)

CPU, I/O hardware vendors(responsible for hardware management)

5

5

Real-Time Computing and Communications Lab.Hanyang University

Huge and Complex

Red Hat Linux version 7.1 (released April 2001) contained over 30 million lines About 8,000 man-years of development effort Over $1 billion (in year 2000 U.S. dollars)

Linux has 18M lines of code

45M lines of code

6

6

Real-Time Computing and Communications Lab.Hanyang University

Difficult to Build and Own

OS interfaces a variety of apps and HW components

CPU

Memory

Network

Disk

Graphic card

Media player

Browser

Terminal

Miscellaneous

Editor

HardwareApplications

Operating System

Highest Form of SW in CS

Needs Expert Developers

7

7

Real-Time Computing and Communications Lab.Hanyang University

Simply, OS is a Big Library

An organized collection of reusable program code Bootstrapping, device drivers, scheduling, GUI … POSIX Standard API implementations

University Library POSIX Standard 1003.1

8

8

Real-Time Computing and Communications Lab.Hanyang University

Common OS Structure

Process &Thread

GUIManager

MemoryManagement

Inter-processManagement

FileSystem

SyncPrimitives

CPUScheduling

NetworkProtocols

System Calls

DeviceDriver

DeviceDriver

Interrupt & ExceptionHandlers

DeviceDriver

DeviceDriver

Processors, Memory, Storage, Network, Sensors, Actuators …

Application 1 Application 2 Application N…

OS

HW

App

9

9

Real-Time Computing and Communications Lab.Hanyang University

Execution Flow with OS

GUIManager

FileSystem

SyncPrimitives

System Calls

DeviceDriver

DeviceDriver

Processors, Memory, Storage, Network, Sensors, Actuators …

Application 1 Application 2 Application N…

OS

HW

App

thread_create( )

task_create( ) alloc_mem( )

schedule( )

receive( )

interrupt( )

proc_protocol( )

10

10

Real-Time Computing and Communications Lab.Hanyang University

Execution Flow without OS

Processors, Memory, Storage, Network, Sensors, Actuators …

SW

HW

interrupt( )wait( )

bootstrap( )

ISR_#1( )

power ON

ISR_#2( )

ISR_#3( )

ISR_#n( )

interrupt

helper_#1( )

helper_#2( )

helper_#m( )

Event Handler Structure

11

11

Real-Time Computing and Communications Lab.Hanyang University

Execution Flow without OS

Processors, Memory, Storage, Network, Sensors, Actuators …

SW

HW

interrupt( )wait( )

bootstrap( )

power ON interrupt

schedule( )

Simple Scheduler Structure

task_#1( )

task_#2( )

task_#3( )

task_#n( )

12

12

Real-Time Computing and Communications Lab.Hanyang University

OS is more than a libraryOS is more than a library

1. Abstraction

2. Protection

3. Illusion

13

13

Real-Time Computing and Communications Lab.Hanyang University

Abstraction

Pick out only key features to reduce complexity Provide a clean, uniform, and standard interface to

users hiding hardware complexity POSIX Standard APIs

Application S/W

Hardware

Application S/W

Operating System S/W

HardwareWithout OS With OS

14

14

Real-Time Computing and Communications Lab.Hanyang University

System Call Interface

System calls provide the interface between application SW and the operating system

15

15

Real-Time Computing and Communications Lab.Hanyang University

System Calls vs. Library Calls

16

16

Real-Time Computing and Communications Lab.Hanyang University

Protection

OS has a dual mode mechanism to protect the system from many types of application faults User mode: application code runs

• Using only non-privileged instructions and/or registers Kernel mode: kernel code runs

• Using privileged I/O instructions and/or registers

17

17

Real-Time Computing and Communications Lab.Hanyang University

Processor Modes

Processors support different privilege modes Intel architectures have four privilege modes ARM architectures have eight privilege modes

Intel Protection Rings ARM Processor Modes

18

18

Real-Time Computing and Communications Lab.Hanyang University

Illusion

Make hardware limitations go away As if there are infinite number of processors (time sharing) As if there is extremely large memory (virtual memory)

time

Task A

Task BTask CTask D

Task A (weight 25.0%)

Task B (weight 12.5%)Task C (weight 50.0%)Task D (weight 12.5%)

Time Shared Multitasking Virtual Memory

Real-Time Computing and Communications Lab.Hanyang University

Dual Mode Operation

20

20

Real-Time Computing and Communications Lab.Hanyang University

Execution Mode and Context

21

21

Real-Time Computing and Communications Lab.Hanyang University

Program Execution without OS

von Neumann machine works by Fetching instructions from memory

• pointed by program counter Executing them on registers

ArithmeticLogic Unit

Registers

ControlUnit

MemoryCPU0001 LDR r0, [r1]0002 MUL r1, r2, r30003 ADD r0, r1, #40997 BIC r0, r1, r2

0998 0x100010100999 0x001010001000 0x110011011001 0x10001000

I/O devices

screen disk

pc codearea

dataarea

22

22

Real-Time Computing and Communications Lab.Hanyang University

Program Execution with OS

Memory is divided into two spaces User space Kernel space

ArithmeticLogic Unit

Registers

ControlUnit

MemoryCPU0001 LDR r0, [r1]0002 SWI 0x11110003 0x100010100997 0x00101000

0998 ADD r0, r1, #40999 BIC r0, r1, r21000 0x110011011001 0x10001000

pc

userspace

kernelspace

code

data

code

data

23

23

Real-Time Computing and Communications Lab.Hanyang University

Mode Switching via System Call

System calls are similar to function calls, but differ The implementations are in the kernel space They are executed in the kernel mode

ArithmeticLogic Unit

Registers

ControlUnit

MemoryCPU0001 LDR r0, [r1]0002 SWI 0x11110003 0x100010100997 0x00101000

0998 ADD r0, r1, #40999 BIC r0, r1, r21000 0x110011011001 0x10001000

pcuser

space

kernelspace

code

data

code

data

24

24

Real-Time Computing and Communications Lab.Hanyang University

Mode Switching via System Call

ArithmeticLogic Unit

Registers

ControlUnit

MemoryCPU0001 LDR r0, [r1]0002 SWI 0x11110003 0x100010100997 0x00101000

0998 ADD r0, r1, #40999 BIC r0, r1, r21000 0x110011011001 0x10001000

pc

ArithmeticLogic Unit

Registers

ControlUnit

MemoryCPU0001 LDR r0, [r1]0002 SWI 0x11110003 0x100010100997 0x00101000

0998 ADD r0, r1, #40999 BIC r0, r1, r21000 0x110011011001 0x10001000

pc

<before invocation> <after invocation>

25

25

Real-Time Computing and Communications Lab.Hanyang University

System Calls vs. Function Calls

Function Call System Call

- Caller and callee are in the same process

- User space, user mode

- Caller and callee are in different spaces

- Kernel space, kernel mode

26

26

Real-Time Computing and Communications Lab.Hanyang University

Common SW Structure

Real-Time Computing and Communications Lab.Hanyang University

OS is a Reusable SW Platform

28

28

Real-Time Computing and Communications Lab.Hanyang University

OS is a Reusable Platform

OS provides a reusable SW platform Reduced development cost and better software quality

Main components Framework

• Design reuse• Inversion of control

Libraries (toolkit)• Code reuse

Kernel• HW abstraction• Resource management

Applications

Kernel

Hardware

Libraries

Framework

OSPlatform

29

29

Real-Time Computing and Communications Lab.Hanyang University

Android Platform

APPLICATIONS

Home Contacts Phone Browser …

APPLICATION FRAMEWORK

gActivityManager g

Window Manager

gPackage Manager

Content Providers

View System

gTelephony Manager g

Resource Manager g

Location Manager g

Notification Manager

Surface Manager

SSL

FreeType

libc

Webkit

SQLiteMedia

Framework

OpenGL | ES

SGL

ANDROID RUNTIME

Core Libraries

Dalvik Virtual Machine

LINUX KERNELDisplay Driver

Keypad Driver WiFi Driver Audio DriversPower

Management

Camera DriverFlash Memory

DriverBinder (IPC)

Driver

LIBRARIES

Java

Java

C++

C &assembly

30

30

Real-Time Computing and Communications Lab.Hanyang University

Framework

OS defines the Architecture

OS defines the whole SW architecture Ease of SW design and implementation

Task

Kernel

Hardware

Libraries

Kernel

Hardware

LibrariesSW

architecturearchitecturereuse

Task

HW change

App change

Framework

Task Task

31

31

Real-Time Computing and Communications Lab.Hanyang University

OS and the Ecosystem

App Developers Cars

Business enablercreating new value chain

Content Providers

Lock-in & Churn-in

OperatingSystem

CEs

Mobiledevices

Wearabledevices

Internetof Things

Real-Time Computing and Communications Lab.Hanyang University

What is RTOS?

33

33

Real-Time Computing and Communications Lab.Hanyang University

RTOS does not mean “fast”RTOS does not mean “fast”• RTOS adds runtime overheads• Slightly slower performance than OS-

less systems

RTOS does mean “deterministic”RTOS does mean “deterministic”• Guarantees “worst case” times• Better performance than GPOS

34

34

Real-Time Computing and Communications Lab.Hanyang University

Timing Guarantees from RTOS

Task

interrupt

ISR scheduling ctx iret

Task Task

interrupt latency

schedulinglatency

response time

35

35

Real-Time Computing and Communications Lab.Hanyang University

Partially Preemptive vs. Fully Preemptive

timelow priority

task Amid priority

task Bhigh priority

task Cmid priority

task Busermodekernelmode

task A

preemption

syscalltask B task C

preemptionsyscall

timelow priority

task Amid priority

task Bhigh priority

task Cmid priority

task Busermodekernelmode

task A

preemption

syscalltask B task C

preemptionsyscall

syscall

response time

response time

36

36

Real-Time Computing and Communications Lab.Hanyang University

Commercial RTOSes

Commercial RTOSes QNX, VxWorks, pSOS, VRTX, Nucleus

Features Multithreading, real-time synchronization and POSIX APIs Lightweight kernel

• Even scaled down to fit in the ROM of the system Modularized structure

• Minimum kernel + service components Responsiveness

• Minimal interrupt and switching latency

37

37

Real-Time Computing and Communications Lab.Hanyang University

Linux for Real-Time?

Linux is a general purpose OS Aimed at high performance and stability

Old Linux did not support Full preemption and bounded latency

Current Linux supports Deterministic scheduler (SCHED_FIFO and EDF) Priority inheritance mutexes Lockable memory (disabling demand paging) High resolution timers? Bounded latency inside the kernel? Boot-up time

38

38

Real-Time Computing and Communications Lab.Hanyang University

Linux Variants for RT

Extensions for real-time systems RTLinux Open Event Machine PREEMP_RT patch

RTLinux Open EM PREEMPT_RT

39

39

Real-Time Computing and Communications Lab.Hanyang University

RTLinux

Developed by Victor Yodaiken, Michael Barabanov, Cort Dougan as a commercial product at FSMLabs

Wind River Systems acquired FSMLabs in February 2007, but ended it in 2011

40

40

Real-Time Computing and Communications Lab.Hanyang University

Open Event Machine

EM partitions the system into one RT domain and one non-RT domain

EM uses an event-based model, a run-to-completion model, for individual “context-less” work packages (NO threading or OS model)

EM does not need a special interrupt handling model. Needs a “scheduler” in either Linux partition OR in HW

Non-essential Linux processes and interrupts are migrated away from the EM cores

Open EM has been chosen as a starting point for the OpenDataPlaneTM (ODP) initiative by Linaro, the not-for-profit engineering organization developing open source software for the ARM® architecture

41

41

Real-Time Computing and Communications Lab.Hanyang University

PREEMPT_RT

PREEMPT_RT has merged into the mainline kernel Since August 2006 by Ingo Molnar et al. https://www.kernel.org/pub/linux/kernel/projects/rt/

42

42

Real-Time Computing and Communications Lab.Hanyang University

PREEMPT_RT

Features Preemptible critical sections Preemptible interrupt handlers Priority inheritance for in-kernel spinlocks and semaphores Deferred operations Some latency-reduction measures

Work is still going on

43

43

Real-Time Computing and Communications Lab.Hanyang University

Linux Boot-up Time

Generic boot sequenceA. Firmware initialization phaseB. Kernel initialization phaseC. User Space initialization phase

Typical boot time < 60 seconds: desktops, set top boxes, GPS devices, … < 30 seconds: smartphones < 5 seconds: smart TVs

A: < 1 sec B: 1 ~ 2 sec C: ~ 30 sec

44

44

Real-Time Computing and Communications Lab.Hanyang University

Boot-up Time Reduction

General techniques for boot-up time reduction Optimize each job of the boot sequence See backup slides for details

Snapshot-based techniques Save RAM content to nonvolatile storage before power-off Upon power-on, restore RAM state

Limitations of snapshot approach Stability problem Retake snapshot every time power is turned off Take snapshot only at “stable” state Android adds another SW layer, increasing snapshot size

45

45

Real-Time Computing and Communications Lab.Hanyang University

GPL License Issue

GPL guarantees end users the freedoms to use, study, share (copy), and modify the software Written by Richard Stallman of the Free Software

Foundation (FSF) for the GNU project

Restriction Modified or derived code must be released under GPL

LGPL (Lesser GPL) Library linking is allowed without releasing the code Android uses Bionic libc derived from BSD libc to isolate

apps from GPL and LGPL

46

46

Real-Time Computing and Communications Lab.Hanyang University