From a59e624c1cd01295a9c33b9672d1c34ad9e3913c Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Fri, 18 Nov 2022 14:27:59 +0100 Subject: [PATCH] added Csharp Uebung projects --- ADIS_Csharp/ADIS_Csharp.sln | 14 ++++- ADIS_Csharp/Sync-Ueb01-Counter/Counter.cs | 22 ++++++++ ADIS_Csharp/Sync-Ueb01-Counter/CounterTest.cs | 36 +++++++++++++ .../Sync-Ueb01-Counter.csproj | 9 ++++ ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs | 13 +++++ ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs | 21 ++++++++ ADIS_Csharp/Sync-Ueb02-Latch/RaceHorse.cs | 28 ++++++++++ .../Sync-Ueb02-Latch/Sync-Ueb02-Latch.csproj | 9 ++++ ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs | 22 ++++++++ ADIS_Csharp/Sync-Ueb03-WaitPool/MyThread.cs | 28 ++++++++++ .../Sync-Ueb03-WaitPool.csproj | 9 ++++ .../Sync-Ueb03-WaitPool/TestWaitPool.cs | 19 +++++++ .../Sync-Ueb04-NestedMonitor/BlackHole.cs | 20 +++++++ .../NotifyingQueue.cs | 32 +++++++++++ .../Sync-Ueb04-NestedMonitor.csproj | 9 ++++ .../TestNestedMonitor.cs | 28 ++++++++++ ADIS_Csharp/Sync-Ueb05-LostSignals/AWait.cs | 16 ++++++ .../Sync-Ueb05-LostSignals/AWaitTest.cs | 19 +++++++ ADIS_Csharp/Sync-Ueb05-LostSignals/ISynch.cs | 8 +++ .../Sync-Ueb05-LostSignals/Operator.cs | 53 +++++++++++++++++++ .../Sync-Ueb05-LostSignals.csproj | 9 ++++ ADIS_Csharp/Sync-Ueb05-LostSignals/Worker.cs | 31 +++++++++++ 22 files changed, 454 insertions(+), 1 deletion(-) create mode 100644 ADIS_Csharp/Sync-Ueb01-Counter/Counter.cs create mode 100644 ADIS_Csharp/Sync-Ueb01-Counter/CounterTest.cs create mode 100644 ADIS_Csharp/Sync-Ueb01-Counter/Sync-Ueb01-Counter.csproj create mode 100644 ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs create mode 100644 ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs create mode 100644 ADIS_Csharp/Sync-Ueb02-Latch/RaceHorse.cs create mode 100644 ADIS_Csharp/Sync-Ueb02-Latch/Sync-Ueb02-Latch.csproj create mode 100644 ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs create mode 100644 ADIS_Csharp/Sync-Ueb03-WaitPool/MyThread.cs create mode 100644 ADIS_Csharp/Sync-Ueb03-WaitPool/Sync-Ueb03-WaitPool.csproj create mode 100644 ADIS_Csharp/Sync-Ueb03-WaitPool/TestWaitPool.cs create mode 100644 ADIS_Csharp/Sync-Ueb04-NestedMonitor/BlackHole.cs create mode 100644 ADIS_Csharp/Sync-Ueb04-NestedMonitor/NotifyingQueue.cs create mode 100644 ADIS_Csharp/Sync-Ueb04-NestedMonitor/Sync-Ueb04-NestedMonitor.csproj create mode 100644 ADIS_Csharp/Sync-Ueb04-NestedMonitor/TestNestedMonitor.cs create mode 100644 ADIS_Csharp/Sync-Ueb05-LostSignals/AWait.cs create mode 100644 ADIS_Csharp/Sync-Ueb05-LostSignals/AWaitTest.cs create mode 100644 ADIS_Csharp/Sync-Ueb05-LostSignals/ISynch.cs create mode 100644 ADIS_Csharp/Sync-Ueb05-LostSignals/Operator.cs create mode 100644 ADIS_Csharp/Sync-Ueb05-LostSignals/Sync-Ueb05-LostSignals.csproj create mode 100644 ADIS_Csharp/Sync-Ueb05-LostSignals/Worker.cs diff --git a/ADIS_Csharp/ADIS_Csharp.sln b/ADIS_Csharp/ADIS_Csharp.sln index 795d69a..0b93a34 100644 --- a/ADIS_Csharp/ADIS_Csharp.sln +++ b/ADIS_Csharp/ADIS_Csharp.sln @@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.1.32421.90 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sync-Ueb01-Counter", "Sync-Ueb01-Counter\Sync-Ueb01-Counter.csproj", "{AB57199F-6373-4730-AB5B-76803A589AAF}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sync-Ueb02-Latch", "Sync-Ueb02-Latch\Sync-Ueb02-Latch.csproj", "{BCCFD952-F042-483E-8660-EEAD346E37E5}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +19,14 @@ Global {42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Debug|Any CPU.Build.0 = Debug|Any CPU {42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Release|Any CPU.ActiveCfg = Release|Any CPU {42720F37-2AAE-4B3D-AA7E-79A7C37D4C37}.Release|Any CPU.Build.0 = Release|Any CPU + {AB57199F-6373-4730-AB5B-76803A589AAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB57199F-6373-4730-AB5B-76803A589AAF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB57199F-6373-4730-AB5B-76803A589AAF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB57199F-6373-4730-AB5B-76803A589AAF}.Release|Any CPU.Build.0 = Release|Any CPU + {BCCFD952-F042-483E-8660-EEAD346E37E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BCCFD952-F042-483E-8660-EEAD346E37E5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BCCFD952-F042-483E-8660-EEAD346E37E5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BCCFD952-F042-483E-8660-EEAD346E37E5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ADIS_Csharp/Sync-Ueb01-Counter/Counter.cs b/ADIS_Csharp/Sync-Ueb01-Counter/Counter.cs new file mode 100644 index 0000000..91e8808 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb01-Counter/Counter.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace Sync_Ueb01_Counter +{ + class Counter + { + private int count = 0; + private static object syncRoot = new(); + + public int NextNumber() + { + lock (syncRoot) + { + count++; + return count; + } + } + } +} \ No newline at end of file diff --git a/ADIS_Csharp/Sync-Ueb01-Counter/CounterTest.cs b/ADIS_Csharp/Sync-Ueb01-Counter/CounterTest.cs new file mode 100644 index 0000000..63de52e --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb01-Counter/CounterTest.cs @@ -0,0 +1,36 @@ +using System; +using System.Threading; + +namespace Sync_Ueb01_Counter +{ + class CounterTest + { + private String id; + private Counter counter; + private object locker = new object(); + + public CounterTest(String id, Counter counter) + { + this.id = id; + this.counter = counter; + } + void Go() + { + while (true) + { + Thread.Sleep(100); + Console.WriteLine(id + counter.NextNumber()); + } + } + static void Main() + { + Counter counter = new Counter(); + CounterTest ct1 = new CounterTest("T1: ", counter); + CounterTest ct2 = new CounterTest("T2: ", counter); + CounterTest ct3 = new CounterTest("T3: ", counter); + new Thread(ct1.Go).Start(); + new Thread(ct2.Go).Start(); + new Thread(ct3.Go).Start(); + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb01-Counter/Sync-Ueb01-Counter.csproj b/ADIS_Csharp/Sync-Ueb01-Counter/Sync-Ueb01-Counter.csproj new file mode 100644 index 0000000..4465112 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb01-Counter/Sync-Ueb01-Counter.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Sync_Ueb01_Counter + + + diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs b/ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs new file mode 100644 index 0000000..b5bd34a --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb02-Latch/ISynch.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Sync_Ueb02_Latch +{ + interface ISynch + { + void Acquire(); + void Release(); + } + +} diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs b/ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs new file mode 100644 index 0000000..f9b8913 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb02-Latch/Latch.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace Sync_Ueb02_Latch +{ + class Latch : ISynch + { private ManualResetEvent waitHandle = new(false); + + public void Acquire() + { + this.waitHandle.WaitOne(); + } + public void Release() + { + this.waitHandle.Set(); + } + } + +} diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/RaceHorse.cs b/ADIS_Csharp/Sync-Ueb02-Latch/RaceHorse.cs new file mode 100644 index 0000000..8bb00ba --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb02-Latch/RaceHorse.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace Sync_Ueb02_Latch +{ + class RaceHorse + { + private ISynch startSignal; + private int nr; + + public RaceHorse(int nr, ISynch startSignal) + { + this.nr = nr; + this.startSignal = startSignal; + } + public void Run() + { + Console.WriteLine("Rennpferd " + nr + " geht in die Startbox."); + startSignal.Acquire(); + Console.WriteLine("Rennpferd " + nr + " läuft los."); + Thread.Sleep(new Random().Next(3000)); + Console.WriteLine("Rennpferd " + nr + " ist im Ziel."); + } + } + +} diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/Sync-Ueb02-Latch.csproj b/ADIS_Csharp/Sync-Ueb02-Latch/Sync-Ueb02-Latch.csproj new file mode 100644 index 0000000..2e619b4 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb02-Latch/Sync-Ueb02-Latch.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Sync_Ueb02_Latch + + + diff --git a/ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs b/ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs new file mode 100644 index 0000000..7062270 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb02-Latch/Turf.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace Sync_Ueb02_Latch +{ + class Turf + { + static void Main() + { + ISynch startBox = new Latch(); + for (int i = 1; i <= 5; i++) + { + new Thread(new RaceHorse(i, startBox).Run).Start(); + } + Console.WriteLine("Start..."); + startBox.Release(); + } + } + +} diff --git a/ADIS_Csharp/Sync-Ueb03-WaitPool/MyThread.cs b/ADIS_Csharp/Sync-Ueb03-WaitPool/MyThread.cs new file mode 100644 index 0000000..5efca4a --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb03-WaitPool/MyThread.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace Sync_Ueb03_WaitPool +{ + class MyThread + { + private Object synch; + + public MyThread(Object synch) + { + this.synch = synch; + } + + public void Run() + { + Console.WriteLine("warten..."); + lock (synch) + { + Monitor.Wait(this); + } + Console.WriteLine("...aufgewacht"); + } + } + +} diff --git a/ADIS_Csharp/Sync-Ueb03-WaitPool/Sync-Ueb03-WaitPool.csproj b/ADIS_Csharp/Sync-Ueb03-WaitPool/Sync-Ueb03-WaitPool.csproj new file mode 100644 index 0000000..cd7d5b2 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb03-WaitPool/Sync-Ueb03-WaitPool.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Sync_Ueb03_WaitPool + + + diff --git a/ADIS_Csharp/Sync-Ueb03-WaitPool/TestWaitPool.cs b/ADIS_Csharp/Sync-Ueb03-WaitPool/TestWaitPool.cs new file mode 100644 index 0000000..7e94be8 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb03-WaitPool/TestWaitPool.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading; + +namespace Sync_Ueb03_WaitPool +{ + class TestWaitPool + { + static void Main() + { + Object synch = new Object(); + MyThread myThread = new MyThread(synch); + new Thread(myThread.Run).Start(); + Thread.Sleep(1000); + Monitor.Pulse(synch); + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb04-NestedMonitor/BlackHole.cs b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/BlackHole.cs new file mode 100644 index 0000000..5d8f0f0 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/BlackHole.cs @@ -0,0 +1,20 @@ +using System; + +namespace Sync_Ueb04_NestedMonitor +{ + class BlackHole { + + private NotifyingQueue queue = new NotifyingQueue(); + + public void Put(String thing) { + lock (this) { + queue.Enqueue(thing); + } + } + public String Get() { + lock (this) { + return queue.Dequeue(); + } + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb04-NestedMonitor/NotifyingQueue.cs b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/NotifyingQueue.cs new file mode 100644 index 0000000..8de3ad6 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/NotifyingQueue.cs @@ -0,0 +1,32 @@ +using System.Threading; + +namespace Sync_Ueb04_NestedMonitor +{ + class NotifyingQueue { + + private const int SIZE = 10; + private T[] queue = new T[SIZE]; + private int head = 0; + private int tail = 0; + + public void Enqueue(T item) { + lock (this) { + head++; + head %= SIZE; + queue[head] = item; + Monitor.Pulse(this); + } + } + + public T Dequeue() { + lock (this) { + if (head == tail) { + Monitor.Wait(this); + } + tail++; + tail %= SIZE; + return queue[tail]; + } + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb04-NestedMonitor/Sync-Ueb04-NestedMonitor.csproj b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/Sync-Ueb04-NestedMonitor.csproj new file mode 100644 index 0000000..a6a80f3 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/Sync-Ueb04-NestedMonitor.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Sync_Ueb04_NestedMonitor + + + diff --git a/ADIS_Csharp/Sync-Ueb04-NestedMonitor/TestNestedMonitor.cs b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/TestNestedMonitor.cs new file mode 100644 index 0000000..ca786f1 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb04-NestedMonitor/TestNestedMonitor.cs @@ -0,0 +1,28 @@ +using System; +using System.Threading; + +namespace Sync_Ueb04_NestedMonitor +{ + class TestNestedMonitor { + + public static BlackHole blackhole = new BlackHole(); + + public static void Main() + { + Thread t1 = new Thread(delegate() { + Console.WriteLine(blackhole.Get().ToString()); + }); + + Thread t2 = new Thread(delegate() { + blackhole.Put("Sonne, Licht, irgendetwas..."); + }); + + Console.WriteLine("Wir starten die Untersuchung..."); + t1.Start(); + t2.Start(); + t1.Join(); + t2.Join(); + Console.WriteLine("Was ist passiert..."); + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb05-LostSignals/AWait.cs b/ADIS_Csharp/Sync-Ueb05-LostSignals/AWait.cs new file mode 100644 index 0000000..de7d424 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb05-LostSignals/AWait.cs @@ -0,0 +1,16 @@ +using System; +using System.Threading; + +namespace LostSignals { + + class AWait : ISynch { + + public void Acquire() + { + } + + public void Release() + { + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb05-LostSignals/AWaitTest.cs b/ADIS_Csharp/Sync-Ueb05-LostSignals/AWaitTest.cs new file mode 100644 index 0000000..810865a --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb05-LostSignals/AWaitTest.cs @@ -0,0 +1,19 @@ +using System.Threading; + +namespace LostSignals +{ + class AWaitTest + { + static void Main() + { + ISynch signal = new AWait(); + Operator op = new Operator(signal); + new Thread(op.Do).Start(); + for (int i = 0; i < 10; i++) + { + Worker worker = new Worker(signal, op, "Worker" + i); + new Thread(worker.Do).Start(); + } + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb05-LostSignals/ISynch.cs b/ADIS_Csharp/Sync-Ueb05-LostSignals/ISynch.cs new file mode 100644 index 0000000..e95559f --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb05-LostSignals/ISynch.cs @@ -0,0 +1,8 @@ +namespace LostSignals +{ + interface ISynch + { + void Acquire(); + void Release(); + } +} diff --git a/ADIS_Csharp/Sync-Ueb05-LostSignals/Operator.cs b/ADIS_Csharp/Sync-Ueb05-LostSignals/Operator.cs new file mode 100644 index 0000000..34f1214 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb05-LostSignals/Operator.cs @@ -0,0 +1,53 @@ +using System; +using System.Threading; + +namespace LostSignals +{ + class Operator + { + public const int MAXCOUNT = 4; + private int counter = 0; + private ISynch signal; + private Random rnd; + private EventWaitHandle next = new AutoResetEvent(false); + + public Operator(ISynch signal) { + this.signal = signal; + rnd = new Random(); + } + + private void Init() { + lock (this) { + counter = MAXCOUNT; + Console.WriteLine("Es wurden " + counter + " Operationen vorbereitet..."); + } + } + + public int Operation() { + lock (this) { + return rnd.Next(1000); + } + } + + public void Done(String id) { + lock (this) { + counter++; + Console.WriteLine(counter + ". Operation wurde durch " + id + " beendet."); + if (counter == MAXCOUNT) { + next.Set(); + } + } + } + public void Do() { + while (true) { + Init(); + lock (this) { + for (; counter > 0; counter--) { + signal.Release(); + } + } + next.WaitOne(); + } + } + } +} diff --git a/ADIS_Csharp/Sync-Ueb05-LostSignals/Sync-Ueb05-LostSignals.csproj b/ADIS_Csharp/Sync-Ueb05-LostSignals/Sync-Ueb05-LostSignals.csproj new file mode 100644 index 0000000..5f75c44 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb05-LostSignals/Sync-Ueb05-LostSignals.csproj @@ -0,0 +1,9 @@ + + + + Exe + net5.0 + Sync_Ueb05_LostSignals + + + diff --git a/ADIS_Csharp/Sync-Ueb05-LostSignals/Worker.cs b/ADIS_Csharp/Sync-Ueb05-LostSignals/Worker.cs new file mode 100644 index 0000000..b275338 --- /dev/null +++ b/ADIS_Csharp/Sync-Ueb05-LostSignals/Worker.cs @@ -0,0 +1,31 @@ +using System; +using System.Threading; + +namespace LostSignals +{ + class Worker + { + private ISynch signal; + private Operator op; + private String id; + + public Worker(ISynch signal, Operator op, String id) + { + this.signal = signal; + this.op = op; + this.id = id; + } + + public void Do() + { + while (true) + { + signal.Acquire(); + Console.WriteLine(id + " released..."); + Thread.Sleep(op.Operation()); + op.Done(id); + Thread.Sleep(1); + } + } + } +}