CS计算机代考程序代写 //

//
// Written by Mike Feeley, University of BC, 2010-2014
// Do not redistribute any portion of this code without permission.
//

#include
#include
#include
#include “uthread.h”
#include “uthread_mutex_cond.h”

#ifndef NUM_PROCESSORS
#define NUM_PROCESSORS 4
#endif
#ifndef NUM_THREAD_PAIRS
#define NUM_THREAD_PAIRS 2
#endif
#ifndef QUEUE_SIZE
#define QUEUE_SIZE 16
#endif
#ifndef NUM_ITERATIONS
#define NUM_ITERATIONS (QUEUE_SIZE*4)
#endif
#ifndef PRINT_EACH_THREAD_DONE
#define PRINT_EACH_THREAD_DONE 1
#endif
#ifndef PRINT_EACH_ITERATION
#define PRINT_EACH_ITERATION 1
#endif

/**
* Bounded Buffer
*/

int queue [QUEUE_SIZE], queue_head=0, queue_tail=0;
uthread_mutex_t queue_mutex;
uthread_cond_t queue_not_empty;
uthread_cond_t queue_not_full;

/**
* queue_init
*/

void queue_init () {
queue_mutex = uthread_mutex_create ();
queue_not_empty = uthread_cond_create (queue_mutex);
queue_not_full = uthread_cond_create (queue_mutex);
}

/**
* queue_enqueue
*/

void queue_enqueue (int val) {
int i;
uthread_mutex_lock (queue_mutex);
while ( (queue_tail+1) % QUEUE_SIZE == queue_head) {
uthread_cond_wait (queue_not_full);
}
queue[queue_tail] = val;
queue_tail = (queue_tail + 1) % QUEUE_SIZE;
uthread_cond_signal (queue_not_empty);
uthread_mutex_unlock (queue_mutex);
}

/**
* queue_dequeue
*/

int queue_dequeue () {
int i;
int val;

uthread_mutex_lock (queue_mutex);
while (queue_head == queue_tail) {
uthread_cond_wait (queue_not_empty);
}
val = queue[queue_head];
queue_head = (queue_head + 1) % QUEUE_SIZE;
uthread_cond_signal (queue_not_full);
uthread_mutex_unlock (queue_mutex);

return val;
}

/**
* producer
*/

void* producer (void* id) {
int i;
for (i=0; i