VI.11 Exercises

Exercises [Slide 264]

Exercise 32.  

Given the following Tempura programs and determine whether they are executable or not. Try to change those that are not executable in such a way that they become executable.

Exercise 33.  

Exercise 34.  

Change the Liquid Tank V2 example so that the outlet pipe can be controlled by the operator via an interrupt C, i.e., when C = 0 then V 2 should be closed. The controller unit should maintain the level of the liquid in the tank within the specified limits while honouring the operator interrupts.

Exercise 35.  

Exercise 36.  

Complete the Alternating Bit Protocol (ABP) program for lossy Data and Ack channel.

Exercise 37.  

Write a Tempura program that models the following supermarket: A supermarket consists of a buying area, where the customers collect the items they wish to purchase, and one or more pay tills, where the customers queue up to pay for those items. Each pay till generally has only one queue of customers. A customer can join any queue and move freely among queues (because all pay tills generally offer the same service). A customer may only join the rear of a queue.

The basic properties of the supermarket and its pay tills and customers are summarised as follows:

Location of clients:

A customer may be in any of the following locations:

Movements of clients.

A customer may make the following moves:

Status of the pay till.

There are three states:

A pay till which is not closed may additionally be in either of the following sub states:

Operation of pay tills:

Status of supermarket:

Operation on supermarket:

Exercise 38.  

Write a Tempura program that models one or more lifts, with interactive input of (simulated) button presses.

The Tempura program should have the following parameters:

The lift shall respond in a timely fashion to the following button presses:

If a lift is moving in a given direction, then it continues to move in that direction until there are no more unserviced requests in that direction. But it shall stop at any floors along the way that have either unserviced CallRequests for the same direction or unserviced GotoRequests. That is, suppose the lift is moving in the direction of floor k to service a request for that floor. If, on the way to floor k, the lift is about to pass a floor j (not equal to k) such that there is an unserviced call request to floor j, then the lift shall stop at floor j if and only if either

The design should make the following assumptions:

(the unit interval consists of two states, i.e., it is skip)

Model the lift as a machine with states and transitions.

The lift can be in the following states:

The lift starts in state [stop, open] and the lift can move from any state to any state.

In order to define the transitions from state [up, close] and [up, open] the following definitions are needed:

The transitions from state [up, close] are then as follows:

The transitions from state [up, open] are as follows

The transitions from the other states should be defined in a similar fashion.

Exercise 39.  

The Mutual Exclusion Problem for 2 processes is defined as follows:

2 processes are executing in an endless loop a sequence of instructions which can be divided into subsequences: the Critical and the Non-Critical section. The mutual exclusion property is that instructions from the Critical sections of these two processes must not be interleaved.

The solution will be describe by inserting into the endless loop additional instructions that are to be executed by a process wishing to enter and leave its critical section, the Pre and Post sections.

while true do {
Non_Critical_Section;
Pre_Section;
Critical_Section;
Post_Section}

A process may halt in its Non-Critical section but it may not halt in the Pre, Post and Critical sections. If a process halts in its Non-Critical section, it must not interfere with the other process.

The solution must satisfy the following:

The two processes must not deadlock. If the two process are trying to enter their Critical Section and no process ever succeeds in making the transition from the Pre Section to the Critical Section then we say that the two processes are deadlocked.

There must be no starvation of one of the processes. If a process indicates its intention to enter the Critical Section by entering the Pre Section, eventually it will succeed.

In the absence of contention for the Critical section, a single process wishing to enter its Critical Section will succeed and with minimal overhead.

Examine the following solution by giving its Tempura version and simulate various interesting runs.

Let Turn be a shared variable with possible values 1 and 2, initially it is 1.

while true do {
while true do {
Non_Critical_Section_1;
Non_Critical_Section_2;
while Turn≠1 do skip;
while Turn≠2 do skip;
Critical_Section_1;
Critical_Section_2;
Turn := 2}
Turn := 1}

Examine the following solution by giving its Tempura version and simulate various interesting runs.

Let Ci be shared variable with possible values 0 and 1, and that can only be set by process i, initially Ci = 1.

while true do {
while true do {
Non_Critical_Section_1;
Non_Critical_Section_2;
while C21 do skip;
while C11 do skip;
C1 := 0;
C2 := 0;
Critical_Section_1;
Critical_Section_2;
C1 := 1}
C2 := 1}

Examine the following solution by giving its Tempura version and simulate various interesting runs.

Let Ci be shared variable with possible values 0 and 1, and that can only be set by process i, initially Ci = 1.

while true do {
while true do {
Non_Critical_Section_1;
Non_Critical_Section_2;
C1 := 0;
C2 := 0;
while C21 do skip;
while C11 do skip;
Critical_Section_1;
Critical_Section_2;
C1 := 1}
C2 := 1}

Examine the following solution by giving its Tempura version and simulate various interesting runs.

Let Ci be shared variable with possible values 0 and 1, and that can only be set by process i, initially Ci = 0. Let Last be a shared variable with possible values 1 and 2, initially Last = 1.

while true do {
while true do {
Non_Critical_Section_1;
Non_Critical_Section_2;
C1 := 1;
C2 := 1;
Last := 1;
Last := 2;
while C20 Last = 1
while C10 Last = 2
do skip;
do skip;
Critical_Section_1;
Critical_Section_2;
C1 := 0}
C2 := 0}

Exercise 40.  

In distributed computing, it is often useful to be able to designate one and only one process as the coordinator of some activity. This selection of a coordinator is known as the leader election problem.

In this exercise you will consider the problem of electing a leader among a group of 10 processes whose communication pattern is arranged in a ring, where each process can only receive messages from its left neighbour and may send messages only to its right neighbour. It is assumed that each process starts out with a unique identifier (an integer).

Implement and test the following two leader election algorithms. Read over the entire assignment before starting.

Le Lann-Chang-Roberts Leader Election: This is perhaps the simplest of all distributed algorithms. It sends around 102 messages in the worst case. Try to construct an execution in which you would get this worst case behaviour.

The idea of this algorithm is that each process starts by passing its own identifier to its neighbour. Whenever a process receives an identifier from its neighbour, it either:

Notice that the process with the greatest identifier is elected.

Create a Tempura program that implements this algorithm.

Peterson Leader Election Algorithm: This algorithm for leader election in a ring is a bit more involved, but its worst case message complexity is much better. See if you can figure out the message complexity of this algorithm. (Hint: notice that at least half of the processes become relays after each round of communication.)

The algorithm works as follows:

Implement this algorithm as a Tempura Program.

Think about how you will manage sending a message two steps around the ring of active processes. Also, print out useful messages in order to demonstrate that your algorithm is working properly. For example, at each round, active processes should print out the round number, the messages received from the two neighbours, and the decision of whether to remain active or become a relay.

Exercise 41.  

Write a Tempura program that simulates a cash machine (atm). First write an informal specification and then implement it as a Tempura program.

2023-09-12
Contact | Home | ITL home | Course | Proofs | Algebra | FL
© 1996-2023