CSCE 3600: Systems Programming

CSCE 3600: Systems Programming

Minor Assignment 4 – Threads, Mutexes, and Condition Variables

Due: 11:59 PM on Thursday, April 8, 2021

PROGRAM DESCRIPTION:

In this assignment, you will be given a functioning program, called minor4.c, that

simply reads user input digits (a string, separated into individual characters) and

compares each pair and displays the result of the comparison two digits at time using

the producer-consumer paradigm. The single producer thread reads user input

characters in pairs, makes sure they are single digits and places them into a pair of

buffers (A and B) while two consumer threads read the pairs of values and compares

them displaying the result of the comparison to the screen. To complicate matters, each

pair of digits (i.e., character) is read and compared by exactly one consumer thread. A

shared variable, called shared_count, keeps track of the number of items in the

shared buffer.

While this program does work (thanks to the mutex locks and unlocks already provided),

it is unfortunately very inefficient. To see just how inefficient this program is, compile the

original minor4.c program (using the pthread library) and execute the program. You

should type in some keys and see them echoed back on the screen in their correct

order. To see the inefficiency, though, run the top command from another shell (don’t

just run the minor4.c program in the background and then run top, but actually open

up another shell/window). Then check out the %CPU column in the top command and

you should see your original minor4.c program using up a significant percentage of

the CPU, which is not good.

Your goal for this assignment is to modify this program to use condition variables that

will drastically reduce its CPU percentage usage. Here are the details:

  • You will modify the original minor4.c file to add two condition variables, but not

change the “spirit” of the program other than necessary changes that are needed

for how conditional variables work, including handling of “spurious wakeup”

discussed in class.

  • You will add two global pthread condition variables – one to handle when the

shared buffer is full (and therefore, nothing else can be added to the buffer until a

key is removed from the buffer) and one to handle when the shared buffer is

empty (and therefore, nothing can be read/echoed back to the screen until a key

is added to the buffer).

  • In the main function, you will initialize and destroy both of the condition variables.

  • You will modify the code in the producer function to wait on and signal the

appropriate condition variable(s) based on what is happening with the shared

variables (i.e., the shared buffer and shared counter) Note that this will require some small changes in logic to accomplish, but you should not change the lines

that work with the prod_index variable.

  • You will modify the code in the consumer function to wait on and signal the

appropriate condition variable(s) based on what is happening with the shared

variables (i.e., the shared buffer and shared counter). Note that this will require

some small changes in logic to accomplish, but you should not change the lines

that work with the cons_index variable.

Be sure to run your solution along with the top command to verify that the program is

more efficient (i.e., does not use nearly as much percentage of the CPU). It is required

that you implement and utilize the condition variables effectively in a manner that

significantly reduces the CPU utilization of this program and not gain the reduction in

another way. Note that the grading rubric requires that the condition variables be

implemented “logically correct”, which includes accounting for “spurious wakeup”, not

just reducing the CPU utilization.

DETAILED ASSIGNMENT

20210408153503minor4

Powered by WordPress