Upload
parthasarothisikder79
View
208
Download
1
Embed Size (px)
Citation preview
Real-time Embedded Systems- Lecture 03
Real-time Embedded SystemsLecture 3
RT Systems EssentialsPart II
RTOS-SynchronizatiionProf. Dr. Amitava Gupta
Department of Power EngineeringJadavpur University, Kolkata
Synchronization Techniques in RTOSs
Explicit Implicit
Cross StimulationSemaphores Event flags
shared Single proc.shared Single proc.
flag counting
burst fifo
Real-time Embedded Systems- Lecture 03
Explicit Task Synchronization by Cross Stimulation
The synchronizing processes know each others task id.
Example
Consider a system comprising two tasks. One task samples a set of temperature datafrom field sensors and another task computes the deviations from set values and switcheson or off the devices that cause heating/cooling (actuators).
timeframe
T1
T2Task T1 collectsdata and stimulatesT2
Real-time Embedded Systems- Lecture 03
Advantages
This scheme ensures that T2 always processes the latest temperature data. Also it ensuresthat the task T2 does not access the shared resource when T1 writes on it.
Disadvantages
Depend on how the cross stimulation is implemented. If it is blocking, i.e. T2 waits forthe stimulation from T1, then the scheme has several problems:-
T2 should have a shorter period that T1
If T1 gets blocked, T2 gets blocked too.
What happens if the number of scanner tasks is more than one?
Real-time Embedded Systems- Lecture 03
Using semaphores
A semaphore is a structure that is used to guard access to a shared resource. Itcould be a flag or a multi-valued integer (counting semaphore)
As a flag, the following operations are possible on a semaphore:
set reset
As a counting semaphore, these are:
incrementdecrement
( a set operation)( a reset operation)
Real-time Embedded Systems- Lecture 03
The temperature controller using a flag semaphore
timeframe
T1 T2
Task T1 collectsdata
Task T1 sets the semaphore,writes the data resets the semaphore
No access for T2 now
Task T2 sets the semaphorereads the dataresets the semaphore
No access for T1 now
Real-time Embedded Systems- Lecture 03
Using semaphores: the Deadly Embrace problem
Task T1 pseudocode
While (forever){ set semaphore_A ----------------------- ------------------------
set semaphore_B----------------------------------------------reset semaphore_Areset semaphore_B
}
Task T2 pseudocode
While (forever){ set semaphore_B ----------------------- ------------------------
set semaphore_A----------------------------------------------reset semaphore_Breset semaphore_A
}Task T1 blocks here Task T2 blocks here
Real-time Embedded Systems- Lecture 03
Moral of the story
•It is the application programmers responsibility to design applications in such a manner that a deadly embrace is avoided. Use of proper tools is recommended.
•The complexity of the problem increases with the number of shared resources.
•As a thumb rule, access to resources should be kept short. That is to say, the application should block access to other tasks just when it is required, and release it as soon as the job is over.
The ultimate bottomline ( on a lighter note, though)
is peace and is war !
Real-time Embedded Systems- Lecture 03
The problem continues…. Priority inversion
Let us suppose, we have 3 tasks, A, B and C which share a semaphore amongst them. The priority sequence is A > B > C.
Let us suppose that C runs for some time, having set the semaphore, when it is pre-empted by A
S
After having run for sometime, A tries to set the same semaphore and is blocked.
S
Now, if B is ready, it will run-displacing A, a higher priority task.
A possible solution would beto increase the priority of C tolevel more than that of B, till itresets the semaphore.
Real-time Embedded Systems- Lecture 03
The counting semaphore
Basic operation
Each semaphore is associated with a MAX_COUNT value and is initialized to an initialvalue which is usually zero.
Each set or increment will increase the COUNT by unity. When the COUNT reaches MAX_COUNT, the task trying to set will block.
Each reset or decrement will decrease the COUNT by unity. When the COUNT reacheszero, the task trying to reset will block.
This is ideal for a producer-consumer environment
Real-time Embedded Systems- Lecture 03
The counting semaphore in a producer-consumer environment
We assume that the Scanner task writes data on to a buffer which is read by the Controller.Thus the Scanner is the producer here and the Controller is the consumer.
We further assume that the buffer can hold 2 data elements.
We use a counting semaphore with a MAX_COUNT value of 2.
The execution profile of the producer and the consumer
Chalk and board
Real-time Embedded Systems- Lecture 03
The Burst and FIFO mode of operation of semaphores
Burst Mode: In this mode, if a number of tasks are in the blocked state waiting to set the semaphore, the task with the highest priority gets unblocked first.
FIFO Mode: In this mode, if a number of tasks are in the blocked state waiting to set the semaphore, they get unblocked in the sequence they posted a request for set.
Real-time Embedded Systems- Lecture 03
Execution Profile: Burst and FIFO mode semaphoresBurst Mode
Task priority A > B > C > D
s
s
s
s
r
r
r
D sets thesemaphore & ispre-empted by B
B is blockedwhen it tries toset the semaphore
C is blockedwhen it tries to set the semaphore
A is blocked when it tries to set the semaphore
When D resets the semaphore, A gets It even though other tasks had posted requests before.
Real-time Embedded Systems- Lecture 03
Execution Profile: Burst and FIFO mode semaphoresFIFO Mode
Task priority A > B > C > D
s
s
s
s
r
r
r
Task B gets the semaphore now.
Real-time Embedded Systems- Lecture 03
The Event Flag
Basic operation
An Event Flag is a single byte or a two byte integer which is used to synchronize processesparticularly in a producer-consumer environment.
A Producer sets one or more bits in a pre-determined manner
………………..
A consumer is activated when a certaincombination is satisfied. This is determinedby the application
Real-time Embedded Systems- Lecture 03
The Multi-processor support
Semaphores and Event Flags may be used to share resources across processorsin a multi-processor environment. Such structures are located in the shared globalmemory .
The usage is usually implementation specific with the following basic properties:
Sharing is explicit. A shared synchronization structure has pre-defined processorsassociated with it.
Semaphores (both flag and counting) are almost always in the FIFO mode.
Real-time Embedded Systems- Lecture 03
The Last Word
Initialization of Synchronization structures is Application Programmer's responsibility.
An Application has to create and initialize these structures before they are used by anytask. The OS provides the necessary system calls.
Usually, this is done by a startup task which starts first when the application starts and then other tasks are started.
Goodbye till next week. Please do not forget to bring a Calculator!
Real-time Embedded Systems- Lecture 03