Skip to content

Commit

Permalink
w3 update
Browse files Browse the repository at this point in the history
  • Loading branch information
minuahr committed Nov 16, 2022
1 parent 97400c1 commit 095b41c
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
25 changes: 25 additions & 0 deletions PrimeNumbers/Concurrent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Diagnostics;
using System.Threading;
using Exercise;

namespace Concurrent
{
public class ConPrimeNumbers : PrimeNumbers
{
public ConPrimeNumbers()
{
}
/// <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)
{
// Todo 1: Create two threads, define their segments and start them. Join them all to have all the work done.
}

}
}
48 changes: 48 additions & 0 deletions PrimeNumbers/Exercise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Diagnostics;
using System.Threading;

namespace Exercise
{
public class PrimeNumbers
{
public PrimeNumbers() { }

public static void printPrimes(int lower, int upper)
{
Boolean isPrime = true;

if (lower > upper)
{
Console.WriteLine("invalid inputs");
return;
}

for (int n = lower; n <= upper; n++)
{
if (n % 1000 == 0) // This condition fakes an IO operation.
Thread.Sleep(100);

isPrime = true; // assume n is a prime number

for (int i = 2; i < n && isPrime; i++)
if (n % i == 0)
isPrime = false; // our assumption was not correct

if (isPrime)
Console.WriteLine("{0}",n); // report prime number if our assumption was correct
}

}

public void runSequential(int m, int M)
{
Stopwatch sw = new Stopwatch();
sw.Start();
PrimeNumbers.printPrimes(m, M);
sw.Stop();

Console.WriteLine("Time for sequential version is {0} msec,", sw.ElapsedMilliseconds);
}
}
}
8 changes: 8 additions & 0 deletions PrimeNumbers/PrimeNumbers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
31 changes: 31 additions & 0 deletions PrimeNumbers/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Diagnostics;
using System.Threading;
using Exercise;
//using Concurrent;
using Solution;

/// <summary>
/// This example implements a concurrent version of finding and printing prime-numbers between two given numbers.
/// </summary>
namespace Program
{
class Program
{
static void Main(string[] args)
{

int min = 5, max = 100000;

ConPrimeNumbers pn = new ConPrimeNumbers();

pn.runSequential(min, max);

Thread.Sleep(5000);

pn.runConcurrent(min, max);


}
}
}
58 changes: 58 additions & 0 deletions PrimeNumbers/Solution.cs
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);
}

}
}

0 comments on commit 095b41c

Please sign in to comment.