Skip to content

Req3 #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
6
3317
7
1877
877
8
5345
9
Expand Down
91 changes: 91 additions & 0 deletions q1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Threading;
public class MessageQueue<T> : IDisposable
{
private readonly int _QUEUE_SIZE;
// a semaphore object is used to notify enqueue happend signal to the dequeue
// subrouting
private Semaphore _semaphore;
// an internal queue data structure holds the messages in a queue
private Queue<T> _internalQueue;
// a private object that is used to acquire a lock on it to ensure synchronous
// execution of some operations in multithreaded environment
private object _syncLock;
/**************************************************************************
* Construct the message queue object with the maximum size limit. *
* If no of messages in the queue meets the maximum size of the queue, any *
* subsequent enqueue will be discareded. i.e. those message will be lost *
* until you dequeue any message i.e. provide a room for new message to *
* enter the queue. *
**************************************************************************/
public MessageQueue(int queueSize)
{
_syncLock = new object();
_QUEUE_SIZE = queueSize;
_internalQueue = new Queue<T>(_QUEUE_SIZE);
_semaphore = new Semaphore(0, _QUEUE_SIZE);
}
/***********************************************************************
* Reset the MessageQueue *
***********************************************************************/
public void Reset()
{
// instantiate the semaphore with initial count 0 i.e. the semaphore is
// entirely woned and there is no room to enter
_semaphore = new Semaphore(0, _QUEUE_SIZE);
// clear all existing messages from the message queue
_internalQueue.Clear();
}
/**********************************************************************
* Enqueue message in to the Message Queue *
**********************************************************************/
public void EnqueueMessage(T message)
{
lock (_syncLock)
{
if (_semaphore != null && message != null)
{
try
{
// try to provide a room in the semaphore so that DequeueMessage
// can enter into it
_semaphore.Release();
// now enqueue the message in to the internal queue data structure
_internalQueue.Enqueue(message);
}
catch { }
}
}
}
/*********************************************************************
* Dequeue message from the Message Queue *
*********************************************************************/
public T DequeueMessage()
{
// try to acquire a room in the semaphore and sleep until the room is available
_semaphore.WaitOne();
// if any room could be acquired, proceed to next step. i.e. dequeue message from
// the internal queue and return it
lock (_syncLock)
{
T message = _internalQueue.Dequeue();
return message;
}
}

/********************************************************************
* Dispose the Message Queue object *
********************************************************************/
public void Dispose()
{
// if the semaphore is not null, close it and set it to null
if (_semaphore != null)
{
_semaphore.Close();
_semaphore = null;
}
// clear the items of the internal queue
_internalQueue.Clear();
}
}
4 changes: 2 additions & 2 deletions q3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void mergeSort(int a[], int l, int h)
{
int i, len=(h-l+1);

// Using insertion sort for small sized array
// Using selection sort for small sized array
if (len<=5)
{
selectionSort(a+l, len);
Expand Down Expand Up @@ -197,4 +197,4 @@ int main()


return 0;
}
}