> For the complete documentation index, see [llms.txt](https://codexpress.gitbook.io/operating-system-showdown/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://codexpress.gitbook.io/operating-system-showdown/concurrency-control/classical-problems-of-concurrency-control/producer-consumer-problem.md).

# Producer Consumer Problem

## Problem Statement

The producer-consumer problem is a classic synchronization problem in concurrent programming. It involves two types of processes or threads: **producers and consumers**. Producers generate data items, and consumers consume or process these data items. The challenge is to coordinate the activities of producers and consumers in a way that ensures data integrity and avoids issues such as race conditions, data corruption, and deadlock.

**Problem Statement**:

1. There is a **shared buffer or queue** that can hold a **limited number of items**.
2. Producers **generate items and add** them to the buffer.
3. Consumers **remove items from the buffer** and process them.
4. Producers and consumers must **access the buffer in a mutually exclusive manner to avoid race conditions.**
5. Producers **must wait if the buffer is full**, and consumers **must wait if the buffer is empty.**

## Solution

1. **mutex :**  binary semaphore used in to acquire lock on buffer.
2. **empty :** a counting semaphore to track empty slots { **initially n** }.
3. **full :** counting semaphore tracks the slots filled { **initially 0** }.

**Producer :**&#x20;

```cpp
do {
    
    wait( empty ); // wait until empty > 0 then , empty->val--
    wait( mutex ); // wait until we don't lock on buffer
    
    // Start critical section
    producer()
    // End Critical Section
    
    signal( mutex ); // release lock on the buffer and notify all
    signal( full ); // increment the value of full , full->val++
    
} while ( True );
```

**Consumer :**&#x20;

```cpp
do {
    
    wait( full ); // wait until full > 0 then , full->val--
    wait( mutex ); // wait until we don't get lock on buffer
    
    // Start critical section
    consumer()
    // End Critical Section
    
    signal( mutex ); // release lock and notify all
    signal( empty ); // increment the empty value , empty->val++
    
} while ( True );
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://codexpress.gitbook.io/operating-system-showdown/concurrency-control/classical-problems-of-concurrency-control/producer-consumer-problem.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
