45
Real Time and Embedded Systems by Dr. Lesley Shannon Email: [email protected] Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc351 Slide Set: 2 Date: September 13, 2011 Simon Fraser University Simon Fraser University

Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Embed Size (px)

Citation preview

Page 1: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Real Time and Embedded Systems

by

Dr. Lesley Shannon

Email: [email protected] Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc351

Slide Set: 2

Date: September 13, 2011

Simon Fraser UniversitySimon Fraser University

Page 2: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Opening Thoughts

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and

ENSC 351: Lecture Set 2 2

Universe trying to produce bigger and better idiots. So far, the Universe is winning.”

- Author Rick Cook, The Wizardry Compiled

Page 3: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Slide Set Overview

• What is the “Kernel”?

• Microkernels and Monolithic kernels

ENSC 351: Lecture Set 2 3

• Microkernels and Monolithic kernels

• QNX

• Linux/PetaLinux

Page 4: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

The KERNEL

ENSC 351: Lecture Set 2 4

The KERNEL

Page 5: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• The kernel is the …

The Kernel

ENSC 351: Lecture Set 2 5

Page 6: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Abstracts low-level system resources from applications

• Parts of the OS critical to its correct operation

– Operates in supervisor mode

– Everything else is in user mode

The Kernel

ENSC 351: Lecture Set 2 6

– Everything else is in user mode

• Operates as trusted software:

– Intended to implement protection mechanisms that

cannot be changed through actions of untrusted software

• Decides which thread runs

Page 7: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Starts the execution of the selected thread

– AKA switches context to the selected thread

• Context switching threads involves

The Kernel

ENSC 351: Lecture Set 2 7

– Saving the currently running thread’s registers and

other context information

– Loading the new thread’s registers and context into

the CPU

• What does the context switch remind you of?

Page 8: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Context for Process/Thread Context Switches:Recall Context Switches for subroutines and interrupts

• “Branch subroutine” (ie bsr)

– Save PC and registers used on stack

ENSC 351: Lecture Set 2 8

• Interrupts (ie traps)

– Save PC, registers used, AND SR on stack

Page 9: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Keeps track of the state of all threads for thread

scheduling

• Possible thread states:

The Kernel

ENSC 351: Lecture Set 2 9

• Possible thread states:

– Running

• means the thread is actively running on the CPU

– Ready

• means that the thread could run right now

Page 10: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Possible Thread States (cont’d):

– Blocked

• Thread is waiting for something to happen

• There are multiple reasons a thread can end up in a

The Kernel

ENSC 351: Lecture Set 2 10

• There are multiple reasons a thread can end up in a

blocked state

– The kernel keeps track of why a thread can’t run

Page 11: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Types of Kernels

ENSC 351: Lecture Set 2 11

Types of Kernels

Page 12: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Monolithic kernels

– Linux, UNIX, MS-DOS

• Microkernels

Types of Kernels

ENSC 351: Lecture Set 2 12

• Microkernels

– QNX, Mach, CHORUS, GNU/Hurd, L4…

• Windows NT was “supposed” to be, but not

• Other kernel types

Page 13: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• The entire kernel is run in the kernel space in

supervisor mode

• Uses a set of primitives or system calls to

Monolithic Kernels

ENSC 351: Lecture Set 2 13

• Uses a set of primitives or system calls to

implement operating system services such as:

– process management,

– concurrency, and

– memory management

– … all in kernel mode

Page 14: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• The code integration is very tight and difficult to do

correctly

• Since all the modules run in the same address

Monolithic Kernels

ENSC 351: Lecture Set 2 14

• Since all the modules run in the same address

space, a bug in one module can crash the whole

system.

• When done well, the tight internal integration of

components makes a good monolithic kernel highly

efficient.

Page 15: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Monolithic Kernel: “The layers in a Linux System”

ENSC 351: Lecture Set 2 15

Page 16: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Monolithic Kernel: The “Structure of the Linux Kernel”

ENSC 351: Lecture Set 2 16

Page 17: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Provides no operating-system services at all (pure

form)

– only the mechanisms needed to implement such services

including:

• low-level address space management,

Microkernels

ENSC 351: Lecture Set 2 17

• low-level address space management,

• thread management, and

• inter-process communication

• Designed to be Modular and Small

Page 18: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• It is the only part of the system executing in kernel

mode

• The actual operating-system services are provided

Microkernels

ENSC 351: Lecture Set 2 18

• The actual operating-system services are provided

by user mode servers including:

– device drivers,

– protocol stacks,

– file systems, and

– the user interface

– … outside the kernel

Page 19: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Microkernel Structure

ENSC 351: Lecture Set 2 19

Page 20: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Other Kernels

• There are other kernels, but you don’t need to worry about them

• If you are interested, check out:

ENSC 351: Lecture Set 2 20

• If you are interested, check out:

– Hybrid Kernels

– Nanokernel

– Exokernel

Page 21: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

ENSC 351: Lecture Set 2 21

QNX

Page 22: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

• The higher-level OS functionality is provided by:

– A tiny kernel that provides minimal services

– A team of optional cooperating processes

ENSC 351: Lecture Set 2 22

– A team of optional cooperating processes

• The QNX kernel does not have many typical OS services

– These are provided by optional processes

Page 23: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

• Optional system processes could include:

– Filesystem managers

– Character device managers

– Graphical user interface (Photon)

ENSC 351: Lecture Set 2 23

– Graphical user interface (Photon)

– Native network manager

– TCP/IP

• System processes are essentially the same as user-written applications

Page 24: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

ENSC 351: Lecture Set 2 24

Page 25: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

• The Kernel provides:

– Thread services

• via POSIX primitives

– Signal services

ENSC 351: Lecture Set 2 25

– Signal services

• via POSIX primitives

– Synchronization services

• via POSIX primitives

Page 26: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

• The Kernel provides (cont’d):

– Timer services

• via POSIX

– Scheduling services

ENSC 351: Lecture Set 2 26

– Scheduling services

• via POSIX realtime scheduling algorithms

– Process management services

• Process manager + kernel = procnto

– Manages processes, memory, and pathname space

Page 27: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

• The Kernel provides (cont’d):

– Message-passing services

• The kernel routes all messages between all threads

in the system

ENSC 351: Lecture Set 2 27

• Inter-Process Communication (IPC) services

– Allows communication between all processes

(application or device drivers)

Page 28: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

• Unlike threads, the kernel is never scheduled for execution

– The code in the kernel is only executed as the

result of:

ENSC 351: Lecture Set 2 28

result of:

• an explicit kernel call,

• an exception, or

• a response to a hardware interrupt

Page 29: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Linux/PetaLinux

ENSC 351: Lecture Set 2 29

Linux/PetaLinux

Page 30: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

PetaLinux

• PetaLinux 2.1 implements a full Linux Kernel

– Version 2.6.37; updates include a device tree

ENSC 351: Lecture Set 2 30

• Therefore PetaLinux has many typical OS services

• Recall the “Structure of the Linux Kernel”

Page 31: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Linux

• System processes include:

– Filesystem managers

– Character device managers

– Block device drivers

ENSC 351: Lecture Set 2 31

– Block device drivers

– Network Device Drivers

– TCP/IP protocols

• Can also include Loadable Kernel

Modules (LKMs)

– Think Dynamically Loaded Libraries (DLLs)

Page 32: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Linux/PetaLinux

• The PetaLinux Kernel provides:

– Thread services

• via POSIX primitives

– Signal services

ENSC 351: Lecture Set 2 32

– Signal services

• via POSIX primitives

– Synchronization services

• via POSIX primitives

Page 33: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Linux/PetaLinux

• The PetaLinux Kernel provides (cont’d):

– Timer services

• via POSIX

– Scheduling services

ENSC 351: Lecture Set 2 33

– Scheduling services

• via POSIX “realtime” scheduling algorithms

– Real-time FIFO; Real-time Round Robin; Timesharing

– Process management services & Memory Management

services

Page 34: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

QNX

• The Kernel provides (cont’d):

– Inter-Process Communication (IPC) services

• Allows Communication between all processes

(application or device drivers)

ENSC 351: Lecture Set 2 34

• Message-passing services are supported via libraries

– Check out the Message Passing Interface (MPI)

functions designed for C and Fortran-77

Page 35: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Questions?

• What type of kernel is the QNX kernel? What

about Linux?

ENSC 351: Lecture Set 2 35

• Why would someone today still choose to use a monolithic kernel?

Page 36: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Questions?

• What type of kernel should be used in

Embedded and Real Time systems? Why?

ENSC 351: Lecture Set 2 36

• Recalling our discussion of processor

architectures, why might a multi-threaded

implementation of an application be slower than

a single-threaded version?

Page 37: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Question

• What additional information has to be saved for

a thread context switch from that of a context

switch between subroutines or a software

interrupt?

ENSC 351: Lecture Set 2 37

Page 38: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Real Time and Embedded Systems

by

Dr. Lesley Shannon

Email: [email protected] Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc351

Slide Set: 2

Date: September 13, 2011

Simon Fraser UniversitySimon Fraser University

Page 39: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Monolithic Kernels

ENSC 351: Lecture Set 2 39

Courtesy of Wikipedia

Page 40: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Microkernels

ENSC 351: Lecture Set 2 40

Courtesy of Wikipedia

Page 41: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

Kernel-level threads vs User-level Threads

ENSC 351: Lecture Set 2 41

Kernel-level threads vs User-level Threads

Page 42: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Supported directly by the O/S

• The O/S has a separate thread for each process**

Kernel-level Threads

ENSC 351: Lecture Set 2 42

process**

– Performs O/S activities on behalf of the O/S

– What is the name of this thread?

• [Hint: Recall the Collaboration graph Notation

discussion]

**At least- depends on the multi-threading model

Page 43: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Performs thread creation, scheduling and management in kernel space

• Thread management inside the kernel is

Kernel-level Threads

ENSC 351: Lecture Set 2 43

• Thread management inside the kernel is generally slower that in user space

– i.e. Takes longer to create and manage kernel

threads than user threads

Page 44: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• Run on top of the kernel

• Only exist within a process

– Cannot access a thread in a different process

User-level Threads

ENSC 351: Lecture Set 2 44

– Cannot access a thread in a different process

• Isolation and modularity provide reliability

• Used by programmers to handle multiple flows of control within a process

Page 45: Real Time and Embedded Systems - Simon Fraser Universitylshannon/courses/ensc351_11/Lecture_Set_2_11.pdf · Real Time and Embedded Systems by ... • Parts of the OS critical to its

• The kernel is unaware of user-level thread

• Implemented with a thread library

– Example?

User-level Threads

ENSC 351: Lecture Set 2

– Example?

– Supports creation and scheduling management

outside of the kernel

• Done in user space without the kernel

– Generally fast to create and manage