Skip to content

shooqMohammed/Producer_Consumer_Problem_Multi_Process_Synchronization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Producer-Consumer Problem Implementation

This project implements a solution to the classic Producer-Consumer Problem using Java. The project demonstrates the importance of synchronization in concurrent systems using Semaphores.

πŸ“– Overview

The Producer-Consumer problem is a well-known illustration of multi-process synchronization. It involves a finite-size buffer shared by two different process types:

  • Producers: Generate data (items) and add them to the buffer.
  • Consumers: Remove data from the buffer for processing.

The Problem

The project addresses two main challenges:

  1. Overflow: Ensuring producers refrain from adding data while the buffer is full.
  2. Underflow: Ensuring consumers do not delete data when the buffer is empty.
  3. Race Conditions: Using synchronization to prevent data corruption or deadlocks.

πŸ› οΈ Synchronization Mechanisms

We use Semaphores to handle synchronization and keep access organized:

  • mutex: Protects the buffer (Mutual Exclusion).
  • emptySlots: Tracks available space in the buffer.
  • fullSlots: Tracks available items in the buffer.

πŸ—οΈ Project Structure

The program is divided into four main classes:

  1. Buffer.java: The shared class that holds the queue and manages synchronization logic.
  2. Producer.java: A thread class that generates and adds items (1 to 10) to the buffer.
  3. Consumer.java: A thread class that removes and processes items from the buffer.
  4. ProducerConsumerDemo.java: The entry point class that initializes a buffer with a capacity of 5 and starts both threads.

πŸš€ Operations Logic

Producer

  • Waits if the buffer is full using emptySlots.acquire().
  • Locks the queue using mutex.acquire().
  • Adds the item and signals availability using fullSlots.release().

Consumer

  • Waits if the buffer is empty using fullSlots.acquire().
  • Locks the queue using mutex.acquire().
  • Removes the item and signals a free slot using emptySlots.release().

About

A Java-based implementation of the classic Producer-Consumer synchronization problem, utilizing semaphores to manage shared resources and prevent race conditions within an operating system environment.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors