Browse Source

Fixed merge conflict.

master
Cameron Weinfurt 3 years ago
parent
commit
7e3f7be296
  1. 64
      Cons_Prod.c
  2. 4
      Makefile
  3. 16
      queue.c
  4. 2
      queue.h

64
Cons_Prod.c

@ -12,9 +12,38 @@
#define TIME_TO_CONSUME 1 // second
#define LOOP 10
/***** Function prototypes *****/
void producer (queue *q);
void consumer (queue *q);
void producer(queue *q)
{
for (int i = 0; i < LOOP; ++i)
{
/*
* TODO for students: obtain the lock and release the lock somewhere
*/
while (queueIsFull(q));
enqueue(q, i+1);
printf ("producer: produced %d th.\n",i+1);
sleep_microseconds(PRODUCER_SLEEP_S * 1000000);
}
}
void consumer(queue *q)
{
// Simulate consumers' random arrival
sleep_microseconds((rand()%LOOP) * 1000000);
/*
* TODO for students: obtain the lock and release the lock somewhere
*/
while (queueIsFull(q));
sleep_microseconds(CONSUMER_SLEEP_S * 1000000);
printf ("------------------------------------>consumer: recieved %d.\n", dequeue(q));
}
int main()
{
@ -55,32 +84,3 @@ int main()
return 0;
}
void producer(queue *q)
{
for (int i = 0; i < LOOP; ++i)
{
while (queueIsFull(q));
enqueue(q, i+1);
printf ("producer: produced %d th.\n",i+1);
sleep_microseconds(TIME_TO_PRODUCE * 1000000);
}
}
void consumer(queue *q)
{
/* Simulate Consumers' random arrival */
sleep_microseconds((rand()%LOOP) * 1000000);
// TODO for students: obtain the lock and release the lock somewhere
while (queueIsFull(q));
sleep_microseconds(TIME_TO_CONSUME * 1000000);
d = dequeue(q);
printf ("------------------------------------>consumer: recieved %d.\n", d);
}

4
Makefile

@ -1,8 +1,8 @@
build: Cons_Prod.c
gcc Cons_Prod.c queue.c thread.c -O2 -DUNIX -pthread -o Cons_Prod
gcc Cons_Prod.c thread.c queue.c -O2 -DUNIX -pthread -o Cons_Prod
debug: Cons_Prod.c
gcc Cons_Prod.c queue.c thread.c -g -DUNIX -pthread -o Cons_Prod
gcc Cons_Prod.c thread.c queue.c -g -DUNIX -pthread -o Cons_Prod
run: Cons_Prod
./Cons_Prod

16
queue.c

@ -30,21 +30,29 @@ void queueDestroy(queue *q)
void queueLock(queue *q)
{
#ifdef UNIX
// TODO for students: Implement locking for UNIX systems.
/*
* TODO for students: Implement locking for UNIX systems.
*/
#endif
#ifdef WINDOWS
// TODO for students: Implement locking for Windows systems.
/*
* TODO for students: Implement locking for Windows systems.
*/
#endif
}
void queueUnlock(queue *q)
{
#ifdef UNIX
// TODO for students: Implement unlocking for UNIX systems.
/*
* TODO for students: Implement unlocking for UNIX systems.
*/
#endif
#ifdef WINDOWS
// TODO for students: Implement unlocking for Windows systems.
/*
* TODO for students: Implement unlocking for Windows systems.
*/
#endif
}

2
queue.h

@ -22,7 +22,7 @@ static inline int queueLen(queue *q) {return q->len;}
static inline int queueIsFull(queue *q) {return (q->len == QUEUESIZE);}
static inline int queueIsEmpty(queue *q) {return (q->len == 0);}
// Add item to queue
// Add item to the queue
static inline void enqueue(queue *q, int in)
{
q->buff[q->len] = in;

Loading…
Cancel
Save