LockMutex.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace First
{
public class LockMutex
{
FileStream resource = null;
public LockMutex(String pathToFile)
{
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);
}
public void Read() {
//lock (resource) – am comentat liniile astea pentru a permite cititorilor accesul in paralel
// {
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(resource);
String s = sr.ReadToEnd();
Console.WriteLine(thId.ToString() + ” read : ” + s);
// }
}
public void Write() {
lock (resource)
{
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(resource);
String s = thId + ” :: ” + System.DateTime.Now.ToString();
sw.WriteLine(s);
Console.WriteLine(thId.ToString() + ” wrote : ” + s);
sw.Flush();
}
}
// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila
public void Close()
{
resource.Close();
}
/* destructor care asigura eliberarea resursei – nu e nevoie de el aici
~LockMutex() {
resource.Close();
}*/
}
}
MonitorMutex.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
namespace First
{
class MonitorMutex
{
static object synObj = new object();
FileStream resource = null;
public MonitorMutex(String pathToFile)
{
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);
}
public void Read()
{
// Monitor.Enter(synObj); – am comentat liniile astea pentru a permite cititorilor accesul in paralel
// try
// {
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(resource);
String s = sr.ReadToEnd();
Console.WriteLine(thId.ToString() + ” read : ” + s);
// }
// catch (Exception e){
// Console.WriteLine(e.Message);
// }
// Monitor.Exit(synObj);
}
public void Write()
{
Monitor.Enter(synObj);
try
{
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(resource);
String s = thId + ” :: ” + System.DateTime.Now.ToString();
sw.WriteLine(s);
Console.WriteLine(thId.ToString() + ” wrote : ” + s);
sw.Flush();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Monitor.Exit(synObj);
}
// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila
public void Close() {
resource.Close();
}
/*destructor care asigura eliberarea resursei – nu e nevoie de el aici
~MonitorMutex() {
resource.Close();
}*/
}
}
MutexMutex.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace First
{
class MutexMutex
{
FileStream resource = null;
public Mutex fileMutex;
public MutexMutex(String pathToFile)
{
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);
fileMutex = new Mutex();
}
public void Read()
{
//fileMutex.WaitOne(); – am comentat liniile astea pentru a permite cititorilor accesul in paralel
//try
//{
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(resource);
String s = sr.ReadToEnd();
Console.WriteLine(thId.ToString() + ” read : ” + s);
//} catch (Exception e) {
// Console.WriteLine(“Exception happened: ” + e.Message);
// }
// fileMutex.ReleaseMutex();
}
public void Write()
{
fileMutex.WaitOne();
try {
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(resource);
String s = thId + ” :: ” + System.DateTime.Now.ToString();
sw.WriteLine(s);
Console.WriteLine(thId.ToString() + ” wrote : ” + s);
sw.Flush();
} catch (Exception e) {
Console.WriteLine(“Exception happened: ” + e.Message);
}
fileMutex.ReleaseMutex();
}
// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila
public void Close() {
fileMutex.Close();
resource.Close();
}
/*destructor care asigura eliberarea resursei – nu e nevoie de el aici
~MutexMutex() {
fileMutex.Close();
resource.Close();
}*/
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace First
{
class Program
{
// metoda ce poae fi folosita pentru a vehicula date de dimensiuni mari
public static String getSring(int id) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2000; i++)
sb.AppendLine(id + “wrote: ” + System.DateTime.Now);
return sb.ToString();
}
static void Main(string[] args)
{
//s-a dat accesul cititorilorx in paralel la resursa
Thread[] ta = new Thread[10];
LockMutex mx = null;
MonitorMutex mm = null;
MutexMutex mxmx = null;
SemaphoreMutex sm = null;
ReaderWriterLockMutex rwm = null;
mx = new LockMutex(“test.txt”);
Thread firstWriter = new Thread(mx.Write);
firstWriter.Start();
firstWriter.Join();
for (int i = 0; i < 10; ++i)
{
int opt = new Random().Next(2);
ta[i] = opt == 0 ? new Thread(mx.Read) : new Thread(mx.Write);
ta[i].Start();
}
Console.WriteLine(“\n>>> Press any key to finish lock example and release resources…<<<”);
Console.ReadKey();
Console.WriteLine(“————————————————————”);
mx.Close();
mm = new MonitorMutex(“test.txt”);
firstWriter = new Thread(mm.Write);
firstWriter.Start();
firstWriter.Join();
for (int i = 0; i < 10; ++i)
{
int opt = new Random().Next(2);
ta[i] = opt == 0 ? new Thread(mm.Read) : new Thread(mm.Write);
ta[i].Start();
}
Console.WriteLine(“\n>>> Press any key to finish Monitor example and release resources…<<<”);
Console.ReadKey();
Console.WriteLine(“————————————————————”);
mm.Close();
mxmx = new MutexMutex(“test.txt”);
firstWriter = new Thread(mxmx.Write);
firstWriter.Start();
firstWriter.Join();
for (int i = 0; i < 10; ++i)
{
int opt = new Random().Next(2);
ta[i] = opt == 0 ? new Thread(mxmx.Read) : new Thread(mxmx.Write);
ta[i].Start();
}
Console.WriteLine(“\n>>> Press any key to finish Mutex example and release resources…<<<”);
Console.ReadKey();
Console.WriteLine(“————————————————————”);
mxmx.Close();
sm = new SemaphoreMutex(“test.txt”);
firstWriter = new Thread(sm.Write);
firstWriter.Start();
firstWriter.Join();
for (int i = 0; i < 10; ++i)
{
int opt = new Random().Next(2);
ta[i] = opt == 0 ? new Thread(sm.Read) : new Thread(sm.Write);
ta[i].Start();
}
Console.WriteLine(“\n>>> Press any key to finish Semaphore example and release resources…<<<”);
Console.ReadKey();
Console.WriteLine(“————————————————————”);
sm.Close();
rwm = new ReaderWriterLockMutex(“test.txt”);
firstWriter = new Thread(new ParameterizedThreadStart(rwm.Write));
firstWriter.Start(ReaderWriterLockMutex.writerTimeouts);
firstWriter.Join();
for (int i = 0; i < 10; ++i)
{
int opt = new Random().Next(2);
if (opt == 0)
{
ta[i] = new Thread(new ParameterizedThreadStart(rwm.Read));
ta[i].Start(ReaderWriterLockMutex.readerTimeouts);
}
else {
ta[i] = new Thread(new ParameterizedThreadStart(rwm.Write));
ta[i].Start(ReaderWriterLockMutex.writerTimeouts);
}
}
Console.WriteLine(“\n>>> Press any key to finish ReaderWriterLock example and release resources…<<<”);
Console.ReadKey();
Console.WriteLine(“————————————————————”);
rwm.Close();
Console.WriteLine(“\n>>> Press any key to exit…<<<”);
Console.ReadKey();
}
}
}
ReaderWriterLockMutex.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace First
{
class ReaderWriterLockMutex
{
FileStream resource = null;
ReaderWriterLock readerWriterLock = null;
public static int readerTimeouts = 0;
public static int writerTimeouts = 0;
int reads = 0;
int writes = 0;
public ReaderWriterLockMutex(String pathToFile)
{
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);
readerWriterLock = new ReaderWriterLock();
}
public void Read(object obj)
{
int timeOut = (int)obj;
try
{
readerWriterLock.AcquireReaderLock(timeOut);
try
{
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(resource);
String s = sr.ReadToEnd();
Console.WriteLine(thId.ToString() + ” read : ” + s);
Interlocked.Increment(ref reads);
}
finally
{
readerWriterLock.ReleaseReaderLock();
}
}
catch (ApplicationException)
{
Interlocked.Increment(ref readerTimeouts);
}
}
public void Write(object obj)
{
int timeOut = (int)obj;
try
{
readerWriterLock.AcquireWriterLock(timeOut);
try
{
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(resource);
String s = thId + ” :: ” + System.DateTime.Now.ToString();
sw.WriteLine(s);
Console.WriteLine(thId.ToString() + ” wrote : ” + s);
sw.Flush();
Interlocked.Increment(ref writes);
}
finally
{
readerWriterLock.ReleaseWriterLock();
}
}
catch (ApplicationException)
{
Interlocked.Increment(ref writerTimeouts);
}
}
// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila
public void Close()
{
resource.Close();
}
/*destructor care asigura eliberarea resursei – nu e nevoie de el aici
~ReaderWriterLockMutex() {
resource.Close();
}*/
}
}
SemaphoreMutex.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace First
{
class SemaphoreMutex
{
FileStream resource = null;
Semaphore semaphore = null;
public SemaphoreMutex(String pathToFile)
{
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);
semaphore = new Semaphore(10, 10);
}
public void Read()
{
//semaphore.WaitOne(); – am comentat liniile astea pentru a permite cititorilor accesul in paralel
//try
// {
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(resource);
String s = sr.ReadToEnd();
Console.WriteLine(thId.ToString() + ” read : ” + s);
// } catch (Exception e) {
// Console.WriteLine(“Exception happened: ” + e.Message);
//}
//semaphore.Release();
}
public void Write()
{
semaphore.WaitOne();
try
{
int thId = Thread.CurrentThread.ManagedThreadId;
resource.Seek(0, SeekOrigin.End);
StreamWriter sw = new StreamWriter(resource);
String s = thId + ” :: ” + System.DateTime.Now.ToString();
sw.WriteLine(s);
Console.WriteLine(thId.ToString() + ” wrote : ” + s);
sw.Flush();
}
catch (Exception e)
{
Console.WriteLine(“Exception happened: ” + e.Message);
}
semaphore.Release();
}
// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila
public void Close()
{
semaphore.Close();
resource.Close();
}
/*destructor care asigura eliberarea resursei – nu e nevoie de el aici
~SemaphoreMutex() {
semaphore.Close();
resource.Close();
}*/
}
}