Preview

Consumer Producer Problem

Good Essays
Open Document
Open Document
462 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Consumer Producer Problem
//buffer.h typedef int buffer_item;
#define BUFFER_SIZE 5
-----------------------------------------------------
// consumer-producer.c
#include
#include
#include
#include
#include "buffer.h"

#define RAND_DIVISOR 100000000 //random function setup

pthread_mutex_t mutex; //setup mutex lock

sem_t full, empty; //setup binary semaphores

buffer_item buffer[BUFFER_SIZE]; //setup buffers

int counter; //setup buffer counter

pthread_t tid; //setup thread ID pthread_attr_t attr; //setup thread attributes

void *producer(void *param); //producer thread void *consumer(void *param); //consumer thread void initializeData() { //Setup locks, semaphores, and counters

pthread_mutex_init(&mutex, NULL); //setup mutex lock sem_init(&full, 0, 0); //setup full semaphore and init to 0 sem_init(&empty, 0, BUFFER_SIZE); //setup empty semaphore and init to BUFFER_SIZE pthread_attr_init(&attr); //Get default attributes counter = 0;
}

//Producer Function void *producer(void *param) { buffer_item item;

while(TRUE) { int rNum = rand() / RAND_DIVISOR; //Generate random number (Kochan) sleep(rNum); //sleep for a random interval of time

item = rand(); //random number generator sem_wait(&empty);//take empty lock pthread_mutex_lock(&mutex);//Take mutex lock

if(insert_item(item)) { fprintf(stderr, " Producer report error\n"); } else { printf("Producer Thread produced: %d\n", item); } pthread_mutex_unlock(&mutex); //release mutex lock sem_post(&full); //signal full semaphore }
}

// Consumer Function void *consumer(void *param) { buffer_item item;

while(TRUE) { int rNum = rand() / RAND_DIVISOR; //Call a random number and divide by the divisor to create a random sleep period (Kochan) sleep(rNum);

/* aquire the full lock */ sem_wait(&full); /* aquire



Cited: * * Kochan, Stephen G. Programming in C: [a Complete Introduction to the C Programming Language]. Indianapolis, IN: Sams, 2009. Print. */

You May Also Find These Documents Helpful

  • Powerful Essays

    The purpose of this Lab. is to familiarize student how to solve practical problems programmatically; they will practice on elementary programming using primitive data types, variables, constants, operators, expressions, and input and output. Also, they will learn how to diagnose errors that may occur when a program is compiled or executed. There are some exercises, through which they will understand the concept learn in this chapter.…

    • 2338 Words
    • 10 Pages
    Powerful Essays
  • Satisfactory Essays

    This week learning assignment is based on Unit 8 Chapter 6 reading of the textbook about the elements of a computing system that focus on the selections relevant for supporting programs with symbols.…

    • 288 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    Pt1420 Unit 1 Assignment 2

    • 1305 Words
    • 6 Pages

    Das, D., Gregersen, E., Hosch, L., Lotha, G., Sampaolo, M., Sinha, S. (2014). C++. In Encyclopedia Britannica.…

    • 1305 Words
    • 6 Pages
    Powerful Essays
  • Good Essays

    C Is difficult, because the control flow is out of the hands of the application programmer…

    • 1508 Words
    • 7 Pages
    Good Essays
  • Good Essays

    Comp 220

    • 1463 Words
    • 6 Pages

    Pointers also have the requirement that the pointer type must be of the same data type as the variable, or the data that it points to or holds the address of. The power of pointers also hints at the potential complexity of their use, which is why this lab is focused almost entirely on several different aspects and uses of pointers. The lab also introduces pointer arrays and pointers to pointers.…

    • 1463 Words
    • 6 Pages
    Good Essays
  • Powerful Essays

    EAS230Syllabus

    • 1748 Words
    • 8 Pages

    C++ programming: editing, compiling, user I/O, variables (ints, doubles, char, strings, booleans), loops, decisions, functions, pointers, arrays, tables, databases, sorting.…

    • 1748 Words
    • 8 Pages
    Powerful Essays
  • Satisfactory Essays

    Lab 7 Student

    • 1242 Words
    • 7 Pages

    This lab accompanies Chapter 5 (pp. 163-183 and pp. 196-201) of Starting Out with Programming Logic & Design.…

    • 1242 Words
    • 7 Pages
    Satisfactory Essays
  • Good Essays

    Concept Programing

    • 443 Words
    • 3 Pages

    3. (15%) Design a state diagram to recognize one form of the comments of the C-based programming languages,…

    • 443 Words
    • 3 Pages
    Good Essays
  • Powerful Essays

    It 210

    • 2960 Words
    • 12 Pages

    Prelude to Programming: Concepts and Design, Fifth Edition, by Stewart Venit and Elizabeth Drake. Published by Addison-Wesley. Copyright © 2011 by Pearson Education, Inc.…

    • 2960 Words
    • 12 Pages
    Powerful Essays
  • Satisfactory Essays

    Unit 8 Research Assignment 1: Exploring the Difference between Using Count-Controlled Loops and While Loops…

    • 391 Words
    • 2 Pages
    Satisfactory Essays
  • Powerful Essays

    It 210 Weeks

    • 2760 Words
    • 12 Pages

    Venit, S., & Drake, E. (2011). Prelude to programming: Concepts and design (5th ed.). Boston, MA: Addison-Wesley.…

    • 2760 Words
    • 12 Pages
    Powerful Essays
  • Good Essays

    Chapter 6 solutions

    • 4145 Words
    • 41 Pages

    b. Modify the reverse-display program so that the user can enter up to 10 numbers…

    • 4145 Words
    • 41 Pages
    Good Essays
  • Good Essays

    It210 Syllabus

    • 2333 Words
    • 10 Pages

    Objectives 1.1 Describe the importance of using a structured, modular approach when creating program requirements, design, and code. 1.2 Identify how a computer processes and stores data. Read the course description and objectives. Read the instructor’s biography and post your own. Read Appendix A. Read Ch. 2 of Prelude to Programming. Read Ch. 3 of Prelude to Programming.…

    • 2333 Words
    • 10 Pages
    Good Essays
  • Powerful Essays

    The following information will introduce general knowledge in basic programming concepts. It shall discuss basic types of computer programming languages as-well-as program development. There are three basic types of computer programming languages that will be discussed in a simple and easy to understand manner. We shall also describe the program development cycle and discuss why it is important to use a structured and organized process to create a computer programming language.…

    • 1318 Words
    • 6 Pages
    Powerful Essays
  • Satisfactory Essays

    Deadlock

    • 426 Words
    • 2 Pages

    Deadlock can occur when the permanent blocking of a set of processes compete for the same system resources. A set of processes is deadlocked when each process in the set is blocked awaiting an event that can only be triggered by another blocked process in the set. Deadlock is permanent because none of the events are ever triggered. Three conditions must take place for a deadlock to take place. The first one is Mutual exclusion, which is a single process that uses one resource at a time. No process may access a resource unit that is being utilized by another process. Hold and wait is the second condition, it can be described as one process that holds assigned resources while waiting for another assignment. Finally, No preemption occurs when no resource can be forced or removed from a process holding it. These three conditions are necessary for a deadlock to exist. However, a fourth condition is required for an actual deadlock to take place. Circular wait occurs when a closed chain of processes exists, and each process holds one or more resources needed by the next process in the chain. This condition is an immediate result of the first three. Below is a self-explanatory illustration of Deadlock.…

    • 426 Words
    • 2 Pages
    Satisfactory Essays

Related Topics