Browse Source

Fix issues in queue and make comments more clear on what to do

master
stew3254 2 years ago
parent
commit
420fa2870b
  1. 16
      main.c
  2. 80
      queue.c
  3. 24
      queue.h
  4. 4
      thread.h

16
main.c

@ -27,12 +27,12 @@ void producer(queue *q)
// Pretend that expensive operations are occuring by sleeping.
sleep_milliseconds(TIME_TO_PRODUCE);
// Busy loop while the queue is full.
// Busy loop while the queue is full. (HINT THIS IS AN UNSYNCHRONIZED READ)
while (queueIsFull(q));
// Put the produced item onto the queue and print that the enqueue occured.
enqueue(q, i+1);
printf ("producer: produced %d th.\n",i+1);
printf("producer: produced %d th.\n",i+1);
}
}
@ -45,16 +45,16 @@ void producer(queue *q)
*/
void consumer(queue *q)
{
// Simulate the consumer's random arrival
sleep_milliseconds((rand()%LOOP) * 1000);
// Simulate the consumer's random quick arrival (Makes race conditions prominent)
sleep_milliseconds((rand()%LOOP) * TIME_TO_CONSUME/LOOP);
// Wait for an item to be in the queue.
// Wait for an item to be in the queue. (HINT THIS IS AN UNSYNCHRONIZED READ)
while (queueIsEmpty(q));
// Pretend to consume the
// Pretend to consume
sleep_milliseconds(TIME_TO_CONSUME);
printf ("------------------------------------>consumer: recieved %d.\n", dequeue(q));
printf("------------------------------------>consumer: recieved %d.\n", dequeue(q));
}
int main()
@ -64,7 +64,7 @@ int main()
if (q == NULL)
{
fprintf(stderr, "main: Queue Init failed.\n");
exit (1);
exit(1);
}
// Seed the random number generator using the system clock.

80
queue.c

@ -3,56 +3,70 @@
queue *queueInit()
{
queue *q;
if ((q = (queue *) malloc(sizeof(queue))) == NULL)
queue *q;
if ((q = (queue *) malloc(sizeof(queue))) == NULL)
return NULL;
q->len = q->index = 0;
/*
* TODO for students: Remember to allocate your lock here if it needs
* to be dynamically allocated
*/
q->len = q->index = 0;
/*
* TODO for students: Initialize the locks here
#ifdef UNIX
/*
* TODO for students: Initialize the lock here
*/
return q;
#endif
#ifdef WINDOWS
/*
* TODO for students: Initialize the lock here
*/
#endif
return q;
}
void queueDestroy(queue *q)
{
/*
/*
* TODO for students: free the locks here
*/
// Free memory used for the queue
free(q);
free(q);
}
void queueLock(queue *q)
{
#ifdef UNIX
/*
* TODO for students: Implement locking for UNIX systems.
*/
#endif
#ifdef WINDOWS
/*
* TODO for students: Implement locking for Windows systems.
*/
#endif
#ifdef UNIX
/*
* TODO for students: Implement locking for UNIX systems.
*/
#endif
#ifdef WINDOWS
/*
* TODO for students: Implement locking for Windows systems.
*/
#endif
}
void queueUnlock(queue *q)
{
#ifdef UNIX
/*
* TODO for students: Implement unlocking for UNIX systems.
*/
#endif
#ifdef WINDOWS
/*
* TODO for students: Implement unlocking for Windows systems.
*/
#endif
#ifdef UNIX
/*
* TODO for students: Implement unlocking for UNIX systems.
*/
#endif
#ifdef WINDOWS
/*
* TODO for students: Implement unlocking for Windows systems.
*/
#endif
}

24
queue.h

@ -3,13 +3,27 @@
#define QUEUESIZE 5
#ifdef UNIX
#include <pthread.h>
#include <unistd.h>
/*
* TODO for students: Define your lock type here
*/
#endif
#ifdef WINDOWS
#include <windows.h>
/*
* TODO for students: Define your lock type here
*/
#endif
typedef struct
{
int buff[QUEUESIZE];
int index;
int len;
/*
* TODO for students: Declare the locks here
/*
* TODO for students: Implement your lock here
*/
} queue;
@ -25,16 +39,16 @@ static inline int queueIsEmpty(queue *q) {return (q->len == 0);}
// Add item to the queue
static inline void enqueue(queue *q, int in)
{
q->buff[q->len] = in;
q->len = (q->len + 1) % QUEUESIZE;
q->buff[(q->index + q->len++) % QUEUESIZE] = in;
}
// Remove item from queue
static inline int dequeue(queue *q)
{
int n = q->buff[q->index];
if ((q->index = (q->index - 1) % QUEUESIZE) == -1)
if ((q->index = (q->index + 1) % QUEUESIZE) == -1)
q->index = QUEUESIZE - 1;
--q->len;
return n;
}

4
thread.h

@ -6,8 +6,6 @@
#include <unistd.h>
#define thread_t pthread_t
#define mutex_t pthread_mutex_t
#define sleep_milliseconds(ms) usleep(ms * 1000)
#endif
@ -17,8 +15,6 @@
#include <windows.h>
#define thread_t HANDLE
#define mutex_t HANDLE
#define sleep_milliseconds(ms) Sleep(ms)
#endif

Loading…
Cancel
Save