Loading...

Messages

Proposals

Stuck in your homework and missing deadline? Get urgent help in $10/Page with 24 hours deadline

Get Urgent Writing Help In Your Essays, Assignments, Homeworks, Dissertation, Thesis Or Coursework & Achieve A+ Grades.

Privacy Guaranteed - 100% Plagiarism Free Writing - Free Turnitin Report - Professional And Experienced Writers - 24/7 Online Support

Producer consumer problem using threads

19/11/2021 Client: muhammad11 Deadline: 2 Day

Producer Consumer Problem

Process Synchronization: Producer-Consumer Problem The purpose of this programming project is to explore process synchronization. This will be accomplished by writing a program on the Producer / Consumer problem described below. Your simulation will be implemented using Pthreads. This assignment is a modification to the programming project “The Producer – Consumer Problem” found at the end of Chapter 7 of our textbook. 1. Your program must be written using C or C++ and you are required to use the Pthread with mutex and semaphore libraries. In chapter 3, we discussed how a "bounded buffer" could be used to enable producer and consumer processes to share memory. We described a technique using a circular buffer that can hold BUFFER_SIZE-1 items. By using a shared memory location count, the buffer can hold all BUFFER_SIZE items. This count is initialized to 0 and is incremented every time an item is placed into the buffer and decremented every time an item is removed from the buffer. The count data item can also be implemented as a counting semaphore. The producer can place items into the buffer only if the buffer has a free memory location to store the item. The producer cannot add items to a full buffer. The consumer can remove items from the buffer if the buffer is not empty. The consumer must wait to consume items if the buffer is empty. The "items" stored in this buffer will be integers. Your producer process will have to insert random numbers into the buffer. The consumer process will consume a number. Assignment Specifications The buffer used between producer and consumer processes will consist of a fixed-size array of type buffer_item. The queue of buffer_item objects will be manipulated using a circular array. The buffer will be manipulated with two functions, buffer_insert_item() and buffer_remove_item(), which are called by the producer and consumer threads, respectively. A skeleton outlining these functions can be found in buffer.h (provided with this assignment). The buffer_insert_item() and buffer_remove_item() functions will synchronize the producer and consumer using the algorithms. The buffer will also require an initialization function (not supplied in buffer.h) that initializes the mutual exclusion object "mutex" along with the "empty" and "full" semaphores. The producer thread will alternate between sleeping for a random period of time and generating and inserting (trying to) an integer into the buffer. Random numbers will be generated using the rand_r() function. See the text on page 290 for an overview of the producer algorithm. The consumer thread will alternate between sleeping for a random period of time (thread safe of course) and (trying to) removing a number out of the buffer. See the text on page 290 for an overview of the consumer algorithm. The main function will initialize the buffer and create the separate producer and consumer threads. Once it has created the producer and consumer threads, the main() function will sleep for duration of the simulation. Upon awakening, the main thread will signal other threads to quit by setting a simulation flag which is a global variable. The main thread will join with the other threads and then display the simulation statistics. The main() function will be passed two parameters on the command line: • The length of time the main thread is to sleep before terminating (simulation length in seconds) • The maximum length of time the producer and consumer threads will sleep prior to producing or consuming a buffer_item A skeleton for the main function appears as: #include int main( int argc, char *argv[] ){ Get command line arguments Initialize buffer Create producer thread(s) Create consumer thread(s) Sleep Join Threads Display Statistics Exit } Creating Pthreads using the Pthreads API is discussed in Chapter 4 and in Assignment-1. Please refer to those references for specific instructions regarding creation of the producer and consumer Pthreads. The following code sample illustrates how mutex locks available in the Pthread API can be used to protect a critical section: #include pthread_mutex_t mutex; /* create the mutex lock */ pthread_mutex_init( &mutex, NULL ); /* aquire the mutex lock */ pthread_mutex_lock( &mutex ); /*** CRITICAL SECTION ***/ /* release the mutex lock */ pthread_mutex_unlock( &mutex ); Pthreads uses the pthread_mutex_t data type for mutex locks. A mutex is created with the pthread_mutex_init() function, with the first parameter being a pointer to the mutex. By passing NULL as a second parameter, we initialize the mutex to its default attributes. The mutex is acquired and released with the pthread_mutex_lock() and pthread_mutex_unlock() functions. If the mutex lock is unavailable when pthread_mutex_lock() is invoked, the calling thread is blocked until the owner invokes pthread_mutex_unlock(). All mutex functions return a value of 0 with correct operation; if an error occurs, these functions return a nonzero error code. Pthreads provides two types of semaphores: named and unnamed. For this project, we will use unnamed semaphores. The code below illustrates how a semaphore is created: #include sem_t sem; /* create the semaphore and initialize it to 5 */ sem_init( &sem, 0, 5 ); The sem_init() function creates and initializes a semaphore. This function is passed three parameters: A pointer to the semaphore, a flag indicating the level of sharing, and the semaphore's initial value. In this example, by passing the flag 0, we are indicating that this semaphore can only be shared by threads belonging to the same process that created the semaphore. A nonzero value would allow other processes to access the semaphore as well. In this example, we initialize the semaphore to the value 5. In Chapter-6 (Section 6.6), we described the classical wait() and signal() semaphore operations. Pthread names the wait() and signal() operations sem_wait() and sem_post(), respectively. The code example below creates a binary semaphore mutex with an initial value 1 and illustrates it use in protecting a critical section: #include sem_t mutex; /* create the semaphore */ sem_init( &mutex, 0, 1 ); /* acquire the semaphore */ sem_wait( &mutex ); /*** CRITICAL SECTION ***/ /* release the semaphore */ sem_post( &mutex ); Program Output Your simulation should output when various conditions occur: buffer empty/full, location of producer/consumer, etc. Submission Guidelines and Requirements 1. Your program must be written using C or C++ and you are required to use the Pthread with mutex and semaphore libraries 2. You may use the C/C++ STL (Standard Template Library) in your solution. 3. You should use Netbeans to implement the assignment. You can download Netbeans with C/C++ features from the following link: https://netbeans.org/downloads/8.2/ 4. Create project in Netbeans for completing this assignment. 5. Add comments (about the function/variable/class) to your code as much as possible

Homework is Completed By:

Writer Writer Name Amount Client Comments & Rating
Instant Homework Helper

ONLINE

Instant Homework Helper

$36

She helped me in last minute in a very reasonable price. She is a lifesaver, I got A+ grade in my homework, I will surely hire her again for my next assignments, Thumbs Up!

Order & Get This Solution Within 3 Hours in $25/Page

Custom Original Solution And Get A+ Grades

  • 100% Plagiarism Free
  • Proper APA/MLA/Harvard Referencing
  • Delivery in 3 Hours After Placing Order
  • Free Turnitin Report
  • Unlimited Revisions
  • Privacy Guaranteed

Order & Get This Solution Within 6 Hours in $20/Page

Custom Original Solution And Get A+ Grades

  • 100% Plagiarism Free
  • Proper APA/MLA/Harvard Referencing
  • Delivery in 6 Hours After Placing Order
  • Free Turnitin Report
  • Unlimited Revisions
  • Privacy Guaranteed

Order & Get This Solution Within 12 Hours in $15/Page

Custom Original Solution And Get A+ Grades

  • 100% Plagiarism Free
  • Proper APA/MLA/Harvard Referencing
  • Delivery in 12 Hours After Placing Order
  • Free Turnitin Report
  • Unlimited Revisions
  • Privacy Guaranteed

6 writers have sent their proposals to do this homework:

Math Specialist
Calculation Master
Chartered Accountant
Top Academic Guru
Accounting & Finance Specialist
Professional Accountant
Writer Writer Name Offer Chat
Math Specialist

ONLINE

Math Specialist

I am a professional and experienced writer and I have written research reports, proposals, essays, thesis and dissertations on a variety of topics.

$17 Chat With Writer
Calculation Master

ONLINE

Calculation Master

I am an academic and research writer with having an MBA degree in business and finance. I have written many business reports on several topics and am well aware of all academic referencing styles.

$38 Chat With Writer
Chartered Accountant

ONLINE

Chartered Accountant

I will provide you with the well organized and well research papers from different primary and secondary sources will write the content that will support your points.

$27 Chat With Writer
Top Academic Guru

ONLINE

Top Academic Guru

I am an elite class writer with more than 6 years of experience as an academic writer. I will provide you the 100 percent original and plagiarism-free content.

$33 Chat With Writer
Accounting & Finance Specialist

ONLINE

Accounting & Finance Specialist

After reading your project details, I feel myself as the best option for you to fulfill this project with 100 percent perfection.

$27 Chat With Writer
Professional Accountant

ONLINE

Professional Accountant

I am an elite class writer with more than 6 years of experience as an academic writer. I will provide you the 100 percent original and plagiarism-free content.

$38 Chat With Writer

Let our expert academic writers to help you in achieving a+ grades in your homework, assignment, quiz or exam.

Similar Homework Questions

Texas bon license renewal - Machine language - Solivia 2.5 solar inverter - Hamad medical corporation residency salary - BW Texas Gov't Unit 2 Topic 4 - Wild country venturi 4 - One 3 Part Lecture Question DUE TODAY 10/19/20 IN 4HRS 10PM EST!!! 300 - 400 Words!! - An prc 117g technical manual - Ausaid scholarship application form - Acs code of ethics pdf - Difference between academic text and non academic text - Discussion board: Financial and Managerial Accounting - Two times the least of three consecutive odd integers - Sociology - Research paper - DiscussionB 8 - Presented below is an aging schedule for bosworth company - Flavour enhancer 621 halal or haram - What is it about the witches prophecies that frustrate macbeth - 6is of e marketing - What is the fattest state in america supersize me - Siemens rwb2e wiring diagram - Anderson greenwood 81 series relief valve - HRM 652 EVALUATING RESULTS AND BENEFITS - What is relation between molarity and molality - 1984 short answer questions - Eth 321 week 5 securities and international regulatory agencies - Denise clark bradford lipstick alley - 20 examples of proper noun - How to make ionic equations - How to calculate heat of neutralization - Overall equipment effectiveness ppt - P6#2 - Spelling city com student login - My plate 1800 calorie meal plan - Lady macbeth sleepwalking scene video - Safety root cause analysis template - Analysis of antacid tablets lab report - Lemon grove incident movie - Speech 1311 Chapter 14 - Persuasive Speech Evaluation - Creac - Year 10 chemistry notes - Managing Group Policy within the Microsoft Windows Environment - Calcium carbonate and hydrochloric acid - Tax and Zakat Accounting - Stuffed mole project ideas - Ulster hospital phone number - Apollo robbins the art of misdirection pdf - Actron air reset controller - Beverly hills md resurface restore youth revealing system - Metric hi lo screws - Junos end of support - Computer gateway device crossword clue - DQ Week 3 - Strategic Planning Implementation and Evaluation - Research methods in political science worksheet answers - Founding brothers the collaborators - Shirlaw v southern foundries - Develop a broad vision, an architecture, and a detailed plan of action that follows a life cycle concept for Developing IT Compliance Program - Development of the atom - Christ church jebel ali wedding - Adxloader log sync error - Best interests case practice model - CJT 202 DISCUSSION - The iron law of responsibility says that - Crime and Criminology - 197 lacey street whyalla - Literature Review : Divorce - Zinc and hydrochloric acid lab - Mcconnell brue flynn economics answers - Viva la causa viewing guide answers - Family therapy gained its initial legitimacy during the 1950's by - Unit 1 Discussion Public Administration - Compare and contrast mission and vision statements - How do the results in data tables 1 and 2 support the role of a buffer? - Quotes that show othello is respected - 6276 kj to calories - Obama nobel peace prize speech analysis - Compare and contrast a business case and a business plan - Kastle meyer test advantages and disadvantages - Managerial Finance - Mis en scene elements - 22.1 data gathering techniques homework answers - Holistic self care - Descriptive writing of a room - How to find standard deviation on statcrunch - Write about one of your self defeating behavior patterns - Danbury medical group hours - DQ5 - Describe the plain view doctrine, and why it has such a significant impact on digital forensics? What are three approaches to determining whether the doctrine applies to a specific case. - Lewisham council housing benefit - Code review practice comes under which domain of bsimm - Sait halim pasha mansion - Isometric drawing top front side - Www softwarepublications com au ohs download - Which of the following typifies decision making in bands - Axial strain and lateral strain - Animal habitat poster project - Plan view piping drawings - 4 Questions in Differential Equations