• No se han encontrado resultados

4 Gestión del Agua Potable en la ASADA de Santa Rosa

4.3 Caracterización de la gestión de la ASADA

3.6

Exercise 2 – Lab 1 preparation

In this exercise you will design a real-time system for an alarm clock application using threads, semaphores and provided hardware interfaces. In lab 1 you will implement your design (the implementation usually takes longer than two hours so be sure to start well in advance of the lab occasion). Study the provided specification, description of the handout code and hardware emulator and answer the following questions (express your design in class diagrams and Java code where necessary):

1. What parallel activities are needed, i.e., which thread objects do you need? 2. What common data need to be shared between threads? Where is the data

to be stored?

3. What operations on data are needed for each thread? Where should this logic be stored?

4. Where will data be accessed concurrently from different threads, i.e. where do you need to provide mutual exclusion?

5. Are there other situations in the alarm clock where semaphores are needed for synchronization?

Hint: study theClockInput class.

6. If you in a thread wait for the next second by simply calling sleep(1000), what will the effect on the clock time be? Is this ok for alarm clock time updating? Can it be improved?

Specification

1. The displayed clock time should be updated every second (by calling the

ClockOutput.showTimemethod). Note: when the user has selectedset alarm

orset time, the hardware/emulator shows the set time and an update of the

clock time will have no effect on the clock display. Still, the clock time should be updated every second regardless of mode.

2. It should be possible to set the clock time and the alarm time. The hard- ware/emulator internally handles the actual setting/editing of the time value. When the user selects another mode, the set value is written to theClock- Input object and give is called for its semaphore. For example, the user

selectsset alarm and edits (using number keys or arrows) the alarm time.

Nothing needs to be done by the control software. Then the user selects

set timeand the alarm time is written to the ClockInput object andgiveis

called. Note: giveis called for each key press or interaction with the alarm

clock. It is therefore necessary to filter signals according to the comment in

ClockInput.getValue.

3. When the clock time is equal to the alarm time, and the alarm on tick

box is selected, the alarm should beep once a second for 20 seconds. The ClockOutput.doAlarm() method provides one beep. The sound should be

3. Multi-Threaded Programming

4. The program should be written in Java, using theThread class. Signalling

and mutual exclusion shall be achieved through semaphores.

5. Optional: Implement an AlarmClock.terminate() method. The terminate

method should result in termination of all your threads, before returning to the caller.

6. Optional challenge: Assume that the time counting should be performed in the hardware giving an interrupt each second. Write another class with a method static void oneTick()that can be called as an interrupt routine.

That class should also implement theRunnableinterface in such a way that

the run method simulates the time interrupts by callingoneTickeach second.

Handout code

The handout code consists of two Java packages calleddoneandtodo. Thedone

package contains the emulator (as an applet) as well as hardware interfaces (Clock- Input and ClockOutput). The todo package will contain your real-time system. todoright now contains one class,AlarmClock, that contains a sample implemen-

tation that beeps upon key presses. You will modify and extendtodowith classes

as you deem necessary for your implementation. AlarmClock class

package todo ; import done .*;

import se . lth . cs . realtime . semaphore . Semaphore ; import se . lth . cs . realtime . semaphore . MutexSem ;

public class AlarmClock extends Thread { private static ClockInput input ; private static ClockOutput output ; private static Semaphore sem ;

public AlarmClock ( ClockInput i , ClockOutput o) { input = i;

output = o;

sem = input . getSemaphoreInstance (); }

// The AlarmClock thread is started by the simulator . No // need to start it by yourself , if you do you will get // an IllegalThreadStateException . The implementation // below is a simple alarmclock thread that beeps upon // each keypress . To be modified in the lab . A recommendation // is to use this thread to start up your system similar // to RTsemBuffer in exercise 1.

public void run () { while ( true ) { sem . take (); output . doAlarm (); } } }

Looking inside the simulator for a moment, what is happening at startup is that the simulator creates an instance of yourAlarmClockclass and starts a new thread

on the resulting object. The new thread starts executing in theAlarmClock.run()

method.

3.6. Exercise 2 – Lab 1 preparation Simulator excerpt: AlarmClock startup code

ClockInput butt2ctrl ; // Interface to user actions via hardware / software .

ClockOutput ctrl2disp ; // Interface to display hardware / software .

AlarmClock control ; // The actual alarm - clock software . // ...

control = new AlarmClock ( butt2ctrl , ctrl2disp ); control . start ();

In the same manner, when the applet is stopped (corresponding to hardware reset), there is a callcontrol.terminate();which you need to override, if you want

to fulfil the optional specification item 5.

The following classes are the ClockOutput and the ClockInput that describe

the interface between the control software and the clock hardware/emulator. Excerpt from the ClockOutput class

public class ClockOutput {

/* *

* Wake - up clock user . */

public void doAlarm () { ... }

/* *

* If the display is currently used to display the time , update it . * If user is using display for setting clock or alarm time , do * nothing .

*/

public void showTime ( int hhmmss ) { ... } }

3. Multi-Threaded Programming

Excerpt from the ClockInput class

public class ClockInput {

/* *

* Semaphore that signals when the user have changed any setting . * Get - method to access the semaphore instance directly .

*/

public Semaphore getSemaphoreInstance () { ... }

/* *

* Get check - box state . */

public boolean getAlarmFlag () { ... }

/* *

* Return values for getChoice . */

public static final int SHOW_TIME = 0; public static final int SET_ALARM = 1; public static final int SET_TIME = 2;

/* *

* Get radio - buttons choice . */

public int getChoice () { ... }

/* *

* When getChoice returns a new choice , and the previous choice * was either SET_ALARM or SET_TIME , the set - value of the display * is returned in the format hhmmss where h , m , and s denotes * hours , minutes , and seconds digits respectively . This means , * for example , that the hour value is obtained by dividing the * return value by 10000.

*/

public int getValue () { ... } }

3.6. Exercise 2 – Lab 1 preparation

Using the emulator

First, you must click somewhere on the image of the clock to give the applet keyboard focus (light blue area). Clicking the check box or another window will steal keyboard focus again.

• HoldShift to set clock time (button 1).

• HoldCtrl to set alarm time (button 3).

• HoldShift+Ctrl to toggle alarm on or off.

• Use direction keys for buttons 2-6.

The tow lines on the LCD display should be fairly obvious. The first line displays the current clock time. The second line displays the alarm time. When the alarm is set the separators in the alarm time turn into colons, otherwise they remain as underscores. When the alarm is beeping the separators will flash with the beeps of the alarm.

Below the buttons is a small status field which displays the alarm status with a check box and the current input mode with three radio buttons. The radio buttons may not be manipulated directly, but the check box can be used to modify the alarm status (on/off).

When either of the modesSet TimeorSet Alarmis active the user may increase

the digit under the cursor (indicated by an underscore) by pressing theupbutton

(2), decrease the digit under the cursor with thedownbutton (5), shift cursor left

3. Multi-Threaded Programming

Documento similar