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.
- 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.
- 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.
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:
sudo apt-get install build-essential
- You can compile the code using the following command:
make
- 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
-
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.
- 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.