Skip to content

An implementation of the producer-consumer problem which is visually aided with dashboard to view the current commodities being produced and consumed, the program is implemented using shared memory, semaphores and mutexes in C.

Notifications You must be signed in to change notification settings

yousefkotp/Producer-Consumer-Implementation

Repository files navigation

Producer-Consumer-Implementation

An implementation of the producer-consumer problem which is visually aided with dashboard to view the current commodities being produced and consumed, the program is implemented using shared memory, semaphores and mutexes in C. The program creates a buffer of variable size, one process for consumer, infinite number of processes for producers. The producer process produces a random number indicating the price of a certain commoditity based on gaussian distribution and puts it in the buffer. The consumer program consumes what's in the buffer. The producer and consumer processes are synchronized using semaphores and mutexes.

Table of Content

Producer

  • All producers are processes running the same codebase. Producers are to be run concurrently, either in separate terminals, or in the background. While running a producer, you will specify the following command line arguments:
    • Commodity name.
    • Commodity Price Mean(μ).
    • Commodity Price Standard Deviation(σ).
    • The interval between two consecutive items produced by this producer in milliseconds.
    • Bounded buffer size.
  • For simplicity, the commodities are limited to GOLD, SILVER, CRUDEOIL, NATURALGAS, ALUMINIUM, COPPER, NICKEL, LEAD, ZINC, MENTHAOIL, and COTTON.

Consumer

  • The consumer prints the current price of each commodity, along the average of the current and past 4 readings. An Up/Down arrow to show whether the current Price (AvgPrice) got increased or decreased from the prior one.
  • The consumer is to be run as a single process. While running the consumer, you will specify the following command line arguments:
    • Bounded buffer size.

Psuedo Code

void producer() {
    while(True) {
        produce()
        wait(E) // wait on empty semaphore
        wait(S) // wait on mutex semaphore
        append()
        signal(S) // signal mutex semaphore
        signal(F) // signal full semaphore
    }
}

void consumer() {
    while(True) {
        wait(F) // wait on full semaphore
        wait(S) // wait on mutex semaphore
        consume()
        signal(S) // signal mutex semaphore
        signal(E) // signal empty semaphore
    }
}
  • You can find the detailed implementation for the producer and consumer processes in the following files:

Requirements

  • GCC

  • GNU Make

  • You can install the requirements using the following command:

sudo apt-get install build-essential

Compilation

  • You can compile the code using the following command:
make

Sample Input

Consumer Process

  • Only one consumer process is allowed to run at a time.
  • The consumer process takes the following arguments in the following order:
    • Bounded buffer size.
  • A sample run for a consumer process with buffer size = 10 would be like this:
./consumer 10

Producer Process

  • The producer process takes the following arguments in the following order:

    • Commodity name.
    • Commodity Price Mean(μ).
    • Commodity Price Standard Deviation(σ).
    • The interval between two consecutive items produced by this producer in milliseconds.
    • Bounded buffer size.
  • A sample run for producer process would be like this:

./producer GOLD 100 10 200 10
  • Infinite number of producer processes can be created.
  • Commodity name is case sensitive.
  • Note: You can make the producer process runs in the background by adding & at the end of the command.

Sample Output

sample-output

Remarks

  • Consumer process should be run first before starting any producer process.
  • The program can be run on any Linux distribution.
  • The program is tested on Ubuntu 22.04.1 LTS.

Author

About

An implementation of the producer-consumer problem which is visually aided with dashboard to view the current commodities being produced and consumed, the program is implemented using shared memory, semaphores and mutexes in C.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published