Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Contents
OS Motivation
OS History
OS Classification
Programs, tasks and processes, threads
Process/thread contexts
RTOS
A detailed view at Scheduling
Queues and TCBs
Example: A simple scheduler
OS Purpose
Enable effective use of computing infrastructure
by providing relevant services for organisation and execution
of applications (API).
off-loading functionality from application
higher level of abstraction
OS History I
Beginning: No OS
Programming by setting jumpers
Programming by punch cards
Output device: printer
Automate program input: Batch OS
Program read by a card-to-tape converter
Tape used as an input device to the computer
OS reads one Job after the other from the tape
Output written on tape
OS History II
OS History III
Dealing with slow input: Multiprogramming for better CPU usage
Memory partitioning
Load multiple programs in parallel
Run sequentially
Dealing with slow output: Spooling
simultaneous peripheral operations on-line
Output queue decouples resource and application
OS History IV
Dealing with slow users: Interactive time-sharing
Mainframe + 1 terminal / user
Terminal = standard input / standard output
Protection mechanisms, resource management
Multics (1965)
One of the first time-sharing OS
Concept of virtual memory
Developers: MIT, Bell Labs, General Electric
OS History V
UNICS (then Unix) - multitasking, multi-user
Motivation: Usable Multics, cross-development tool
Modular design, unified file system for IPC, shell script language
Firstly developed by Ken Thompson (former Bell employee) on a
PDP-7
C was designed by Dennis Ritchie as the programming language
for Unix
First 2 main derivatives developed (BSD - Berkeley Software
Distribution, System V AT&T)
De-facto standard for academia during late 70's and 80's
Linux = free open source x86 port of key Unix functionality
Historic OS Habitat
OS Classification I
By application domain
General Purpose
Windows, Unix, Linux, Android, OS X
Batch
Historical: OS/360, Today: Printers
Smartcard
MULTOS
OS Classification II
By features
Single user vs. multi user
Single-tasking vs. multi-tasking
By supported CPU
Distributed Desktop Embedded
OS Kernel Types
Our Focus
OS Terminology
What is a Task?
Typical embedded systems decompose their functionality into
smaller, independent (concurrent) programs
These programs work together in an organized way
Each of these programs is called a task when executed
A system that can handle multiple tasks is called a
multitasking system
-> A task is a unit of execution supported by
some multitasking system
Parts of a Process
A process is much more than just program code
Set Menu
Prepare Stock
Prepare Meat
Add Veges
Season Meat
Manage Seasoning
Cook Meat
Clean Veges
Steam Veges
Clean Fruit
Season Fish
Puree Fruit
Prepare Sauce
Add Sugar
Steam Fish
[Needs Salt]
Simmer
Clean Fish
Prepare Garnish
Make Sauce
Adjust Flavoring
Add Salt
Put into Freezer
[Lightly Frozen]
Serve Soup
Reduce Sauce
Serve Veges
Serve Fish
Eat
Enjoy Meal
Multitasking
A multitasking design must support the operations:
Operating System
An Operating System (OS) is software that provides some kind
of task coordination
If the OS provides the functionality of specifying and meeting
time constraints it is called a real-time operating system
Every OS has three primary components to implement task
coordination:
Schedule
Dispatcher
Intertask communication
Dispatcher
Does everything required to start a task (context switch)
Intertask Communication
Exchange data and information between tasks on the same machine
or across machines.
Can also facility communication between tasks the OS
Other OS components
Most operating systems that we use are full featured
operating systems
Windows
Linux
OSX
Process Creation
When a process is created it is allocated resources by the
system or OS, potentially including:
Stack
Memory address space
Registers (on the CPU)
Program counter
I/O ports
Network connections
File descriptors, etc.
CPU as a Resource
Traditional view of computing focuses on the program, not
the process
You talk about a program running on the computer, or maybe
tasks within the program
For embedded application, we need a different point of view
The microprocessor is the center, not the program
Persistence
Time to complete task is called execution time
Time from entering the system to terminating (finishing) is
called the persistence of a task
Limited Resources
For a single process in the system, no contention for resources
It can run for as long as it wants
Use as much memory as it wants
Use all other resources freely
Resolving Contention
Contention can be resolved by carefully managing how
ressources are allocated to processes
Usually limit the time process can use resource
Implementing Control
If the CPU is a resource -> most important resource
Processes also share
Timers
I/O units
Busses
Process State
A system with three processes
One is always running, other two waiting for CPU
Must decide
Which process gets the CPU
When the task gets the CPU
Solution
Schedule
Schedule
The schedule determines
When a process gets the CPU
Under what conditions the process gets the CPU
How long the process can use CPU and other resources
Schedule Strategy
For embedded systems there are three
Multiprogramming
Real-time
Time-sharing
Multiprogramming
Running task continues until it requires an external input
This includes waiting for I/O event or timer
Real-time
Task with specific deadlines
Guaranteed to complete before deadline expires
Systems that require a response to event within well defined time
limits or constraints use this system
Hard real-time
Context
A tasks context comprises important information about the
state of the task;
such as:
the values of any variables held in the CPUs registers
the value of PC (program counter)
and so on
Context Switch
Upon context switch
State of running process is saved
State of new process is restored and execution of this process
continues from where it left off
Only for process that was previously running
Threads
Task or process characterized by resources required to execute
that portion of the program
A thread is the smallest subset of resources required to
execute a program
Includes all CPU registers, including PC
Program stack
Threads (cont.)
A thread can only be in one process
Processes without
a thread cant do
anything
A single thread
sequentially executes
a set of instructions
Called thread of
execution or control
Status
Resources
Stack
Thread
Firmware
(Address Space)
CPU
Data
(Address Space)
Single Thread
Thread has a stack and status information, including copy of
registers (lightweight context)
Uses, but does not contain
Code (firmware)
Data
CPU (and the physical registers)
Other process resources
Multiple Threads
More complicated embedded systems perform a single
primary function, using related operations
During partitioning and functional decomposition, identify
which parts can use parallel execution
Could have one task for each type of I/O
Resources
CPU
Multiple Threads
Threads are useful because switching is so fast
Much less information is saved and restored on switch
An operating system supporting tasks with multiple threads is
a multithreaded operating system
Can easily extend this design to support multiple separate
process as well, each with its own threads
This is called multiprocess-> multithread design
Supervisor Mode
Access
User Mode
Address Space
User Mode
Access
Main advantage is
increased modularity
Layers are designed such
that each layer
Uses functions / operations
and services of lower layers
(Embedded)
Application
Command Interface
System I/O
System and User
Memory Management
Intertask
Communication
CPU and Resource
Scheduling / Dispatching
Thread Management
Microprocessor Hardware
and
Hardware Resources
(Embedded)
Application
Command Interface
System I/O
System and User
Memory Management
Intertask
Communication
CPU and Resource
Scheduling / Dispatching
Thread Management
Microprocessor Hardware
and
Hardware Resources
Job Queue
Process entering system is put into the job queue
All jobs in the system are contained in the queue, including
system jobs and user jobs
Ready and waiting processes are in ready queue
Can be other queues, including those for resources
Processes waiting for a resource are on device queue
Process ID
Program Counter
Register Contents
Memory Limits
Open Files
Etc.
Task Queue
When a task enters the system it is placed into the Entry
Queue or Job Queue
Easiest way to implement queue is with a linked list data
structure
List node data contains TCBs
Each TCB points to the next TCB in the queue
TCB in C
TCB is implemented as a struct which
Contains pointers to all relevant task information
TCB C Example
Here is an example TCB in C
A Simple Kernel
3 simple jobs are scheduled and performed
Bring in some data
Perform a computation on the data
Display the data
Data Buffer
Display
3.0
Remote Output
Example 11.0
// Building a simple OS kernel - step 1
#include <stdio.h>
// Declare the prototypes for the tasks
void get (void* aNumber);
// input task
void increment (void* aNumber); // computation task
void display (void* aNumber); // output task
void main(void)
{ int i=0;
// queue index
int data;
// declare a shared data
int* aPtr = &data;
// point to it
void (*queue[3])(void*);
// declare queue as an array of pointers to
// functions taking an arg of type void*
queue[0] = get;
// enter the tasks into the queue
queue[1] = increment;
queue[2] = display;
Example 11.1
// Building a simple OS kernel - step 2
#include <stdio.h>
// Declare the prototypes for the tasks
void get (void* aNumber);
// input task
void increment (void* aNumber); // computation task
void display (void* aNumber);
// output task
// Declare a TCB structure
typedef struct
{
void* taskDataPtr;
void (*taskPtr)(void*);
} TCB;
void main(void)
{
int i=0;
// queue index
int data;
// declare a shared data
int* aPtr = &data; // point to it
TCB* queue[3];
// perform computation
Homework
What advantage do you see in using a TCB (Code 11.1) compared to
the approach in Code 11.0 (max. 2 sentences)?
What are the potential problems in the 2 code examples (list, max.
50 words in total)?
Install C compiler (if necessary), compile code on PC
Add automatic data source (rand()) to replace manual input
Instrument code for profiling (either by adding some timing
functionality or by using the integrated profiler, if available).
Report min/mean/max execution time per task 1/2/3
(100.000 iterations)
Hand in till Monday at midnight