MUP1+Øvelse+6.2

toc =The abstract OS API=

Opgave 1

 * How do you encapsulate the information that must be sent around in the system?
 * A container class.
 * What resource do you need for communication between the Input Thread and the Storage Thread?
 * A shared class.
 * How do you make the Info Thread look every 3 seconds for new data?
 * Using sleep.
 * How do you secure atomic access to the most recent info between the Storage Thread and the Info Thread?
 * Using a mutex

Opgave 2
Vi skal designe vores system således:

Med det i tanke designede vi vores system således:

Opgave 3
Ud fra ovenstående klasse diagram udarbejder vi programmet. Hele koden

Relevant kode udsnit. Sørger for at hente det eneste element hvert 3. sekund. code format="cpp" void run {	T old; while (1) {		T data = l.get; if (data != old) std::cout << std::endl << data << std::endl; old = data; Sleep(3000); } } code Sørger for at sætte det seneste element i den shared container klasse som InfoThread har adgang til. code format="cpp" void run {	while (1) {		T info = mailbox.get; latest.set(info); } } code Denne klasse er det delte objekt mellem StorageThread og InfoThread, derfor er både set og scopelocked. code format="cpp" template class Latest { public: Latest(void) {}
 * InfoThread.h**
 * StoreageThread.h**
 * Latest.h** //Most recent info//

~Latest(void) {}

void set(T data) {		ScopedLocker s(m); this->data = data; }

T get {		ScopedLocker s(m); return data; } private: T data; Mutex m; }; code Output: code Enter your name: asdf Enter your surname: fdsa Enter your CPR: 123 Enter your name: asdf fdsa 123 fdsa Enter your surname: asdf Enter your CPR: 2134 Enter your name: Muhh Enter your surname: fdsa asdf 2134 Enter your CPR: code

Opgave 4
Vi behøver kun at udskifte main, data klassen og vores input klasse da resten er lavet som templates.

Hele koden

Så det kommer til at så således ud: Mailbox m(10); Latest l; InputThread input(m); StorageThread store(m, l); InfoThread info(l); code || code format="cpp" Mailbox m(10); Latest l; InputThread input(m); StorageThread store(m, l); InfoThread info(l); code || void run {	while (1) {		std::string firstname; std::string surname; unsigned int cpr; std::cout << "Enter your name: "; std::cin >> firstname; std::cout << "Enter your surname: "; std::cin >> surname; std::cout << "Enter your CPR: "; std::cin >> cpr; UserData data; data.cpr = cpr; data.name = firstname + " " + surname; mailbox.put(data); } } code || code format="cpp" void run {	while (1) {		std::string make, model, color, plate; std::cout << "Enter make: "; std::cin >> make; std::cout << "Enter model: "; std::cin >> model; std::cout << "Enter color: "; std::cin >> model; std::cout << "Enter plate: "; std::cin >> plate; CarData data; data.make = make; data.model = model; data.color = color; data.plate = plate; mailbox.put(data); } } code || class UserData { public: bool operator==(const UserData &other) const {		if (cpr == other.cpr && name == other.name) return true; return false; }	bool operator!=(const UserData &other) const {		return !(*this == other); }	unsigned int cpr; std::string name; }; code || code format="cpp" class CarData { public: std::string make, model, color, plate; bool operator==(const CarData &other) const {		if (make == other.make && 			model == other.model &&			color == other.color &&			plate == other.plate) return true; return false; }	bool operator!=(const CarData &other) const {		return !(*this == other); } }; code ||
 * ~ FØR ||~ EFTER ||
 * ~ MAIN || code format="cpp"
 * ~ InputThread || code format="cpp"
 * ~ Data Klasse || code format="cpp"