/* NOTE: DO NOT REMOVE THIS COMMENT!! CSE 2431 buffer.c SU 21 22389 */
#include
#include
#include
#include
#define BUFFER_SIZE 7
/* Student name: PUT YOUR NAME HERE */
typedef unsigned int buffer_item;
buffer_item buffer[BUFFER_SIZE]; /* shared data buffer */
/* Declaration of pthread mutex locks here; these are shared by threads. */
/* See the lab instructions to find out how to initialize the lock, */
/* and also how to acquire the lock before entering a critical section, */
/* and how to release the lock after exiting a critical section. */
pthread_mutex_t prod_mutex; /* lock used to ensure only one producer thread */
/* can access buffer at any given time */
pthread_mutex_t cons_mutex; /* lock used to ensure only one consumer thread */
/* can access buffer at any given time */
pthread_mutex_t output_mutex; /* lock used to ensure only one thread */
/* can write output at any given time */
sem_t empty; /* shared data empty counting semaphore */
sem_t full; /* shared data full counting semaphore */
int in = 0; /* shared data to be used by producers for next position in */
/* buffer to insert produced item */
int out = 0; /* shared data to be used by consumers for next position in */
/* buffer from which to consume item */
unsigned int seed = 100; /* seed value for rand_r; initialized to 100 */
unsigned int *seedp = &seed; /* pointer to seed for rand_r(), a */
/* re-entrant, and therefore thread-safe, */
/* version of rand() */
void insert_item (buffer_item);
void remove_item (buffer_item *);
/* function shared by all producer threads */
void *producer (void *param) {
buffer_item rand;
while (1) {
/* generate a random number between 0 and 99 */
rand = rand_r(seedp) % 100;
/* sleep for rand * 10000 microseconds */
usleep(rand * 10000);
/* insert item into buffer */
insert_item(rand);
}
}
/* function shared by all consumer threads */
void *consumer (void *param) {
buffer_item rand;
while (1) {
/* generate a random number between 0 and 99 */
rand = rand_r(seedp) % 100;
/* sleep for rand * 10000 microseconds */
usleep(rand * 10000);
/* remove item from buffer */
remove_item(&rand);
}
}
/* function shared by all producer threads */
void insert_item(buffer_item item) {
/* fill in code to insert a single item into buffer */
/* output_mutex lock should be held before writing output */
printf(“producer produced %d\n”, item);
/* output_mutex lock should be released after writing output */
}
/* function shared by all consumer threads */
void remove_item(buffer_item *item) {
/* fill in code to remove a single value from buffer and place it in *item */
/* output_mutex lock should be held before writing output */
printf(“\tconsumer consumed %d\n”, *item);
/* output_mutex lock should be released after writing output */
}
int main(int argc, char *argv[]) {
int sleep_time = atoi(argv[1]);
int num_prod_threads = atoi(argv[2]);
int num_cons_threads = atoi(argv[3]);
int i;
/* initialize 3 mutex locks using pthread_mutex_init */
/* initialize full semaphore using sem_init */
/* initialize empty semaphore using sem_init */
/* initialize all buffer_items to 0 */
/* Create single thread id (tid) and attribute variable (attr) for the multiple threads */
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
/* create num_prod_threads with loop */
/* create num_cons_threads with loop */
/* sleep for number of seconds equal to second param on command line */
sleep(sleep_time);
return 0;
}