Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include #include using namespace std; #define MAX 10 #define N 4 // Data structure to represent a simplified Order // that has an

image text in transcribed

image text in transcribed

image text in transcribed

#include #include #include #include

using namespace std;

#define MAX 10 #define N 4

// Data structure to represent a simplified Order // that has an order number and an item number. struct Order { int order_num; int item_num; };

Order new_orders [N]; // array of elements of type Order to be used as a shared buffer int num_new_orders = 0; // count of number of new (i.e., unprocessed) orders int order_num = 0; // global variable used to generate unique order numbers

// TODO: Define and initialize necessary mutex and condition variables here

void* takeOrders(void* arg) { int item; int index = 0; for(int i = 0; i > item;

// Print new order's details cout

// Put new order into new orders buffer and update number of new orders new_orders[index].order_num = order_num; new_orders[index++].item_num = item; ++num_new_orders; // End of critical region 2

// Update order number so that next order gets a different number ++order_num; // If the end of the new orders buffer is reached, wrap back around if(index == N) index = 0; }

pthread_exit(NULL); }

void* processOrders(void* arg) { int item; int index = 0; int o_num;

for(int i = 0; i

// Retrieve new order details from buffer and update number of new orders o_num = new_orders[index].order_num; item = new_orders[index++].item_num; --num_new_orders;

// End of critical region 3

// Beginning of critical region 4 // Print retrieved order's details cout

// End of critical region 4

// Suspend self for 1 second sleep(1); // If the end of the new orders buffer is reached, wrap back around if(index == N) index = 0; }

pthread_exit(NULL); }

int main() { // Create threads to take and process orders pthread_t id1, id2; pthread_create(&id1, NULL, processOrders, NULL); pthread_create(&id2, NULL, takeOrders, NULL); // TODO: Add code to wait for both threads to finish // Print goodbye message cout

pthread_exit(NULL); }

Part 1: Producer- Consumer-execution (o points) 1. The producer_consumer.cpp program given to you simulates a highly simplified restaurant order taking and processing system using pthreads. Study this program, using the comments in the code to help you understand the functionality. 2. Build and execute this program multiple times. You will observe that a. Some random, dummy orders that were never placed may get b. Some orders entered by the user may not be processed at all. c. The message "Phew! Done with orders for today! may get printed even before orders are placed. All these problems arise because of lack of synchronization among the processOrders and takeOrders threads. Copy producer consumer.cpp intofie named producer_consumerwith synchronization.cpp and modify the copy s instructed below (refer to your prEp work videos/slides if you need to help 1. Protect acto shared (i.-. globel) dats variables used in the processOrders and takeOrders threads. As part of this: a. Create a global pthread mutex variable named data_mutex and initialize it to default sttributes using the macro provided by b. Insert mutex lock/unlc statements before/fter regions o beginning and end of all critical regions are marked. Identify and 2. Protect accss to the console in the processOrders and takeOrders the pthread library protect critical region(s) that are relevant to this task. threads (remember that the consoe that cin & cout access isashared esourc). As part of this: a. Create a global pthread mutex variable named console mutex and inlize it to defaut attributes using the macro provided by the pthread library b. Insert mutex lock/unlock statements befoe/fter regions o Note: the beginning and end of all critical regions are marked. Identify and protect critical region(s) tha rrelevant to this 3. Use condition variables to synchronize operations between the a. Create necessary global pthread condition variables and initialize processOrders .nd takeOrde threads. As part of this D. Insert pthread condition wait statements in processOrders and keOrders threads such that: i no new orders are put into the buffer unless there is at least ont space in te butter, i , n new orders are put into the buffer it tte buffer is full; ii. orders are not retrieved from the buffer unless there is at least one new order, i.e., orders re not retrieved it the buffer is empty nd bsreOrders thresds so that the bove waits will end when th. : ka.iiines Pa , fin. ,ias! ! , i.s1. 4. In the "sin function, after both threads are created nd before printing the goodbys messg, insert code to ensure thet the main S.Build and run your program. When prompted, enter 10 unique integers in the range of 1 to 50. I you have put in ell synchronizstion statemants correctly, your program must adhere to the following a. Print statements must not be interieavedie.ctatement c.All orders thet are placed must ge processed, in the order in Multiple orders may be placed before the firs one gets e. The message "Phew! Done with must always orders for today! 5. Build and run your program. When prompted, enter 10 unique integers in the range of 1 to 50. If you have put in all synchronization statements correctly, your program must adhere to the following a. Print statements must not be interleaved (i.e., a cout statement in a given thread must be not interrupted by a cout statement in a different thread) b. Only orders that have been placed must get processed c. All orders that are placed must get processed, in the order in which they were placed d. Multiple orders may be placed before the first one gets processed e. The message "Phew! Done with orders for today!" must always be printed and printed last. Sample Output: Below is a sample of what your output should look like after adding appropriate synchronization constructs Enter a nenu iten nunber betwecen 1 and 58: 2 new order! Order nunber is and iten number: 2 Processing order nunber e with iten nunberi 2 Enter a menu item nunber between 1 and 50: 10 new orderl Order nunber is 1 and iten nuber: 1 Enter a nenu iten nunber between 1 and 58: 43 new order! Order nunber is 2 and iten number: 43 Processing order number 1 with iten nunberi 1e Enter a nenu item nunber between 1 and 50: 19 new orderl Order nunber is 3 and iten nuber: 1 Enter a nenu iten nunber between 1 and 5a: 22 new order! Order number is 4 and item number: 22 Enter a nenu iten nunber between 1 and 581 40 Got new order! Order nunber is 5 and item number: 4e Enter a neeu iten nunber between 1 and 50: 28 Orders are processed in the t new order! Order munber is 6 and item number: 28 sequence they were placed Enter a neru iten nunber between 1 and 58: 15 Got new order! Order nunber is 7 and iten numberi 15 Processing order number 2 with iten number: 43 Processing order nunber 3 with iten nunber: 19 Enter menu item nuber between 1 and 58: 23 new order! Order nunber is 8 and iten nuber: 23 Processing order nunber 4 with iten nurberi 22 Enter a neru iten nunber between 1 and 50: 9 new orderi Order nunber is 9 and iten nunber: 9 Processing order nunber 5 with iten nunber: 40 Processing order number 6 with iten nunber: 28 Processing order number 7 with iten nunberi 15 Processing order nunber 8 with iten nurber: 23 Process ing order nunber 9 with iten nunber:9 6 1 solutions and grading(master) Must always be printed at the end

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

Step: 3

blur-text-image

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

Project Management A Systems Approach to Planning Scheduling and Controlling

Authors: Harold Kerzner

10th Edition

978-047027870, 978-0-470-5038, 470278706, 978-0470278703

Students also viewed these Databases questions

Question

why do consumers often fail to seek out higher yields on deposits ?

Answered: 1 week ago