Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Preamble: Create a subdirectory underyour class directory named Threadsto doallwork onLab3assignment. Thecollecting parts of lab3should be in Threadsfolder and not under any anotherdirectory.The general problemtargets

Preamble: Create a subdirectory underyour class directory named Threadsto doallwork onLab3assignment. Thecollecting parts of lab3should be in Threadsfolder and not under any anotherdirectory.The general problemtargets using Pthreadprocesses to work collaborativelyand concurrentlyonashared resource. The programtargets to developand implementa Bonded-Bufferwhere severalProducersmake itemstoadd into the buffer, whileseveral Consumerstry to receive theseitemsfrom the buffer toconsume. Producers & Consumers is a classic operating system problem demonstrating shared resources with multiple complication. Lab 3 covers Part 1 of the problem having only a singleproducer thread and a single consumer process thread. These two threads share one single buffer with a limited size of 10, and both exit when they have walked through the buffer. This assignment hasmultipleparts:Part1: A1: Pseudocode;B1:Programming Code.Lab3focuses onasthe framework of the Producers & Consumers programto be launched inLab4 as a follow-up assignment,with thedetailsynchronization between multi-processes.

Write a C++ programcalled thread.cppand make an executablewiththreadand a log filecalledthread.logfor this part of your assignment.

There isa buffer with S=10(the size of buffer is 10). The producer makes 10 items and add them to the buffer in order of 1, 2, 3 to 10 and stops. The consumer goes to the buffer 10 times and gets one-item-at-a-timeto consume, but each time beforeaccessing the buffer,it waits certainamount oftime for the item to be produced.The buffer in this part, has a limited sizeof 10.When 10 itemsareproducedor consumed, the program terminates.

Consumerin this part (and for simplicity)can use abusy-loopfor 10000 iterations. Inside the loop, there isa simple tasklike multiplying two numbers saving in third one; e.g. c=a*b. This busy loop forces the consumer towait a bit![It Part 2 of the problem, we will replace itwith real waiting condition.]

Create two Pthreads,one as producerand the second one as consumer. The producer generates a random number between 1 and1000(simulating an item) and inserts it in the buffer. Theconsumer threadfetches these randomnumbers(one at a time)and considers them as products to consume.

At the endof the program,the parentthread(the main program)reportstheMin, Max, Avg of the items.

pthreads that will be useful: pthread_attr_t a;// if needed

pthread_t myID = pthread_self();// returns calling threads ID

pthread_atti_init(&a);

pthread_create(tidC[x], NULL, (void *) consumer, ...);

pthread_create(tidP[y], NULL, (void *) producer, ...);

pthread_join(ptid[z], NULL);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions