Lab – Threads and Synchronization

Your task is to write a program that generates and solves math questions.

Create a structure to hold a math question:

enum oper {PLUS, MIN, MUL, DIV};

struct Question{
      double num1;
      double num2;
      oper   op;
};

create an array to hold up to 10 questions

Create 2 producers (threads):

  • Each thread sleep for n seconds
  • Generates 2 random numbers between lowval and highval
  • Generates random operator
  • Stores the question in the questions array

Create both threads with the same function (send n, lowval, highval to the thread function)

Create a solver thread

  • Sleeps for 5 seconds
  • Check if a new question exists
  • Solve it and print result

Use semaphorsand mutexes to access the shared data structures

Part 2 – Processes

Change the above to use processes instead of threads

Add command line arguments to declare the number of consumers and producers

for example to create 2 producers and 3 consumers run:

# ./mathapp -p 2 -c 3

Create a python program that reads files with math questions and send it to a producer process.

  • Use pipe to send the data from python to C (popen)
  • Use socket from C to python – send list of files to process
  • Use signal from C to python to process the next file