I3MUP1+Exercise+6.2

toc =Exercise 6.2= //In this exercise you will use the OS API (for Windows) to implement a system that uses some OS facilities. In// //this way, you will gain some more routine in the use of the OS API and an insight into design with the OS// //API//

Exercise 6.2.1 & 6.2.2
//Design your system.// //Draw a class diagram that shows the relationship between the classes involved.//

The picture below shows how we've decided to design our system. We have three Thread classes responsible for user input, data storage, and output. The InputThread and StorageThread share a Mailbox for their communication, and the StorageThread and InfoThread utilize a Monitor class to share data protected by a scopelocker, which secures atomic access to the data. The data we send will be contained in a Person class. The InfoThread uses a SleepTimer to only look for new data every 3 seconds. Additionally, we use a Mutex which isn't shown on the class diagram to avoid the InfoThread overwriting the output of the InputThread, since they both output to the screen. A different solution might have been to output to a file.

Exercise 6.2.3
//Implement and test your system.//

Main
Our main initializes the mailbox, mutex, monitor and thread objects, and calls the start function on the thread objects. We avoid busy-waiting in main by using a sleeptimer. code format="cpp" int main { Monitor myMonitor; Mailbox myMailbox(10); Mutex myMutex; InputThread myInput(&myMailbox, &myMutex); StorageThread myStorage(&myMailbox, &myMonitor); InfoThread myInfo(&myMonitor, &myMutex);

myInfo.start; myInput.start; myStorage.start;

SleepTimer t; while(1) t.sleep(3000); return 1; } code

InputThread
The input thread has a run-function which prompts the user for name and social security number, and sends this data via the mailbox. code format="cpp" template class InputThread : public Thread { public: InputThread(Mailbox* mb = NULL, Mutex * myMutex = NULL) : Thread(Thread::PRIORITY_NORMAL, "input"), mailbox(mb), mutex(myMutex) { } void run { DT dataInput; while(1) { mutex->wait; printf("\nPlease type in your name : "); getline(cin, dataInput.name);//cin >> dataInput.name; printf("\n...And your social security number : "); getline(cin, dataInput.cpr);//cin >> dataInput.cpr; cout << "\nThank you "<< dataInput.name << endl; mailbox->put(dataInput); mutex->signal; Sleep(1000); } } private: Mutex * mutex; Mailbox* mailbox; }; code

StorageThread
The storage thread simply waits for new data in the mailbox and saves it in the monitor class. code format="cpp" template class StorageThread : public Thread { public: StorageThread(Mailbox* mb_i = NULL, Monitor* mb_s = NULL) : Thread(Thread::PRIORITY_NORMAL, "storage"), mailbox(mb_i), storage(mb_s) { } void run { while(1) { storage->put(mailbox->get); } }

private: Mailbox* mailbox; Monitor* storage; }; code

InfoThread
The infothread loads data from the monitor class, checks if the data is new, and if it is, outputs it to the screen and sleeps for 3 seconds before checking for new data again. code format="cpp" template class InfoThread : public Thread { public: InfoThread(Monitor * s = NULL, Mutex * myMutex = NULL) : Thread(Thread::PRIORITY_NORMAL, "info"), storage(s), mutex(myMutex) { } void run { SleepTimer timer; DT old; DT temp;

while(1) { temp = storage->get; if ( !(old == temp) ) { old = temp; mutex->wait; cout << "\nThe person on storage is " << old.name << " who has the social security number " << old.cpr; mutex->signal; timer.sleep(3000); } } } private: Mutex * mutex; Monitor * storage; }; code

Monitor
The monitor class simply saves a data object which is protected by a scopelocker, code format="cpp" template<typename Item> class Monitor { public: Monitor { } void put(const Item& myItem) { ScopedLocker sl(mutex); item = myItem; }

Item get { ScopedLocker sl(mutex); return item; }

private: Mutex mutex; Item item; }; code

Exercise 6.2.4
//Change the system so that instead of user name and social security number, it now handles information on cars (make, model, colour, and license plate).//

To make our system able to handle car data instead of person data, the InputThread and InfoThread were changed to prompt the user for car data instead of person data, and to display that data. The Car class we use to represent this data has, like the Person class, an overloaded == operator to compare old data to new data. The more elegant solution would have been to also overload the >> and << operators to prompt and display data relevant to car and person objects. By doing this, the application itself wouldn't have needed to be modified besides using a typedef to define the kind of data being moved around.