PDC@UNL

Distributed Computing - C Demonstration

Code Demonstration - C version

We have written a producer-consumer simulation in C that is available in the project available at https://github.com/cbourke/PDCatUNL

Navigate to the producerConsumer folder. We have organized the code into different source files representing producers, consumers, etc.

  • producer.c is the producer code; the produce() function runs continuously in its own thread and generates a new request at random intervals (default: 2-5 seconds; you can change this in the simulation.c file; be sure to run the make simulate command if you do).
  • consumer.c is the consumer code; the consume() function runs continuously in its own thread and handles requests.
  • request.c is the actual request code. It simply makes a dummy request to a webserver that "simulates" work by making an artificial delay between 2 and 5 seconds (randomly)
  • queue.c contains all the code that coordinates the simulation by managing the request queue (using sempahores and a mutex if you'd like to read more).
  • simulation.c is the main code that creates and starts all the threads.

Instructions

  1. Compile the program using the following (note: we recommend that you compile and run on the CSE server).
    make simulate
          
    Run the program using the following.
    ./simulate 5 5
          
    This will launch the simulation using 5 producers and 5 consumers.
  2. Run the simulation by running the Simulation class with the default parameters and observe the output. The producers and consumer report on each action and the queue monitor periodically reports how many tasks are waiting in the queue. The simulation runs continuously; to stop it you'll need to kill the program manually (control-c on the command line).
  3. Run/rerun the simulation for a few minutes with the following parameters and observe and note the results.
  4. Number of Producers Number of Consumers Interval Min Interval Max
    5
    5
    2 6
    1
    5
    2 6
    5
    1 2 6
    5
    5
    1 2
    5
    5 5 10