Vous êtes sur la page 1sur 17

Real-time Embedded Systems- Lecture 03

Real-time Embedded Systems


Lecture 3
RT Systems Essentials
Part II
RTOS-Synchronizatiion
Prof. Dr. Amitava Gupta
Department of Power Engineering
Jadavpur University, Kolkata
Real-time Embedded Systems- Lecture 03

Synchronization Techniques in
RTOSs

Explicit Implicit

Semaphores Event flags


Cross Stimulation

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 data
from field sensors and another task computes the deviations from set values and switches
on or off the devices that cause heating/cooling (actuators).

timeframe

T1

Task T1 collects T2
data and stimulates
T2
Real-time Embedded Systems- Lecture 03

Advantages

This scheme ensures that T2 always processes the latest temperature data. Also it ensures
that 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. T 2 waits for
the 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. It


could 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:

increment ( a set operation)


decrement ( a reset operation)
Real-time Embedded Systems- Lecture 03

The temperature controller using a flag semaphore

timeframe

T1 T2

Task T1 collects
Task T2 sets the semaphore
data
reads the data No access for T1 now
resets the semaphore
Task T1 sets the semaphore,
writes the data No access for T2 now
resets the semaphore
Real-time Embedded Systems- Lecture 03

Using semaphores: the Deadly Embrace problem

Task T1 pseudocode Task T2 pseudocode

While (forever) While (forever)


{ {
set semaphore_A set semaphore_B
----------------------- -----------------------
------------------------ ------------------------
set semaphore_B set semaphore_A
----------------------- -----------------------
----------------------- -----------------------
reset semaphore_A reset semaphore_B
reset semaphore_B reset 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
After having run for sometime, A tries to set the same semaphore and is blocked.

Now, if B is ready, it will run-displacing A, a higher priority task.

A possible solution would be


to increase the priority of C to
S level more than that of B, till it
resets the semaphore.

S
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 initial


value 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 reaches
zero, 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 semaphores


Burst Mode

Task priority A > B > C > D


A is blocked
when it tries
to set the
semaphore
s r

s r
s
s

B is blocked r When D resets the semaphore, A gets


when it tries to
D sets the It even though other tasks had posted
set the semaphore
semaphore & is C is blocked requests before.
pre-empted by B when it tries to
set the semaphore
Real-time Embedded Systems- Lecture 03

Execution Profile: Burst and FIFO mode semaphores


FIFO Mode

Task priority A > B > C > D

s r
s
s 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 processes
particularly in a producer-consumer environment.

………………..

A Producer sets one or more bits in a pre-


determined manner
A consumer is activated when a certain
combination is satisfied. This is determined
by the application
Real-time Embedded Systems- Lecture 03

The Multi-processor support

Semaphores and Event Flags may be used to share resources across processors
in a multi-processor environment. Such structures are located in the shared global
memory .

The usage is usually implementation specific with the following basic properties:

Sharing is explicit. A shared synchronization structure has pre-defined processors


associated 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 any
task. 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!

Vous aimerez peut-être aussi