-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
are now all online.
- Loading branch information
Showing
5 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Pipes; | ||
using System.Linq; | ||
using Exercise; | ||
|
||
/* | ||
* This is an example representing how two processes can communicate through NamedPipe | ||
*/ | ||
|
||
namespace Solution | ||
{ | ||
public class SolutionIPCNamedClient : IPCNamedClient | ||
{ | ||
NamedPipeServerStream server; | ||
StreamReader serverReader; | ||
StreamWriter serverWriter; | ||
|
||
public SolutionIPCNamedClient(String pipeName) | ||
{ | ||
server = new NamedPipeServerStream(pipeName); | ||
} | ||
|
||
public void prepareClient() | ||
{ | ||
Console.WriteLine("Pipe Client is being executed ..."); | ||
Console.WriteLine("[Client] Client will be waiting for the server"); | ||
|
||
server.WaitForConnection(); | ||
serverReader = new StreamReader(server); | ||
|
||
// The client needs a writer stream to write its processing result | ||
serverWriter = new StreamWriter(server); | ||
} | ||
|
||
public void communicate() | ||
{ | ||
while (true) | ||
{ | ||
String msg = serverReader.ReadLine(); | ||
|
||
if (String.IsNullOrEmpty(msg)) | ||
{ | ||
Console.WriteLine("[Client] Programs is being terminated."); | ||
break; | ||
} | ||
else | ||
{ | ||
Console.WriteLine(msg); | ||
String reverseMsg = String.Join("", msg.Reverse()); | ||
Console.WriteLine(reverseMsg); | ||
serverWriter.WriteLine(reverseMsg); | ||
serverWriter.Flush(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.IO; | ||
using System.IO.Pipes; | ||
using Exercise; | ||
|
||
/* | ||
* This is an example representing how two processes can communicate through NamedPipe | ||
*/ | ||
|
||
namespace Solution | ||
{ | ||
class SolutionIPCNamedServer: IPCNamedServer | ||
{ | ||
NamedPipeClientStream client; | ||
StreamReader clientReader; | ||
StreamWriter clientWriter; | ||
|
||
public SolutionIPCNamedServer(String pipeName) | ||
{ | ||
client = new NamedPipeClientStream(pipeName); | ||
} | ||
|
||
public void prepareServer() | ||
{ | ||
Console.WriteLine("Pipe Server is being executed ..."); | ||
Console.WriteLine("[Server] Enter a message to be reversed by the client (press ENTER to exit)"); | ||
client.Connect(); | ||
clientReader = new StreamReader(client); | ||
clientWriter = new StreamWriter(client); | ||
} | ||
|
||
public void communicate() | ||
{ | ||
while (true) | ||
{ | ||
String input = Console.ReadLine(); | ||
if (String.IsNullOrEmpty(input)) | ||
{ | ||
Console.WriteLine("[Server] Program is being terminated."); | ||
break; | ||
} | ||
else | ||
{ | ||
clientWriter.WriteLine(input); | ||
clientWriter.Flush(); | ||
String clientMsg = clientReader.ReadLine(); | ||
Console.WriteLine(clientMsg); | ||
|
||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System; | ||
using System.Threading; | ||
using Exercise; | ||
|
||
namespace Solution | ||
{ | ||
public class SolutionConcurrentMergeSort: ConcurrentMergeSort | ||
{ | ||
// Implements concurrent version of MergeSort. | ||
public override void sortCon(int[] d) | ||
{ | ||
// Todo 1: Instantiate an object of mergeSort. | ||
SequentialMergeSort mergeSort = new SequentialMergeSort(d); | ||
|
||
mergeSort.printContent("\n Before the concurrent merge-sort \n"); | ||
|
||
// Todo 2: Divide the main array into two pieces: left and right. Where is the middle? | ||
int midPos = d.Length / 2; | ||
|
||
// Todo 3: Give the tasks. Each thread sorts one piece independent from the other. | ||
Thread leftSort = new Thread(() => mergeSort.sortSeq(0,midPos)); | ||
Thread rightSort = new Thread(() => mergeSort.sortSeq(midPos+1, d.Length-1)); | ||
|
||
// Todo 4: Start the threads. | ||
leftSort.Start(); | ||
rightSort.Start(); | ||
|
||
// Todo 5: Join to the working threads. | ||
leftSort.Join(); | ||
rightSort.Join(); | ||
|
||
// Todo 6: Merge the results to create the complete sorted array. Then print the content | ||
mergeSort.merge(0,midPos,d.Length-1); | ||
mergeSort.printContent("\n After the concurrent merge-sort \n"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using Exercise; | ||
|
||
/// <summary> | ||
/// This example implements a concurrent version of finding and printing prime-numbers between two numbers. | ||
/// </summary> | ||
|
||
namespace Solution | ||
{ | ||
public class ConPrimeNumbers: PrimeNumbers | ||
{ | ||
/// <summary> | ||
/// This method | ||
/// </summary> | ||
/// <param name="m"> is the minimum number</param> | ||
/// <param name="M"> is the maximum number</param> | ||
/// <param name="nt"> is the number of threads. For simplicity assume two.</param> | ||
public void runConcurrent(int m, int M) | ||
{ | ||
int nt = 2; // number of devisions | ||
|
||
// Create nt number of threads, define their segments and start them. Join them all to have all the work done. | ||
Stopwatch sw = new Stopwatch(); | ||
|
||
int numTs = nt; | ||
int s = (M - m) / nt; | ||
|
||
Thread[] ts = new Thread[numTs]; | ||
// this loop divides numbers between m up to M to nt segments and each segment is given to a separate thread | ||
for (int i = 0; i < numTs; i++) | ||
{ | ||
int l = m + s * i; | ||
int u = 0; | ||
if (i == numTs - 1) | ||
u = M; | ||
else | ||
u = m + s * (i + 1); | ||
|
||
ts[i] = new Thread(() => PrimeNumbers.printPrimes(l, u)); | ||
} | ||
|
||
sw.Start(); | ||
for (int i = 0; i < numTs; i++) | ||
ts[i].Start(); | ||
|
||
// Here, the main thread can be busy with something else | ||
|
||
for (int i = 0; i < numTs; i++) | ||
ts[i].Join(); | ||
|
||
sw.Stop(); | ||
Console.WriteLine("Time for concurrent version with {0} threads is {1} msec,", nt, sw.ElapsedMilliseconds); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Exercise; | ||
using System.Threading; | ||
|
||
namespace Solution | ||
{ | ||
public class SolutionProcessCreation: ProcessCreation | ||
{ | ||
public override void createProcess() | ||
{ | ||
// First define your process | ||
ProcessStartInfo prInfo = new ProcessStartInfo(); | ||
prInfo.FileName = "../../../../Processes/bin/Debug/netcoreapp3.0/Processes"; // This is an executable program. | ||
prInfo.CreateNoWindow = false; // This means start the process in a new window | ||
prInfo.UseShellExecute = false; | ||
|
||
try | ||
{ | ||
// Start the defined process | ||
using (Process pr = Process.Start(prInfo)) | ||
{ | ||
Thread.Sleep(100); | ||
Console.WriteLine("I can be busy here doing something else ... "); | ||
|
||
pr.WaitForExit(); // Parent process waits here to have the child finished. | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.Out.WriteLine(e.Message); | ||
} | ||
} | ||
} | ||
} |