Skip to content

Commit

Permalink
DiningPhilosopher exercise is modified
Browse files Browse the repository at this point in the history
  • Loading branch information
afshinamighi committed Jan 19, 2021
1 parent 11ad254 commit 9063812
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 23 deletions.
2 changes: 1 addition & 1 deletion ConcurrentCSharp/Asynchronous/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void ExampleTasks()
}
static void Main(string[] args)
{
//Program.ExampleTasks();
Program.ExampleTasks();
}
}
}
11 changes: 9 additions & 2 deletions ConcurrentCSharp/Asynchronous/TaskExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ class ConcurrentTasks
public async Task<int> InvokeAnInefficientAsyncTask()
{
int c = 0;
Task printTask = new Task(() => Operations.PrintConsole(iterations, wait_time));

Task printTask = new Task(() => Operations.PrintConsole(iterations, wait_time)); // io bound
Console.WriteLine(" Now an Async task is going to be called ...");
printTask.Start();

await printTask;
c = Operations.FindPrimes(min_prime, max_parime);

c = Operations.FindPrimes(min_prime, max_parime); // mostly CPU bound task

Console.WriteLine(" All the tasks are ready here ...");
return c;
}
Expand All @@ -112,8 +116,11 @@ public async Task<int> InvokeAnEfficientAsyncTask()
Task printTask = new Task(()=>Operations.PrintConsole(iterations,wait_time));
Console.WriteLine(" Now an Async task is going to be called ...");
printTask.Start();

c = Operations.FindPrimes(min_prime, max_parime);

// check: what will be the result if we do not await for printTask? Comment this line and check the output.

await printTask;
Console.WriteLine(" All the tasks are ready here ...");
return c;
Expand Down
6 changes: 4 additions & 2 deletions ConcurrentCSharp/DiningPhilosophers/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ static void Main(string[] args)
int numPhilosphers = 5 , iteration = 300;
Table table = new Table(numPhilosphers);

//Console.WriteLine("[Dinining Philospher] Dining with one fork is going to start ...");
Console.WriteLine("[Dinining Philospher] Dining with one fork is going to start ...");
table.startOneForkDining(iteration);

//Console.WriteLine("[Dinining Philospher] Dining with two forks is going to start (press enter)...");
//Console.ReadLine();
//table.startTwoForksDining(iteration);
//table.startTwoForksDining(iteration);
//table.startTwoForksDiningSafe(iteration);

}
}
}
8 changes: 5 additions & 3 deletions ConcurrentCSharp/DiningPhilosophers/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ public virtual void eatWithTwoForks()
// solution: no deadlock here, implemented using mutex with timeout
public void eatWithTwoForksSafe()
{
Int32 timeout = 10;
Int32 timeout = 10; // todo: pass timeout as a parameter to the mutexes and check if deadlock happens
Console.WriteLine("[{0} waiting for right fork ...]", number);

if (rightFork.forkMutex.WaitOne())
if (rightFork.forkMutex.WaitOne())
{
rightFork.pick();

Expand All @@ -107,7 +107,7 @@ public void eatWithTwoForksSafe()
}
}

public void startEatingWithOneFork(Object it)
public void startEatingWithOneFork(Object it) // why the parameter has the type Object?
{
int iterations = (int)it;
for (int i = 0; i < iterations; i++)
Expand Down Expand Up @@ -169,7 +169,9 @@ public void startTwoForksDining(int it)
threads[i] = new Thread(philosophers[i].startEatingWithTwoForks);

for (int i = 0; i < threads.Length; i++)
{
threads[i].Start(it);
}

for (int i = 0; i < threads.Length; i++)
threads[i].Join();
Expand Down
1 change: 1 addition & 0 deletions ConcurrentCSharp/Exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ Make your solutions ready.
## Exercises:

1. [~ 20 min] **DiningPhilosophers**: Check the provided solution.
1. In the solution a method (named *startEatingWithTwoForksSafe*) is added to prevent a deadlock. In this method, shared resources are protected using class *Mutex*. Check how instances of this class is used. First, try the code without timeout. You should see the deadlock. Then, pass timeout as a parameter to the methods *WaitOne*. Do you expect a deadlock? Justify your answer.
2. [~ 30 min] **Semaphores**: We have learned how producers consumers can be synchronized with semaphores.
1. Check the provided solution.
2. Wtach this video to see how semaphores can be initialized to exploit full capacity of a shared buffer.
Expand Down
12 changes: 6 additions & 6 deletions ConcurrentCSharp/MergeSort/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Threading;
using Exercise;
//using Solution;
using Solution;

namespace Program
{
Expand All @@ -15,14 +15,14 @@ static void Main(string[] args)

SequentialMergeSort mergeSort = new SequentialMergeSort(arr);

mergeSort.printContent("\n Before the sequential merge-sort ");
mergeSort.printContent("\n Before the sequential merge-sort \n");
mergeSort.sortSeq(0, arr.Length - 1);
mergeSort.printContent("\n After the sequential merge-sort ");
mergeSort.printContent("\n After the sequential merge-sort \n");

// uncomment this only if the solution is available
//Console.WriteLine("\n Now concurrent sort will be running ...");
//SolutionConcurrentMergeSort concMergeSort = new SolutionConcurrentMergeSort();
//concMergeSort.sortCon(arr);
Console.WriteLine("\n Now concurrent sort will be running ...\n");
SolutionConcurrentMergeSort concMergeSort = new SolutionConcurrentMergeSort();
concMergeSort.sortCon(arr);

}
}
Expand Down
1 change: 1 addition & 0 deletions ConcurrentCSharp/PrimeNumbers/Exercise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void runSequential(int m, int M)
sw.Start();
PrimeNumbers.printPrimes(m, M);
sw.Stop();

Console.WriteLine("Time for sequential version is {0} msec,", sw.ElapsedMilliseconds);
}
}
Expand Down
8 changes: 5 additions & 3 deletions ConcurrentCSharp/PrimeNumbers/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Diagnostics;
using System.Threading;
using Exercise;
using Concurrent;
//using Solution;
//using Concurrent;
using Solution;

/// <summary>
/// This example implements a concurrent version of finding and printing prime-numbers between two given numbers.
Expand All @@ -20,7 +20,9 @@ static void Main(string[] args)
ConPrimeNumbers pn = new ConPrimeNumbers();

pn.runSequential(min, max);
Thread.Sleep(2000);

Thread.Sleep(5000);

pn.runConcurrent(min, max);


Expand Down
6 changes: 4 additions & 2 deletions ConcurrentCSharp/PrimeNumbers/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public class ConPrimeNumbers: PrimeNumbers
/// <param name="nt"> is the number of threads. For simplicity assume two.</param>
public void runConcurrent(int m, int M)
{
int nt = 2;
int nt = 2; // number of devisions

// Todo 1: Create nt number of threads, define their segments and start them. Join them all to have all the work done.
// 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;
Expand All @@ -44,7 +44,9 @@ public void runConcurrent(int m, int M)
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();

Expand Down
4 changes: 2 additions & 2 deletions ConcurrentCSharp/ProcessCreation/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Exercise;
//using Solution;
using Solution;

namespace Program
{
Expand All @@ -9,7 +9,7 @@ static void Main(string[] args)
{
new ProcessCreation().createProcess();
//uncomment this only when Solution is available
//new SolutionProcessCreation().createProcess();
new SolutionProcessCreation().createProcess();
}
}
}
4 changes: 2 additions & 2 deletions ConcurrentCSharp/ProcessCreation/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public override void createProcess()
{
// First define your process
ProcessStartInfo prInfo = new ProcessStartInfo();
prInfo.FileName = "dotnet"; // This is an executable program.
prInfo.Arguments = "../../../../Processes/bin/Debug/netcoreapp3.1/Processes.dll";
prInfo.FileName = "ls"; // This is an executable program.
prInfo.Arguments = "-all";
prInfo.CreateNoWindow = false; // This means start the process in a new window
prInfo.UseShellExecute = false;

Expand Down

0 comments on commit 9063812

Please sign in to comment.