Starting from:

$25

CPEN333 -  Software Systems Engineering - Lab  3 - Solved

This lab continues on from Lab/Tutorial 2 where threads were first introduced and further introduces the student to concepts of multi-threading. That is, how to make an application composed of several parallel executing sections of code all running together at the same time.

 

Introduction

Before tackling these questions, make sure you have downloaded and expanded (in the correct directory) the zip file from the web-site relating to the “compressed RTExamples”. 

 

Also make sure you are familiar with the Visual C++ environment and know how to create projects, build, execute and run them. There is a handout on the web-site showing you how to create Concurrent Applications using Visual C++.


 

Procedure- Part A – Making an Active Class with its own main()

 

Start a new Visual C++ project and create two new Active classes derived from the base class ActiveClass (see lecture 4 notes page 22 and one of the examples in project Q2.sln of RTExamples). Let’s call these new active classes ‘CoffeeMaker’ and ‘WashingMachine’.

Note: Remember to create new header and source files for each of these new classes.

 

 Inside the CoffeeMaker and WashingMachine classes add a function main() as a private member function (again see lecture 4 page 22 notes). Design a simple software state machine for each class that captures its behaviour as a series of States. For example, a washing machine should initially startup in the IDLE state and when it receives a message it should transition through the FILLING, WASHING, DRAINING, SPINNING state. Transitions can be on the basis of time, e.g. after 10 seconds in the FILLING state, move to the WASHING State etc. A coffee maker could do something similar related to making different types of coffee (obviously!!!)

 

Write the constructor for both classes so that it can be given an integer argument. This can be used to identify the number of the WashingMachine or CoffeeMaker.

 

Inside the main() for the CoffeeMaker and Washing Machine make the classes start up in the IDLE state and print out the message “#WM1: Ready:” where #WM1 represents the WashingMachine’s number, so we can identify which one is printing that message. Do something similar for the CoffeeMaker.

Now create a separate source file with a main() function. Inside that main() create 5 instances of your WashingMachine and 5 instances of your CoffeeMaker class and check that at run time you have created 10 active objects. Remember that active classes are created in the suspended state so you will have to remember to call the ‘Resume()’ function to allow each of them to run (see lecture 4 page 23 for examples) 

(Note: Don’t forget to put 10 WaitForThread() calls inside your program main() to wait for each active object to complete as per page 23 of lecture 4.)

 

As we mentioned above, see if you can write the program so that the program main() passes an integer argument to the WashingMachine and CoffeeMaker classes constructor function when the object is created (see hint below for how to achieve this). 



Run the program and check that all 10 WashingMachine and CoffeeMakers announce they are ready.

 

Part B 

 

In your program main() we created several instances of the ‘WM and CM’ classes. Add some public member functions to each such as Start(), Stop() etc. Modify your program main() to randomly (and not too fast) invoke the member functions of each WM and CM object, i.e. get your program main() to send Start()and Stop() messages to each WM and CM. 

Of course if you just send messages to the WM and CM objects from your program main()  nothing will happen unless these functions can communicate with the class main() (the thread running inside the Car class), so these functions will have to set/clear additional member variables in the class (you introduce them as you see fit) which can be tested by the class main() and used to affect its behaviour. Now when you send messages to the WM/CM, the class main() should respond to them (if you have done it correctly).

 

Part C

 

Inside your WashingMachine class create two other child threads. They could represent something like a WaterInletValve and a Motor. Modify your WM class main() to run and control these concurrent child threads (as appropriate for the states of a Washing Machine). Don’t forget to wait for them to end. The child threads could do something like print out a message every time they change state e.g. #WM1:Motor Running etc

 

Repeat something similar to the above for the coffee maker, creating threads to control suitable activities associated with making different types of coffee e.g. add and control threads to grind coffee, add water, sprinkle chocolate, steam, add milk/cream and any other associated stuff etc. Control/activate these threads via the CM main thread and print out suitable messages.

 

Part D 

 

Create the four Visual C++ process projects necessary for the Elevator problem given to you as your first assignment. Make sure you can build each of the processes, Dispatcher, IO and Elevators 1 and 2. Make sure that the Dispatcher is the Parent process and that it can successfully create the three other child processes with their own window (initially at least while you are developing the project). Keep this project as we will add to it in later labs.


More products