Skip to content

A solution to the producer-consumer problem using System V IPC in C. The project implements shared memory and semaphores to synchronize communication between the producer and consumer processes. The aim is to demonstrate the use of System V IPC for interprocess communication and to provide a clear and efficient solution.

License

Notifications You must be signed in to change notification settings

XMaroRadoX/Producer-Consumer-Problem-Using-System-V-IPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Producer Consumer Problem Using System V IPC 🤝

This program utilizes the power of System V IPC to solve the classic problem of producer-consumer, where multiple producers generate data and multiple consumers consume them.The challenge is to synchronize these two entities and avoid any race conditions.

Explaination 📖

What is the producer consumer problem?

The producer-consumer problem is a classic concurrency problem in computer science, in which multiple producers generate data items and multiple consumers process them. The problem is to ensure that the producers do not produce too quickly for the consumers to keep up, and that the consumers do not consume too quickly for the producers to catch up. This car be achieved using synchronization mechanisms such as semaphores or message queues.

How to solve this problem? 💻

Solution using system V IPC (Inter Process Communication)

System V IPC (Inter-Process Communication) is a set of inter-process communication mechanisms provided by the System V operating system family, which includes UNIX and Linux.

It includes three main components:

  • message queues, which allow processes to send and receive messages
  • semaphores, which provide a mechanism for synchronizing access to shared resources
  • shared memory, which allows multiple processes to share a region of memory.

These mechanisms are implemented in the kernel, and are accessible to user-space processes through system calls. They provide a way for processes to coordinate and synchronize their activities, and are commonly used in multi-process and multi-threaded applications.

Code Need to Know 📊

Producer

Each producer is supposed to continuously declare the price of one commodity. For simplicity, we assume that each commodity price follows a normal distribution with parameters (𝜇, 𝜎^2).

While running a producer, you will specify the following command line arguments:

  • Commodity name (e.g., GOLD – Assume no more than 10 characters.)
  • Commodity Price Mean; 𝜇 – a double value.
  • Commodity Price Standard Deviation; 𝜎 – a double value.
  • Length of the sleep interval in milliseconds; T – an integer.
  • Bounded-Buffer Size (number of entries); N – an integer.
gnome-terminal -- ./producer ZINC 12 0.5 3200 40

in run.sh you will find already made demo with 10 diffrent producers

Consumer

The consumer is to print 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.

While running the consumer, you will specify the following command line argument:

  • Bounded-Buffer Size (number of entries); N – an integer.
gnome-terminal -- ./consumer 40 

Table of contents 🏷️

File Name Description
producer.cpp Contians the implementation of producer code using OOP principles
consumer.cpp Contains the implementation of consumer code using OOP principles
Makefile Used to run the program
run.sh contains run commands used by Makefile

Pre-requisites 🪛

  • Linux : Ubuntu or any debian based distro
  • gnome-terminal
  • C Compiler
  • make

Install pre-requisites 🧰

Have to be on debian-based distro that have apt as it's package manager to run these commands

sudo apt update && sudo apt install gcc make gnome-terminal

Run 🟢

  • Navigate to directory
  • Run Make

make

Licensing 📝

This code is licensed under the MIT License.

Authors 🖊️

  • Marwan Radwan

Contribution 🥂

Images are provided by Interviewbit

Feel free to contribute just make a pull request and do what you wish. 😼

License

About

A solution to the producer-consumer problem using System V IPC in C. The project implements shared memory and semaphores to synchronize communication between the producer and consumer processes. The aim is to demonstrate the use of System V IPC for interprocess communication and to provide a clear and efficient solution.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published