Protect access to shared (i.e., global) data variables used in the processOrders and takeOrders threads. As part of this: Create a global pthread mutex variable named data_mutex and initialize it to default attributes using the macro in the pthread library.  Insert mutex lock/unlock statements before/after regions of code using shared data in the two pthread functions. Note: the beginning and end of all critical regions are marked. Identify and protect critical region(s) that are relevant. Protect access to the console in the processOrders and takeOrders threads (remember that the console that cin & cout access is a shared resource). As part of this: Create a global pthread mutex variable named console_mutex and initialize it to default attributes using the macro provided by the pthread library.  Insert mutex lock/unlock statements before/after regions of code where the console is accessed in the two pthread functions. Note: the beginning and end of all critical regions are marked. Identify and protect critical region(s). Use condition variables to synchronize operations between the processOrders and takeOrders threads. As part of this: no new orders are put into the buffer unless there is at least one space in the buffer, i.e., no new orders are put into the buffer if the buffer is full; orders are not retrieved from the buffer unless there is at least one new order, i.e., orders are not retrieved if the buffer is empty. Create necessary global pthread condition variables and initialize all of them to default attributes using the macro provided by the pthread library. Insert pthread condition wait statements in processOrders and takeOrders threads such that: Insert corresponding condition signal statements in processOrders and takeOrders threads so that the above waits will end when the conditions being waited for are satisfied. In the main function, after both threads are created and before printing the goodbye message, insert code to ensure that the main thread waits for both pthreads to complete. Build/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:  Print statements must not be interleaved (i.e., a cout statement in a given thread must not be interrupted by a cout statement in a different thread). Only orders that have been placed must get processed. All orders that are placed must get processed, in the order in which they were placed. Multiple orders may be placed before the first one gets processed. The message “Phew! Done with orders for today!” must always be printed and printed last. ----- CODE: #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 < MAX; ++i) { // Beginning of critical region 1 // Get user input cout << "Enter a menu item number between 1 and 50: "; cin >> item; // Print new order's details cout << "Got new order! Order number is " << order_num << " and item number: " << item << std::endl; // End of critical region 1 // Beginning of critical region 2 // 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 < MAX; ++i) { // Beginning of critical region 3 // 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 << "Processing order number " << o_num << " with item number: " << item << std::endl; // 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 << "Phew! Done with orders for today!" << endl; pthread_exit(NULL); } Sample Output: IN IMAGES

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
  1. Protect access to shared (i.e., global) data variables used in the processOrders and takeOrders threads. As part of this:
    1. Create a global pthread mutex variable named data_mutex and initialize it to default attributes using the macro in the pthread library. 
    2. Insert mutex lock/unlock statements before/after regions of code using shared data in the two pthread functions. Note: the beginning and end of all critical regions are marked. Identify and protect critical region(s) that are relevant.
  2. Protect access to the console in the processOrders and takeOrders threads (remember that the console that cin & cout access is a shared resource). As part of this:
    1. Create a global pthread mutex variable named console_mutex and initialize it to default attributes using the macro provided by the pthread library. 
    2. Insert mutex lock/unlock statements before/after regions of code where the console is accessed in the two pthread functions. Note: the beginning and end of all critical regions are marked. Identify and protect critical region(s).
  3. Use condition variables to synchronize operations between the processOrders and takeOrders threads. As part of this:
    1. no new orders are put into the buffer unless there is at least one space in the buffer, i.e., no new orders are put into the buffer if the buffer is full;
    2. orders are not retrieved from the buffer unless there is at least one new order, i.e., orders are not retrieved if the buffer is empty.
    1. Create necessary global pthread condition variables and initialize all of them to default attributes using the macro provided by the pthread library.
    2. Insert pthread condition wait statements in processOrders and takeOrders threads such that:
    3. Insert corresponding condition signal statements in processOrders and takeOrders threads so that the above waits will end when the conditions being waited for are satisfied.
  4. In the main function, after both threads are created and before printing the goodbye message, insert code to ensure that the main thread waits for both pthreads to complete.
  5. Build/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: 
    1. Print statements must not be interleaved (i.e., a cout statement in a given thread must not be interrupted by a cout statement in a different thread).
    2. Only orders that have been placed must get processed.
    3. All orders that are placed must get processed, in the order in which they were placed.
    4. Multiple orders may be placed before the first one gets processed.
    5. The message “Phew! Done with orders for today!” must always be printed and printed last.

-----

CODE:

#include <pthread.h>
#include <iostream>
#include <unistd.h>
#include <cstdlib>

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 < MAX; ++i) {
// Beginning of critical region 1

// Get user input
cout << "Enter a menu item number between 1 and 50: ";
cin >> item;

// Print new order's details
cout << "Got new order! Order number is " << order_num << " and item number: " << item << std::endl;

// End of critical region 1


// Beginning of critical region 2

// 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 < MAX; ++i) {
// Beginning of critical region 3

// 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 << "Processing order number " << o_num << " with item number: " << item << std::endl;

// 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 << "Phew! Done with orders for today!" << endl;

pthread_exit(NULL);
}

Sample Output:

IN IMAGES

 

 

Enter a menu item number between 1 and 50: 2
Got new order! Order number is e and item number: 2
Processing order number e with item number: 2
Enter a menu iten number between 1 and 50: 10
Got new order! Order number is 1 and item number: 10
Enter a menu item number between 1 and 50: 43
Got new order! Order number is 2 and iten number: 43
Processing order number 1 with item number: 10
Enter a menu item number between 1 and 50: 19
Got new order! Order number is 3 and iten number: 19
Enter a menu item number between 1 and 50: 22
Got new order! Order number is 4 and itemn number: 22
Enter a menu iten number between 1 and 50: 40
Got new order! Order number is 5 and iten number: 40
Enter a menu iten number between 1 and 50: 28
Got new order! Order number is 6 and item number: 28 sequence they were placed
Enter a menu item number between 1 and 50: 15
Got new order! Order number is 7 and item number: 15
Processing order number 2 with item number: 43
Processing order number 3 with item number: 19
Enter a menu item number between 1 and 50: 23
Got new order! Order number is 8 and iten number: 23
Processing order number 4 with item number: 22
Enter a menu item number between 1 and 50: 9
Got new order! Order number is 9 and iten number: 9
Processing order number 5 with item number: 40
Processing order number 6 with item number: 28
Processing order number 7 with item number: 15
Processing order number 8 with item number: 23
Processing order number 9 with item number: 9
Phew! Done with orders for today!
Orders are processed in the
Transcribed Image Text:Enter a menu item number between 1 and 50: 2 Got new order! Order number is e and item number: 2 Processing order number e with item number: 2 Enter a menu iten number between 1 and 50: 10 Got new order! Order number is 1 and item number: 10 Enter a menu item number between 1 and 50: 43 Got new order! Order number is 2 and iten number: 43 Processing order number 1 with item number: 10 Enter a menu item number between 1 and 50: 19 Got new order! Order number is 3 and iten number: 19 Enter a menu item number between 1 and 50: 22 Got new order! Order number is 4 and itemn number: 22 Enter a menu iten number between 1 and 50: 40 Got new order! Order number is 5 and iten number: 40 Enter a menu iten number between 1 and 50: 28 Got new order! Order number is 6 and item number: 28 sequence they were placed Enter a menu item number between 1 and 50: 15 Got new order! Order number is 7 and item number: 15 Processing order number 2 with item number: 43 Processing order number 3 with item number: 19 Enter a menu item number between 1 and 50: 23 Got new order! Order number is 8 and iten number: 23 Processing order number 4 with item number: 22 Enter a menu item number between 1 and 50: 9 Got new order! Order number is 9 and iten number: 9 Processing order number 5 with item number: 40 Processing order number 6 with item number: 28 Processing order number 7 with item number: 15 Processing order number 8 with item number: 23 Processing order number 9 with item number: 9 Phew! Done with orders for today! Orders are processed in the
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY