Skip to content

(CS341) Operating Systems: Assignment 1 - The Producer-Consumer Problem

Notifications You must be signed in to change notification settings

youssef-attai-fcai-cu/os-assignment-1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 

Repository files navigation

The Producer-Consumer Problem

Description

In the Producer-Consumer problem, there is a shared resource that a Producer keeps populating with data items, and a Consumer that keeps reading data items from it, and possibly processing them in some way.

Each of the Producer and the Consumer are executing their code in their own separate threads, which means they can both access the shared resource at the same time, which can be a problem.

The solution is to simply lock the shared resource when it is in use, blocking any thread trying to access it at that time, and only unlock it when it is ready for use again.

Example

This example is outdated

In this example, we have:

  • a Producer that produces prime numbers and puts them in a Buffer.
  • a Consumer that takes those prime numbers from the Buffer, and writes them to an output text file.

So, the Buffer is the shared resource.

The Producer takes an integer N, and iterates over all integers from 0 to N in search for prime numbers.

When the Producer finds a prime number, it attempts to put it in the Buffer.

If the Buffer is empty, the prime number is put safely.

If the Buffer is not empty, the Producer thread is blocked until it is notified that the Buffer is not full anymore, and is ready to have more prime numbers put in it.

The Consumer on the other hand keeps taking prime numbers from the Buffer as long as the Producer is not done producing (which is handled using a simple flag in the Producer), and writing them to the output file.

If the Consumer tries to take a prime number from the Buffer and did not find any, the Consumer thread will be blocked until it is notified that the Buffer is not empty anymore, and is ready to have prime numbers taken from it.

Authors

Acknowledgements

About

(CS341) Operating Systems: Assignment 1 - The Producer-Consumer Problem

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages