Guarded Commands
Guarded Commands
Guarded Commands
Module - III
Concept of true nondeterminancy was proposed in
1970s by Dijkstra (with the help of guarded commands
as a means to simplify both program development and
program verification)
Types of execution 2 Types
Deterministic execution each part of the statement has
a specific order of execution. Uses the concept of
Deterministic finite automata
Nondeterministic execution is one where alternative
execution paths are possible. This condition arises
frequently in the execution of concurrent programs
Uses the concept of non deterministic automata
Eg: if there are n processes ready to execute in a
system, it is not always clear which one will be the
next to execute.
Guards : written as
If B is a guard (ie, condition)and S is a command (ie,
statement), then a guarded command, BS means
that statement S is enabled or ready for execution if
guard B is true.
Guarded if Statement
If Bi is a set of conditions and Si is a set of
statements, the guarded if is written as
If B1S1 || B2S2 |||| BnSn fi
which has the meaning that at least one of the
guards must be true and the execution is that of a
corresponding command.
It differs from the standard if-then and LISP cond
(where the statement to execute was the first one in
the sequence with a true condition.)
Guarded if is defined with true non determinancy
In guarded if arbitrarily choose any Si to
execute as long as Bi is true.
Guarded Repetition Statement
Generalization to the sequential while and
is similar to the guarded if : if Bi is a set of
guards and Si is a set of commands, the
guarded do is written as
do
B1S1 || B2S2 || || BnSn
od
Execution proceeds as follows:
if some guards Bi are true, one of the corresponding
Si is executed. The process repeats as long as some
guard is true. If no guard is true initially, the do is
skipped, which is similar to the behavior of the usual
while.
Non determinancy is introduced into the execution if
more than one guard is true at the same time.
Nondeterminancy- ways
Guard way to add nondeterminancy into
programming. Forms a good model for task
synchronization.
Synchronization form of nondeterminancy because
it is not always clear which task is the next one to
execute.
Example - ADA
The guarded if command in Ada is termed a select
statement and has the general form
select
when condition1 statement1
or when condition2 statement2
or when conditionn statementn
else statementn+1 ---optional else clause
end select;