What is an operating system?

Download Report

Transcript What is an operating system?

Real-time Operating System
and RTLinux
金仲達教授
清華大學資訊工程學系
(slides adapted from Prof. T.Y. Huang)
Overview
Operating Systems
 Real-Time Operating Systems
 RTLinux

Real-time OS1
What is an operating system?
An operating system is the “permanent”
software that controls/abstracts hardware
 User applications can thus be simpler and
device-independent

User Applications
virtual machine interface
Operating System
physical machine interface
Architecture
Real-time OS2
Multi-tasking Operating Systems
Manages resources and processes to support
different user applications
 Provides Applications Programming Interface
(API) for user applications

User Application
User Application
Operating System
Architecture
Real-time OS3
General-Purpose Operating System
User program
Executable binary
user
Compiler
Linker
compiler
user mode
kernel
System library
OS interface
Operating system
Device drivers
OS
Architecture
Hardware
Real-time OS4
2-layered Architecture
Applications
Operating
System
Kernel
Graphics
Subsystems
Scheduler
I/O Manager
Network
Drivers
Device
Drivers
Graphics
Drivers
Hardware
Real-time OS5
3-layered Architecture
Processes
Operating
System
Kernel
Applications
Applications
Graphics
Subsystems
Scheduler
I/O Manager
Network
Drivers
Device
Drivers
Graphics
Drivers
Hardware
Real-time OS6
3-layered with Microkernel
Applications
Processes
Graphics
Subsystems
Applications
I/O Manager
Microkernel
Processes
Network
Drivers
Device
Drivers
Graphics
Drivers
Hardware
Real-time OS7
Embedded System Devices

3-layered device

Palm, PocketPC

2-layered device

XBox
User Applications
Embedded OS
Application
Hardware
Hardware
Real-time OS8
Device Driver
Device driver的唯一目的就是要將所有的硬
體隱藏.
 他應該是整套軟體中唯一能和硬體溝通的窗
口.
 他能直接讀取或寫入特定裝置控制及狀態暫
存器, 如果裝置發生中斷,那ISR也要整合到
裝置驅動程式.

Real-time OS9
Importance of Operating Systems

System API are the only interface between
user applications and hardware


OS code cannot allow any bug


API are designed for general-purpose, not
performance driven (e.g. network applications)
Any break (e.g. invalid access) causes reboot
The owner of OS technology controls the
software & hardware industry
Real-time OS10
Overview
Operating Systems
 Real-Time Operating Systems
 RTLinux

Real-time OS11
What is a real-time OS?

An operating system with real-time features


real-time operating system (general-purpose)
real-time embedded system (device-specialized)
“Real-time” does not mean speed, but keeping
deadlines
 Overall deterministic behavior
 Guaranteed – typically short – response/reaction
times

Real-time OS12
Soft vs. Hard Real-Time

Soft real-time requirements:



Missing the deadline is unwanted, but is mot
immediately critical
Examples: multimedia streaming
Hard real-time requirements:


Missing the deadline results in a fundamental failure
Examples: nuclear power plant controller, multimedia
streaming
Real-time OS13
Scheduler

A component in OS that decides which task in
the ready queue gets the CPU
new
task
ready
scheduler
signal
events
waiting
running
terminating
tasks
Real-time OS14
Key Components in RTOS
Process/Thread management – scheduler
 Resource management – synchronization
 FCFS scheduling algorithm





no preemption  no flexibility
T1 = (0, 4, 10) == (Ready, Execution, Deadline)
T2 = (1, 2, 4)
Priority scheduling algorithms


preemption allowed  complexity
resource blocking
Real-time OS15
Variants of RTOS

Pure real time OS
RT-Application
RTOS
Hardware
Real-time OS16
Pure RTOS
Especially designed for real-time requirements
 Completely real-time compliant
 Often usable for simple architecture
 Advantage: no or little overhead


Computing power, memory
Disadvantage: limited functionality
 Example: eCos, Nucleus, pSOS, VxWork, QNX,
OSE, Lyra

Real-time OS17
OS Real-Time Extensions
Extension of an OS by real-time components
 Cooperation between RT- and non-RT parts
 Advantages: rich functionality
 Disadvantage:




No general real-time ability
Computing and memory resources
Example: RT-Linux, Solaris, Windows NT
Real-time OS18
OS Real-Time Extensions
Applications
RT Applications
Standard OS
RT
extension
Hardware
Real-time OS19
Windows CE

Manufacturer: Microsoft


PDA, Industrial embedded systems, game consoles
Characteristics





Win32 API
Well-developed tool chains (Visual Studio)
Most well-known 32 bit platforms with MMU
Typical memory requirements : a few MB’s
Relative high royalty
Real-time OS20
Embedded Linux

Open source


Almost free: porting cost
Massive development support from community
Code quality
 Robust
 Brand name
 Internet platform
 IBM will invest 1 Billion per year for Linux

Real-time OS21
A Typical Embedded Linux
 Browser
Pocket
Word
ICQ
JavaVM
SDK
E-mail
MP3
PIM
ICA
GTK+ & GDK GUI
DDK
GW32
MultiLanguage
Embedded Linux OS
Screen Phone &
XC
WBT
PDA
Web Pad
Real-time OS22
Embedded and Real-Time Linux
Solution Providers
Lynx -- Blue Cat Linux
 MontaVista -- Hard Hat Linux
 Lineo -- Embedix Linux
 FSMLabs -- RTLinux
 Zentropix -RealTime Linux
 Coollogic -- On-Channel Linux
 Redhat
 IBM

Real-time OS23
Overview
Operating Systems
 Real-Time Operating Systems
 RTLinux

Real-time OS24
Linux Kernel

Design focus: application throughput




Linux use interrupt off critical section management
Linux kernel is non-preemptable
Linux compromises the timely selection of real-time
processes for throughput
Linux allow nested interrupts (depends on hardware)
Real-time OS25
Hybrid Linux Kernel

RTLiux (work around the problem)




run Linux as the idle task
real-time threads are executing in a real-time kernel
interrupt off/on operations by Linux are emulated by
the lower level kernel
complex and difficult for application development
Real-time OS26
RTLinux Architecture
Init
Bash
Xterm
System calls
Interrupt
or
polling
Real time
task
Linux
Kernel
Real time
task
Drivers
I/O
RT-Linux Scheduler
I/O
Interrupt
Interrupt
Hardware
Real-time OS27
Writing RTLinux programs
Writing modules:
 Linux module: an object file w/o main(), but use:




init_module(): called when module is inserted into
kernel
cleanup_module(): called before module is removed
Typically, init_module() registers a handler with kernel,
or replaces one of kernel function
Example:



convert module.c into a module by:
$ gcc -c {SOME-FLAGS} my_module.c
insert created module, module.o, by:
$ insmod module.o
Removes the module by: $ rmmod module
Real-time OS28
RTLinux program example

The program executes once every second, and
during each iteration it will print 'Hello World‘
#include <rtl.h>
#include <time.h>
#include <pthread.h>
pthread_t thread;
int init_module(void){
return pthread_create(&thread, NULL,
thread_code, NULL);
}
void cleanup_module(void){
pthread_delete_np(thread);
}
Real-time OS29
RTLinux program example
void * thread_code(void){
struct sched_param p;
p.sched_priority = 1;
pthread_setschedparam(pthread_self(),
SCHED_FIFO, &p);
pthread_make_periodic_np(pthread_self(),
gethrtime(), 1000000000);
while (1)
{ pthread_wait_np();
rtl_printf("Hello World\n"); }
return 0;
}
Real-time OS30
Compiling and executing

Can use a 'Makefile'.

Locate and copy the rtl.mk file into the same
directory as your hello.c and Makefile
Compile the code with

include rtl.mk
all: hello.o
clean: rm -f *.o
hello.o: hello.c $(CC) ${INCLUDE} ${CFLAGS} –c
hello.c
$ make

Use 'rtlinux' to insert: (must be 'root')
$ rtlinux start hello
Real-time OS31
Interprocess communication
Only that part of a program which requires
precise time restrictions should be written as a
real-time process
 Others can be written and executed in user
space



User spaces processes are often easier to write,
execute and debug than real-time threads
There should be a way to communicate between
user space Linux processes and real-time thread

Consider the most important and common way of
communication, the real-time FIFO
Real-time OS32
Real-time FIFO

Unidirectional queues:


Usually, one end processes is the real-time thread and
the other is a user space process
Are character devices (/dev/rtf*) with a major
number of 150

Real-time threads uses integers to refer to each FIFO
(for example, 2 for /dev/rtf2)
There is a limit to the number of FIFOs
 Functions, rtf_create(), rtf_destroy(), rtf_get(), rtf_put()
for handling the FIFOs


The Linux user process sees the real-time FIFOs as
normal character devices

Functions, open(), close(), read() and write() can be
used on these devices
Real-time OS33
IPC example program

A C program (filename pcaudio.c) to play music
(of just two tones) through the PC speaker
For now, we need write to the character device
/dev/rtf3
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define DELAY 30000
void make_tone1(int fd)
{
static char buf = 0;
write (fd, &buf, 1);
}

Real-time OS34
IPC example program
void make_tone2(int fd)
{
static char buf = 0xff;
write (fd, &buf, 1);
}
main()
{
}
int i;
int fd=open("/dev/rtf3", O_WRONLY);
while (1)
{
for (i=0;i<DELAY;i++);
make_tone1(fd);
for (i=0;i<DELAY;i++);
make_tone2(fd);
}
Real-time OS35
A real-time version
#include <rtl.h>
#include <pthread.h>
#include <rtl_fifo.h>
#include <time.h>
#define FIFO_NO 3
#define DELAY 30000
pthread_t thread;
int init_module(void)
{
return pthread_create(&thread,
NULL, sound_thread, NULL);
}
void cleanup_module(void)
{
pthread_delete_np(thread);
}
Real-time OS36
A real-time version
void * sound_thread(int fd)
{
static char buf = 0;
pthread_make_periodic_np(pthread_self(),
gethrtime(), 500000000);
while (1)
{
pthread_wait_np();
buf = (int)buf^0xff;
rtf_put(FIFO_NO, &buf, 1);
}
return 0;
}
Real-time OS37