Tasks, Semaphores and Message Queues
Tasks, Semaphores and Message Queues
Tasks, Semaphores and Message Queues
Queues
• Real-time embedded systems are reactive systems.
• Their primary purpose is to respond to or react to
signals from their environment.
• Finite-state machines (FSMs) are a powerful
mathematical and graphical tool in specifying the
behavior of reactive systems.
• An FSM is an abstract computation model that is used
to design both software and sequential logic circuits.
• a state machine is a device that stores the context of
an object at a given time.
• It operates on input events to change the context
and/or cause an action to take place.
• The context is called a state, which captures the
relevant aspects of the object’s history.
• A change from one state to another is called a
transition.
• A state transition occurs because of the occurrence
of some triggering events or conditions.
• An FSM can be represented by a directed graph
called a state diagram.
• In many FSMs, it is necessary to classify states into
initial states, final states, and intermediate states.
• Normally, an initial state is graphically marked with
an incoming arrow, while a final state is represented
with two circles.
• State Diagram of a Safe
• Consider a safe that is locked with a code 2-0-1-7.
• The states that we are interested in are as follows:
• State q0: locked, with no meaningful input.
• State q1: locked, with an input sequence that ends
with a “2.”
• State q2: locked, with an input sequence that ends
with a “2-0.”
• State q3: locked, with an input sequence that ends
with a “2-0-1.”
• State q4: unlocked (after an input sequence that
ends with a “2-0-1-7.”
• A state machine may have outputs corresponding to
each transition. There are two types of FSMs that
generate outputs:
• Moore machines
• Mealy machines
• A Moore machine is a model whose output depends
only on the current state.
• A Mealy Machine is a model whose output depends
on the current input as well as the current state.
• Moore Machine model of a Vending Machine:
• Consider a vending machine that sells candy bars for
20 cents each.
• Assume that the machine only accepts coins of 5
cents, 10 cents, and 25 cents.
• When coins of 20 cents or more are inserted into the
machine, it releases a candy bar and ejects changes.
• To model the behavior of the vending machine, we
need to find out all possible
• states and inputs and then decide transitions among
the states.
• All possible input events are as follows:
• 5c (insert a coin of 5 cents)
• 10c (insert a coin of 10 cents)
• 25c (insert a coin of 25 cents)
• signal(S)
• {
• S++;
• }
• Operating systems often distinguish between counting
and binary semaphores.
• The value of a counting semaphore can range over an
unrestricted domain.
• The value of a binary semaphore can range only
between 0 and 1.
• consider two concurrently running processes: P1 with
a statement S1 and P2 with a statement S2.
• Suppose we require that S2 be executed only after S1
has completed.
• We can implement this scheme readily by letting P1
and P2 share a common semaphore synch, initialized
to 0, and by inserting the statements.
• S1;
• signal(synch);
• in process P1
• the statements
• wait(synch);
• S2;
• in process P2.
• Suppose that P0 executes wait(S) and then P1
executes wait(Q)
• When P0 executes wait(Q), it must wait until P1
executes signal(Q).
• Similarly, when P1 executes wait(S), it must wait until
P0 executes signal(S).
• Since these signal() operations cannot be executed,
P0 and P1 are deadlocked.
• We say that a set of processes is in a deadlock state
when every process in the set is waiting for an event
that can be caused only by another process in the
set.
Priority Inversion
• A higher-priority process needs to read or modify
kernel data that are currently being accessed by a
lower-priority process.
• Assume we have three processes, L, M, and H, whose
priorities follow the order L < M < H.
• Assume that process H requires resource R, which is
currently being accessed by process L.
• Ordinarily, process H would wait for L to finish using
resource R.
• However, now suppose that process M becomes
runnable.
• thereby preempting process L.
• Indirectly, a process with a lower priority—process M
—has affected how long process H must wait for L to
relinquish resource R.
• This problem is known as priority inversion.
• Typically systems solve the problem by
implementing a priority-inheritance protocol.
• According to this protocol, all processes that are
accessing resources needed by a higher-priority
process
• inherit the higher priority until they are finished with
the resources in question.
• When they are finished, their priorities revert to their
original values.
• In the example above, a priority-inheritance protocol
would allow process L to temporarily inherit the
priority of process H.
• Thereby preventing process M from preempting the
execution of L.
• When process L had finished using resource R, it
would relinquish its inherited priority from H and
assume its original priority.
• Because resource R would now be available, process
H—not M—would run next.
• Resource Sharing
• A task may need some resources in addition to a
processor in order to make progress.
• When a task wants to access the shared data
guarded by a semaphore R,
• it must lock the semaphore first and then enters the
critical section of the code where it accesses the
shared data.
• In this case, we say that the task requires the
resource R for the duration of the critical section.
• Suppose that there are m types of resources in a
system, namely R1, R2, …, Rm
• there are Vi indistinguishable units of Ri, i=1, 2, …,m.
• When a task requests Ni units of a resource Ri, it
executes a lock to request them.
• The action is denoted by L(Ri, Ni).
• When the task no longer needs a resource, it executes
an unlock to release the resource, denoted by U(Ri,
Ni).
• The code segment of a task that begins at a lock and
ends at a matching unlock is called a critical section,
which cannot be concurrently executed by multiple
processes.
• When a task is denied a lock request, the task is in
blocked state.
• The task which enters blocked state is removed from
ready queue.
• It again joins the ready queue when the kernel grants
the lock request of that task.
• This is known as multi-process resource access
synchronization.
Message queue:
• A message queue is a buffer-like object through
which tasks and ISRs send and receive messages to
communicate and synchronize with data.
• A message queue is like a pipeline.
• It temporarily holds messages from a sender until the
intended receiver is ready to read them.
• When a message queue is created, it is assigned a
queue control block.
• A queue has name, ID, memory buffers, queue
length, message length.
• Message Queue can be most appropriately
used for
• a temperature value from a sensor.
• a bitmap to draw on a display.
• a text message to print to an LCD.
• a keyboard event.
• a data packet to send over the network.
• Messages can be read from the head of a message
queue in two different ways:
• destructive read, and
• non-destructive read.
• In a destructive read, when a task successfully
receives a message from a queue,
• the task permanently removes the message from the
message queue’ s storage buffer.
• In a non-destructive read, a receiving task peeks at
the message at the head of the queue without
removing it.
• The message queue can be classified into
• 1. Interlocked, one-way data communication.
• 2. Interlocked, two-way data communication.
• Messages can be sent in LIFO and FIFO order.
• When sending messages, a kernel typically fills a
message queue from head to tail in FIFO order.
• Develop a state-diagram based model for a
tea/coffee vending machine subject to the following
constraints.
• Each cup of tea/coffee costs 5 rupees.
• Develop a state-diagram based model for a
tea/coffee/milk vending machine subject to the
following constraints.
• Each cup of tea/coffee costs 5 rupees.
• Milk costs 10 rupees.(10 rupee coin is available)
• If a 5 rupee coin is inserted and milk option is
selected, then refund the coin.
Assumptions
• At start the machine asks the user for Tea/Coffee.
• After this choice the machine displays “insert 5 rupee
coin .”Then wait.
• Once user drops a coin, based on the option dispense
tea or coffee.
• Once the user selects the option get an option for
sugar/sugarless.
• If the coin is less than 5 rupee. The change is
returned back.
States
• Ready
• Tea/Coffee
• Coin
• Change
• Sugar
• Dispense tea.
• Dispense coffee.
Events
• Check:1
• Coin_insrt: 2
• Sugar_opt: 3
• Tea/coffee: 4/5
• Wrong_coin: 6
• Dispensed/change: 7
• Reset: 8
State diagram
1 2 Tea/Coff 3
Ready Coin sugar
ee
6 4
5
7
Dispense Dispense
Change
Tea Coffee
7 7