Introduction to RTOS
About SwiftACT
• A Technology services startup company
o
Under establishment
• Areas of specialties:
o
o
Mobile telecommunication services development
Embedded systems development
• Types of services:
o
o
o
o
Consultation
Managed services
Sourcing
Training
Amr Ali Abdel-Naby@2010
Introduction to RTOS
About Me
• Graduated 2004
o
ECE, ASU: 5 yrs distinction
• 5+ years in embedded systems development
o
SDLC, Apps, MW, DD, Porting, ...
• 3+ years in SW engineering
o
PSP, CMMI, Systematic reuse, ...
• 3+ years in SW testing
o
IBM certified, ISTQB certified, ...
Amr Ali Abdel-Naby@2010
Introduction to RTOS
Copyright
• Materials in this course is the property of Amr Ali Abdel-Naby.
• Reproduction or transmission of the materials in any manner
without the copyright owner permission is a law violation.
Amr Ali Abdel-Naby@2010
Introduction to RTOS
Outline
•
•
•
•
•
•
Introduction
Tasks
Semaphores
Message Queues
Other Kernel Objects
Other Kernel Services
Amr Ali Abdel-Naby@2010
Introduction to RTOS
Outline
•
•
•
•
•
•
Introduction
Tasks
Semaphores
Message Queues
Other Kernel Objects
Other Kernel Services
Amr Ali Abdel-Naby@2010
Introduction to RTOS
Critical Sections: Resource Sharing
• Resources used by 1 task at a time, not interrupted
o
Some devices, shared data, algorithms (non-reentrant)
• Critical Section == Code for serially reusable resource
• If 2 tasks enter same critical section simultaneously
Catastrophe
• Mechanisms to avoid such collisions using an RTOS
o
o
o
o
Resource monitor tasks
Semaphores
Mutexes
…
Print
“I am #1”
I am #I am #21
Print
“I am #2”
Amr Ali Abdel-Naby@ 2009
Real-Time Operating Systems
Defining Semaphores
• Binary Semaphore
o
o
Can be used as a lock to protect a critical region
In some RTOSes, need to build your own:
Restrict counting semaphore to ≤ 1
• Counting Semaphore
o
o
o
Can be used to protect pools of equivalent resources
Keeps track of number of free equivalent units of resource
Available in many RTOSes
• Mutex
o
o
Specialized kind of binary semaphore, for mutual exclusion
purposes
Available in many modern RTOSes
Amr Ali Abdel-Naby@ 2009
Real-Time Operating Systems
Binary Semaphore
Release
(value = 1)
Initial
value = 1
Initial
value = 0
Available
Unavailable
Acquire
(value = 0)
Amr Ali Abdel-Naby@2010
Introduction to RTOS
Counting Semaphore
Release
(count++)
Release
(count = 1)
Initial
count > 1
Initial
count = 0
Available
Acquire
(count--)
Amr Ali Abdel-Naby@2010
Unavailable
Acquire
(count = 0)
Introduction to RTOS
Mutual Exclusion Semaphore (Mutex)
Acquire recursive
(lock count++)
Acquire
(lock count = 1)
Initial
Lock count = 0
Locked
Release recursive
(lock count--)
Amr Ali Abdel-Naby@2010
Unlocked
Release
(lock count = 0)
Introduction to RTOS
Mutexes vs. Semaphores
•
•
•
•
Mutex ownership
Recursive locking
Task deletion safety
Priority inversion safety
o
o
Ceiling protocol
Inheritance protocol
Amr Ali Abdel-Naby@2010
Introduction to RTOS
Semaphores: 4 Traditional Usages
•
Resource limit … or use a Resource Monitor Task
o
o
o
•
Critical Section … or use a Resource Monitor Task or Mutex
o
o
o
•
Initial count = 1
wait_sem to enter critical section
signal_sem to leave critical section
Wait / Signal … or use a Message
o
o
o
•
Initial count = # of equivalent resources
wait_sem to gain access to a resource
signal_sem to release a resource
Initial count = 0
wait_sem to wait
signal_sem to signal
Credit tracking … or use Messages
o
o
o
Initial count = 0
signal_sem to increment (Credit)
wait_sem to consume (Debit)
Amr Ali Abdel-Naby@ 2009
Real-Time Operating Systems
Examples: Wait/Signal, Credit
Tracking
HW
Device 1
ISR 1
SW Task
• Wait/Signal
o
o
o
Task waits on semaphore.
Interrupt task (or ISR) signals to semaphore when it runs.
semaphore counts don’t accumulate.
• Credit tracking
o
o
Semaphore counts can accumulate.
Count represents interrupts not yet handled by the task.
• Alternative: Messages
HW
Device n
Amr Ali Abdel-Naby@ 2009
ISR n
Real-Time Operating Systems
SW Task
Some Problems with Semaphores
• Processor boundaries
• Difficult to debug (all tokens identical)
• When used for multiple equivalent resources ...
o
When task is allowed access to a resource, which one is it ??
• Interrupt tasks and ISRs can not take a token (get the
semaphore).
• Task entrapments / Priority inversions
Amr Ali Abdel-Naby@ 2009
Real-Time Operating Systems
Typical Semaphore Operations
• Semaphores creation and deletion
o
o
Create: binary, counting, or mutex
Delete
• Acquiring and releasing semaphores
o
o
Acquire: wait for ever, wait with timeout, or do not wait
Release
• Clearing semaphore task-waiting lists
o
Flush
• Obtaining semaphore information
o
o
Show info
Show blocked tasks
Amr Ali Abdel-Naby@2010
Introduction to RTOS