Upload
annis-mitchell
View
229
Download
0
Tags:
Embed Size (px)
Citation preview
Embedded Systems
OS
Reference Materials• The Concise Handbook of
Real-Time Systems TimeSys Corporation
Real-time Embedded Systems
Embedded Systems
Real-time Systems
Real-time Embedded Systems
Real-time System
• Function correct
• time correct
Hard realtime vs. Soft real-time• Soft-realtime
– accept delayed response
• Hardware– Ensure response time
response time deadline
response time deadline
Hard realtime system may not be a fast system
RTOSKey Features
• Reliability
• Predictability
• Performance (Fast , Small)
• Compactness (xMB ~ xxxKB)
• Scalability(Network, FS, IPC, GUI…)
Reliability of High Available System
Reliability Down Time / Year Applications
99.9% 1~9h Desktop System
99.99% 0~1h Enterprise Server
99.999%0~5min
Telecommunication Server
99.9999%0~31s
Telecommunication Switcher
Why using OS?
• Resource Management (Mem, IO, Time)
• Multi-task
• Reliable
• Standard System API
• Complex Applications
What is Task?
Func_B(){
}
Task B
Func_A(){
}
Task Aint Func_XX(){ while(1) { do_work_1(); do_work_2(); } return TASK_DEAD;}task looks like a simple C function
How Task is Running?
Func_B(){
}
Task B
Func_A(){
}
Task A
CPU
Static Task and Dynamic Task
Memory
Task ATask A
External Storage
Task D Task
C
Remote SystemRemote System
Task ETask F
LoaderLoader
State of Threads/Tasks
Ready
Run
Blocked
Resource Obtained
Highest Priority Task
Request For Resource
Implementation Details
Task A Task B
TCB A TCB B
ISR (Software/Hardware)ISR (Software/Hardware)
1. Execute System Call2. Copy Registers into TCB3. Schedule
a) Update Task Statusb) Select Task with the
Highest Priority
ISR means Interrupt Service Routine
System_call()
System_call()
1
3
CPU PC
R0
R1
RN
…
PSR
2
4
TCB (Task Control Block)
PC
R0
R1
RN
…
PSR
Task StatusTask Status
Task PriorityTask Priority
Semaphore
• Binary Semaphore
• Counting Semaphore
• Mutex Semaphore
Look like normal variable.Accessing (changing) its value is monitored by the OS
ValueValueAcquire(S)
Release(S)
SS
Binary Semaphore
1 0
Acquire()set Value=0
Release()set Value=1
• Task Blocks when acquiring semaphore with value 0• Any task can release semaphore (can be release more
than once)
Init() with value 1
Init with value 0
Counting Semaphore
N 0
Acquire()- -Value
Acquire()--Value==0
• Task Blocks when acquiring semaphore with value 0• Any task can release semaphore (can be release more than once)
Release()++Value
Release()++Value
Init() with value N
Init() with Value 0
Mutex Semaphore
N0
Acquire()++Value
Acquire()set Value=1
Release()--Value
Release()--Value==0
Init() with value 0
Task Blocks when acquiring semaphore with non-zero valueOnly task who locked the semaphore can release it
BufferBuffer
Use of Binary Semaphore
Task A
Pre-processing
Task B
Post-processing
0/1
Binary Semaphore
Acquire()set value=0blocks at value==0
Release()set value=1
Task A won’t be blocked on it,
Task A can be ISR
B blocks itself until obtain the data
Use of Counting Semaphore
Task A
Pre-processing
Task B
Post-processing
N
Counting Semaphore
Acquire()value—blocks at value==0
Release()value++
Task A won’t be blocked on it, it can be ISR
Task A issue batch of semaphores after pre-processing and block itself. then task B continue data processing
Use of Mutex Semaphore
N
Task_A(){ Acquire(S); Access_Share_Mem(); Release(S);}
Shared Memory
Task_B(){ Acquire(S); Access_Share_Mem(); Release(S);}
Task_B(){ Acquire(S); Access_Share_Mem(); Release(S);}
Use of Mutex Semaphore
N
Task_A(){ Acquire(S); Access_Share_Mem(); Func_X(); Release(S);}
Func_X(){ Acquire(S); Access_Share_Mem(); Func_Y(); Release(S);}
Func_Y(){ Acquire(S); Access_Share_Mem(); Release(S);}
Shared Memory
Same Task won’t block itself when try
locking the same lock
Other Synchronization Methods
• Interrupt Locking– Enable Interrupt– Disable Interrupt
• Preemption Locking– Enable Scheduling– Disable Scheduling
Use of Interrupt Locking
ISR(){ Access_Share_Mem(); }
Shared Memory
Task_B(){ Disable_IRQ(); Access_Share_Mem(); Enable_IRQ();
Data Processing}
IRQ disable time should be very short
Need to add protection code to avoid access conflict of shared memory
Use of Interrupt Locking (1)ISR_1(){ Access_Share_Mem();}
Shared Memory 1
Task_B(){
}
Shared Memory 2
Shared Memory N
ISR_2(){ Access_Share_Mem();}
ISR_N(){ Access_Share_Mem(); }
…
…
Use of Interrupt Locking (2)
Task_B(){ for (i=1; i<=N; i++) { Disable_IRQ(); Access_Memory(i); enable_IRQ(); Data_Proc(); }}
ISR_1(){ Access_Share_Mem();}
Shared Memory 1
Shared Memory 2
Shared Memory N
ISR_2(){ Access_Share_Mem();}
ISR_N(){ Access_Share_Mem();}
…
…
Use of Preemption Locking
Task_A(){ Disable_Sched(); Access_Share_Mem(); Enable_Sched();
Data Processing}
Shared Memory
Task_B(){ Disable_Sched(); Access_Share_Mem(); Enable_Sched();
Data Processing}
Scheduler disable time should be very short
Soft Timer
Execution Start Time 1Execution
Start Time 1
Execution Start Time 2Execution
Start Time 2
Execution Start Time 3Execution
Start Time 3
Execution Start Time 3Execution
Start Time 3
WorkWork
Work
WorkWork
Work
WorkWork
Work
WorkWork
Work
RTC ISR
Run taskswhose start time has reached
Timer Counter(variable)
++