Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- *
- * CS 441/541: Bounded Buffer (Project 4)
- *
- */
- #include "bounded-buffer.h"
- int main(int argc, char * argv[]) {
- int ret;
- time_t t;
- // Initialize
- elements_produced = 0;
- elements_consumed = 0;
- in = 0;
- out = 0;
- sleep_time = atoi(argv[1]);
- num_producer_threads = atoi(argv[2]);
- num_consumer_threads = atoi(argv[3]);
- srand((unsigned) time(&t));
- // Check for BUFFER_SIZE argument
- if(argc > 4) {
- buffer_size = atoi(argv[4]);
- }
- // Create num of threads based on user input
- pthread_t producer_threads[num_producer_threads];
- pthread_t consumer_threads[num_consumer_threads];
- if( 0 != (ret = init_buffer()) ){
- fprintf(stderr, "Error initalizing buffer");
- }
- print_inital_info();
- int rc, i;
- // Create producer threads
- for(i = 0; i < num_producer_threads; i++){
- //printf("In main creating producer thread: %d\n", i);
- rc = pthread_create(&producer_threads[i], NULL, producer, (void *)(intptr_t)i);
- if(0 != rc){
- printf("ERROR");
- exit(-1);
- }
- }
- // Create consumer threads
- for(i = 0; i < num_consumer_threads; i++){
- //printf("In main creating consumer thread: %d\n", i);
- rc = pthread_create(&consumer_threads[i], NULL, consumer, (void *)(intptr_t)i);
- if(0 != rc){
- printf("ERROR");
- exit(-1);
- }
- }
- sleep(sleep_time);
- printf("-------------------------------\n");
- printf("Produced | %d\n", elements_produced);
- printf("Consumed | %d\n", elements_consumed);
- free(buffer);
- return 0;
- }
- int insert_item(int item){
- buffer[in] = item;
- in = (in+1) % buffer_size;
- elements_produced++;
- return 0;
- }
- int remove_item(){
- if (buffer[out] == -1){
- int start_idx = out;
- int x;
- for(x = out; x != start_idx; x++){
- x = (x+1) % buffer_size;
- //Found our next non-negative index
- if(buffer[x] != -1){
- out = x;
- break;
- }
- }
- // Found no non-negative values to consume
- return -1;
- }
- buffer[out] = -1;
- out = (out+1) % buffer_size;
- elements_consumed++;
- return 0;
- }
- void *producer(void *threadid){
- int tid = (intptr_t)threadid;
- int nextProduced;
- long s_timer;
- while(TRUE) {
- s_timer = rand() % 1000000;
- usleep(s_timer);
- nextProduced = rand() % 10;
- semaphore_wait(&empty);
- semaphore_wait(&mutex);
- insert_item(nextProduced);
- print_buffer(1, tid, nextProduced);
- semaphore_post(&mutex);
- semaphore_post(&full);
- }
- }
- void *consumer(void *threadid){
- int tid = (intptr_t)threadid;
- int nextConsumed;
- long s_timer;
- while(TRUE) {
- semaphore_wait(&full);
- semaphore_wait(&mutex);
- nextConsumed = buffer[out];
- remove_item();
- print_buffer(0, tid, nextConsumed);
- semaphore_post(&mutex);
- semaphore_post(&empty);
- s_timer = rand() % 1000000;
- usleep(s_timer);
- }
- }
- void print_inital_info(){
- printf("Buffer Size: %d\n", buffer_size);
- printf("Time To Live (seconds): %d\n", sleep_time);
- printf("Number of Producer threads: %d\n", num_producer_threads);
- printf("Number of Consumer threads: %d\n", num_consumer_threads);
- printf("-------------------------------\n");
- printf("Initial Buffer:\t [");
- // Printing buffer values with carrots
- int j;
- for(j = 0; j < buffer_size; j++){
- printf("%d", buffer[j]);
- if(j == in){
- printf("^");
- }
- if(j == out){
- printf("v");
- }
- printf("\t");
- }
- printf("]\n");
- }
- void print_buffer(int thread_type, int thread_id, int item_entered){
- // thread_type of 0 is consumer and 1 is producer
- if(thread_type){
- printf("Producer %d: Total %d, Value %d ", thread_id, elements_produced, item_entered);
- }
- else {
- printf("Consumer %d: Total %d, Value %d ", thread_id, elements_consumed, item_entered);
- }
- // Printing the buffer values with carrots
- int j;
- printf("[");
- for(j = 0; j < buffer_size; j++){
- printf("%d", buffer[j]);
- if(j == in){
- printf("^");
- }
- if(j == out){
- printf("v");
- }
- printf("\t");
- }
- printf("]\n");
- }
- int init_buffer(){
- int y, ret;
- buffer = malloc(buffer_size * sizeof(int));
- // Init buffer to have all -1 values
- for(y = 0; y < buffer_size; y++){
- buffer[y] = -1;
- }
- // Init semaphores
- if( 0 != (ret = semaphore_create(&mutex, 1)) ) {
- fprintf(stderr, "Error: semaphore_create() failed with %d\n", ret);
- return -1;
- }
- if( 0 != (ret = semaphore_create(&full, 0)) ) {
- fprintf(stderr, "Error: semaphore_create() failed with %d\n", ret);
- return -1;
- }
- if( 0 != (ret = semaphore_create(&empty, buffer_size)) ) {
- fprintf(stderr, "Error: semaphore_create() failed with %d\n", ret);
- return -1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement