// Øving uke 2 // Program ramme #include "stdafx.h" using namespace System; using namespace System::Threading; using namespace System::Diagnostics; public ref class TraadKlasse public: static void traadramme() int teller=0; array<string^>^colornames = Enum::GetNames(ConsoleColor::typeid); while(true) Thread::Sleep(1000); ConsoleColor spartcolor = Console::BackgroundColor; int Posx = Console::CursorLeft; int Posy = Console::CursorTop; if (++teller == colornames->length) teller = 0; Console::BackgroundColor = *dynamic_cast<consolecolor^> (Enum::Parse(ConsoleColor::typeid, colornames[teller])); for (int i=1; i<=20; i++) Console::CursorLeft = i; Console::CursorTop = 1; Console::Write(" "); Console::CursorTop = 11; Console::Write(" "); for (int j=1; j<=11; j++) Console::CursorLeft = 1; Console::CursorTop = j; Console::Write(" "); Console::CursorLeft = 21; Console::Write(" "); Console::BackgroundColor = spartcolor; Console::CursorLeft = Posx; Console::CursorTop = Posy; 1
public: static void Start() ThreadStart^ threaddelegate1 = gcnew ThreadStart(traadRamme); Thread^ traad = gcnew Thread(threadDelegate1); traad->start(); ; // end class Traadklasse int main(array<system::string ^> ^args) Console::Title = "Øving 3, uke 2"; TraadKlasse^ traadobj = gcnew TraadKlasse(); traadobj->start(); Console::WriteLine("\n\n"); Console::WriteLine(" 1. Kalkulator"); Console::WriteLine(" 2. Paint"); Console::WriteLine(" 3. NotePad"); Console::WriteLine(" 4. Excel"); while (true) Console::CursorLeft = 4; Console::CursorTop = 8; Console::Write("Gi et valg: "); Console::CursorLeft = 16; String^ str = Console::ReadLine(); int valg = Convert::ToInt32(str); String^ prog; switch (valg) case 1: case 2: case 3: case 4: prog = "calc.exe"; break; prog = "mspaint.exe"; break; prog = "notepad.exe"; break; prog = "excel.exe"; break; Process^ minprosses = gcnew Process; ProcessStartInfo^ minprossesstartinfo = gcnew ProcessStartInfo(prog); minprosses->startinfo = minprossesstartinfo; minprosses->start(); return 0; 2
// Øving uke 3 Program synkronisering // Murer og handlanger #include "stdafx.h" using namespace System; using namespace System::Threading; public ref class Murstein private: static int ant_steiner; static Semaphore^ stabel_semafor; static Semaphore^ tom_semafor; static Semaphore^ full_semafor; static Mutex^ skjerm_mutex; public: static void murer() Random^ rand = gcnew Random; while (true) stabel_semafor->waitone(); --ant_steiner; if (ant_steiner == 14) full_semafor->release(); skjerm_mutex->waitone(); ConsoleColor spar = Console::ForegroundColor; Console::ForegroundColor = ConsoleColor::Yellow; Console::WriteLine("\n Murer tar en stein:\t\t" + ant_steiner + " steiner i stabelen"); Console::ForegroundColor = spar; skjerm_mutex->releasemutex(); stabel_semafor->release(); // må frigi etter utskrift // av ant_steiner if (ant_steiner == 0) tom_semafor->waitone(); Thread::Sleep(1000 * rand->next(1, 2)); static void handlanger() Random^ rand = gcnew Random; while (true) 3
stabel_semafor->waitone(); ++ant_steiner; if (ant_steiner == 1) tom_semafor->release(); skjerm_mutex->waitone(); ConsoleColor spar = Console::ForegroundColor; Console::ForegroundColor = ConsoleColor::White; Console::WriteLine("\n Handlanger legger en stein:\t" + ant_steiner + " steiner i stabelen"); Console::ForegroundColor = spar; skjerm_mutex->releasemutex(); stabel_semafor->release(); // må frigi etter utskrift // av ant_steiner if (ant_steiner == 15) full_semafor->waitone(); Thread::Sleep(1000 * rand->next(3, 5)); static void Main() ant_steiner = 10; stabel_semafor = gcnew Semaphore(1,1); tom_semafor = gcnew Semaphore(0,1); full_semafor = gcnew Semaphore(0,1); skjerm_mutex = gcnew Mutex(); ThreadStart^ threaddelegate1 = gcnew ThreadStart(murer); ThreadStart^ threaddelegate2 = gcnew ThreadStart(handlanger); Thread^ traad1 = gcnew Thread(threadDelegate1); traad1->start(); ; Thread^ traad2 = gcnew Thread(threadDelegate2); traad2->start(); int main(array<system::string ^> ^args) Console::Title = "Murer og handlanger"; Murstein^ obj = gcnew Murstein(); obj->main(); return 0; 4
// Øving uke 4 Program interprosess kommunikasjon // Prosess handlanger (samarbeider med Prosess murer) #include "stdafx.h" using namespace System; using namespace System::Threading; using namespace System::IO; using namespace System::Runtime::Serialization; using namespace System::Runtime::Serialization::Formatters::Binary; int antall_i_stabel() int antsteiner; String^ filnavn = "C:\\kladd\\steinfil.txt"; FileStream^ fs = gcnew FileStream(filnavn, FileMode::Open); BinaryFormatter^ formatter = gcnew BinaryFormatter; antsteiner = (int)formatter->deserialize(fs); catch (SerializationException^ ) Console::WriteLine("Kunne ikke lese fil"); fs->close(); return(antsteiner); void oppdatstabel(int antsteiner) String^ filnavn = "C:\\kladd\\steinfil.txt"; FileStream^ fs = gcnew FileStream(filnavn, FileMode::Create); BinaryFormatter^ formatter = gcnew BinaryFormatter; formatter->serialize(fs, antsteiner); catch (SerializationException^ ) Console::WriteLine("Kunne ikke skrive til fil"); fs->close(); 5
int main(array<system::string ^> ^args) Console::Title = "Prosess handlanger"; int ant_steiner = 10; oppdatstabel(10); // skriver 10 steiner til fil Mutex^ stabel_mutex = gcnew Mutex(false, "stabel_mutex"); Mutex^ skjerm_mutex = gcnew Mutex(false, "skjerm_mutex"); // false for å angi at skjerm_mutex skal brukes av andre prosesser // Oppretter to event objekter EventWaitHandle^ tomt_event = gcnew EventWaitHandle(false, EventResetMode::AutoReset, "tomt_event"); EventWaitHandle^ fullt_event = gcnew EventWaitHandle(false, EventResetMode::AutoReset, "fullt_event"); Random^ rand = gcnew Random; while (true) stabel_mutex->waitone(); ant_steiner = antall_i_stabel();// henter antall steiner fra fil ++ant_steiner; oppdatstabel(ant_steiner); // skriver til fil if (ant_steiner == 1) tomt_event->set(); skjerm_mutex->waitone(); Console::WriteLine("\n Handlanger legger en stein:\t" + ant_steiner + " steiner i stabelen"); skjerm_mutex->releasemutex(); stabel_mutex->releasemutex(); // må frigi etter bruk // av ant_steiner if (ant_steiner == 25) fullt_event->waitone(); Thread::Sleep(1000 * rand->next(3, 4)); return 0; 6
// Øving uke 4 Program interprosess kommunikasjon // Prosess murer (samarbeider med Prosess handlanger) #include "stdafx.h" using namespace System; using namespace System::Threading; using namespace System::IO; using namespace System::Runtime::Serialization; using namespace System::Runtime::Serialization::Formatters::Binary; int antall_i_stabel() int antsteiner; String^ filnavn = "C:\\kladd\\steinfil.txt"; FileStream^ fs = gcnew FileStream(filnavn, FileMode::Open); BinaryFormatter^ formatter = gcnew BinaryFormatter; antsteiner = (int)formatter->deserialize(fs); catch (SerializationException^ ) Console::WriteLine("Kunne ikke lese fil"); fs->close(); return(antsteiner); void oppdatstabel(int antsteiner) String^ filnavn = "C:\\kladd\\steinfil.txt"; FileStream^ fs = gcnew FileStream(filnavn, FileMode::Create); BinaryFormatter^ formatter = gcnew BinaryFormatter; formatter->serialize(fs, antsteiner); catch (SerializationException^ ) Console::WriteLine("Kunne ikke skrive til fil"); fs->close(); int main(array<system::string ^> ^args) 7
Console::Title = "Prosess murer"; int ant_steiner; Mutex^ stabel_mutex = nullptr; Mutex^ skjerm_mutex = nullptr; EventWaitHandle^ tomt_event = nullptr; EventWaitHandle^ fullt_event = nullptr; // Skal først åpne tilgang til semafor, mutex og Event objektene stabel_mutex = Mutex::OpenExisting("stabel_mutex"); catch (WaitHandleCannotBeOpenedException^ ex) Console::WriteLine("Kunne ikke åpne stabel_semafor: 0", ex->message); catch (UnauthorizedAccessException^ ex) Console::WriteLine("Ikke tilgang til stabel_semafor: 0", ex->message); skjerm_mutex = Mutex::OpenExisting("skjerm_mutex"); catch (WaitHandleCannotBeOpenedException^ ex) Console::WriteLine("Kunne ikke åpne skjerm_mutex: 0", ex->message ); catch (UnauthorizedAccessException^ ex) Console::WriteLine("Ikke tilgang til skjerm_mutex: 0", ex->message ); tomt_event = EventWaitHandle::OpenExisting("tomt_Event"); catch (WaitHandleCannotBeOpenedException^ ex) Console::WriteLine("Kunne ikke åpne fullt_event: 0", ex->message); catch (UnauthorizedAccessException^ ex) Console::WriteLine( L"Ikke tilgang til tomt_event: 0", 8
ex->message ); fullt_event = EventWaitHandle::OpenExisting("fullt_Event"); catch (WaitHandleCannotBeOpenedException^ ex) Console::WriteLine("Kunne ikke åpne fullt_event: 0", ex->message); catch (UnauthorizedAccessException^ ex) Console::WriteLine("Ikke tilgang til fullt_event: 0", ex->message); Random^ rand = gcnew Random; Console::WriteLine(); while (true) stabel_mutex->waitone(); ant_steiner = antall_i_stabel();// henter antall steiner fra fil --ant_steiner; oppdatstabel(ant_steiner); // skriver til fil if (ant_steiner == 24) fullt_event->set(); skjerm_mutex->waitone(); Console::WriteLine("\n Murer tar en stein:\t\t" + ant_steiner + " steiner i stabelen"); skjerm_mutex->releasemutex(); stabel_mutex->releasemutex(); // må frigi etter bruk av ant_steiner if (ant_steiner == 0) tomt_event->waitone(); Thread::Sleep(1000 * rand->next(1, 2)); return 0; 9
// Øving uke 4 // Program monitor synkronisering #include "stdafx.h" using namespace System; using namespace System::Threading; using namespace System::Collections; public ref class MonitorOving3 // member variables private: static ArrayList^ m_liste; // member functions public: static void foytilob(string^ str) Monitor::Enter(m_liste); if (m_liste->count < 10) m_liste->add(str); Monitor::Exit(m_liste); public: static void fjernob1() Monitor::Enter(m_liste); if (m_liste->count > 0) m_liste->removeat(0); Monitor::Exit(m_liste); public: static void skrivliste() Monitor::Enter(m_liste); if (m_liste->count > 0) Console::WriteLine(); for (int i=0; i<m_liste->count; i++) String^ str = (String^)m_liste[i]; Console::Write(" " + str); Console::WriteLine(); Monitor::Exit(m_liste); 10
public: static void Traad1() Random^ rand = gcnew Random(); int teller=0; while(true) String^ str = (++teller).tostring(); foytilob(str); skrivliste(); int pause = rand->next(1, 2); Thread::Sleep(1000*pause); public: static void Traad2() Random^ rand = gcnew Random(); while(true) fjernob1(); int pause = rand->next(2, 3); Thread::Sleep(1000*pause); void Start() m_liste = gcnew ArrayList; ThreadStart^ threaddelegate1 = gcnew ThreadStart(Traad1); ThreadStart^ threaddelegate2 = gcnew ThreadStart(Traad2); Thread^ traad1 = gcnew Thread(threadDelegate1); traad1->start(); Thread::Sleep(5000); ; Thread^ traad2 = gcnew Thread(threadDelegate2); traad2->start(); 11
int main(array<system::string ^> ^args) Console::Title = "Monitor øving 3"; MonitorOving3^ oving = gcnew MonitorOving3; oving->start(); Console::WriteLine(L"\n\n"); return 0; 12