Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objective In this assignment, you will be writing a multithreaded program that shares data between threads. You will need to properly protect data that is

Objective

In this assignment, you will be writing a multithreaded program that shares data between threads. You will need to properly protect data that is shared between threads. You will be using mutex locks around critical sections to ensure you program works correctly and does not experience race conditions.

Details

The previous program could possibly use too many threads and we need to fix that problem. You will rewrite the program from Project 3 - Threads to only use three threads; the main thread, a producer, and a consumer. The three threads will accomplish the following tasks:

Main Thread 1. Create the producer and consumer threads 2. Send each number from the command line to the producer thread via a shared buffer. This buffer only needs to be shared between the main thread and the producer. 3. Wait for each thread to complete before terminating the program Producer Thread 1. Wait for numbers to be added to the buffer shared with the main thread 2. Factor each number 3. Save numbers to a buffer shared between the producer and consumer threads Consumer Thread 1. Wait for factors to be added to the buffer shared with the producer thread 2. Display the factors

This program will use two sets of shared data, in the producer-consumer style. Both sets of data need to be protected using pthread_mutex_t variables. The only thread producing any output to the console should be the consumer thread. All other threads should perform their actions silently. Your program should terminate after processing the last number.

To make testing and grading easier, no buffer should hold more than 10 items.

Output

Example 1: No arguments

$ ./p4 Usage:./p4

...

$ ./p3 12

12: 2 2 3

$ ./p3 7 12 25

7: 7

12: 2 2 3

25: 5 5

$ ./p3 {32..48}

32: 2 2 2 2 2

33: 3 11 34: 2 17

35: 5 7

36: 2 2 3 3

37: 37

38: 2 19

39: 3 13

40: 2 2 2 5

41: 41

42: 2 3 7

43: 43

44: 2 2 11

45: 3 3 5

46: 2 23

47: 47

48: 2 2 2 2 3

Notes

Producer-Consumer Example Here is an example of a producer-consumer threading model: https://github.com/jjolly/uvu-cs3060/blob/master/sync/prodcons_1/prodcons.c

Buffers The buffer between the main thread and the producer thread can be a simple integer array. The buffer between the producer thread and the consumer thread might be an array of integer pointers. Each element in the array of integer pointers will point to an array of integers that contains the original number and the factors of that number. You could also make the producer/consumer buffer an array of pointers to structures. The possibilities are endless.

Mutexes To declare and initialize a mutex named mutex1 you can use lines of code like this: pthread_mutex_t mutex1; pthread_mutex_init(&mutex1, NULL); In some cases you can use a static initializer like this: pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

To lock a mutex named mutex1 you can use this line of code: pthread_mutex_lock(&mutex1);

To unlock a mutex named mutex1 you can use this line of code: pthread_mutex_unlock(&mutex1);

Condition Variables You can read about how to use condition variables on pages 252-253 in our textbook (Silberschatz et al). You'll probably want two condition variables for each buffer: one to wait for something to be put into the buffer when the buffer is empty and another to wait for something to be removed when the buffer is full. Note: A student pointed out that the parameters to pthread_cond_wait are in the wrong order on page 253 of our textbook. &cond_var should be first and &mutex should be second.

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

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

What proactive strategies might you develop?

Answered: 1 week ago

Question

Types of cultural maps ?

Answered: 1 week ago

Question

Discuss the various types of leasing.

Answered: 1 week ago

Question

Define the term "Leasing"

Answered: 1 week ago

Question

What do you mean by Dividend ?

Answered: 1 week ago

Question

How will the members be held accountable?

Answered: 1 week ago

Question

a. Do team members trust each other?

Answered: 1 week ago

Question

a. How will the leader be selected?

Answered: 1 week ago