The purpose of this assignment is to gain experience with concurrent programming and synchronization mechanisms. The scenario simulates a news broadcasting system, where different types of stories are produced, sorted, and displayed to the public.
The assignment involves four types of active actors: Producers, Dispatcher, Co-Editors, and Screen Manager. The Producers generate strings representing news stories and send them to the Dispatcher. The Dispatcher sorts the stories based on their type and sends them to the corresponding Co-Editors. The Co-Editors edit the stories and pass them to the Screen Manager. Finally, the Screen Manager displays the stories on the screen. Each of them works in a different thread, allowing the system to work simultaneously.
The system includes a bounded buffer, which is used as a queue between the Producers and the Dispatcher, as well as between the Co-Editors and the Screen Manager. This desgind needs to face the Producer–consumer problem. The Dispatcher's queues are unbounded buffers.
The system can be visualized as follows:
Three Producers -> Dispatcher -> Three Co-Editors -> Screen Manager
![2](https://private-user-images.githubusercontent.com/103560553/239731704-8d67b13c-42d2-401a-b556-c9db0c08ad60.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1OTM1NDMsIm5iZiI6MTczOTU5MzI0MywicGF0aCI6Ii8xMDM1NjA1NTMvMjM5NzMxNzA0LThkNjdiMTNjLTQyZDItNDAxYS1iNTU2LWM5ZGIwYzA4YWQ2MC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjE1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxNVQwNDIwNDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jOGMxNGVlZTBkZTBhZTJiZDNjNTU1ZDMxNzUxNWFlZTc1ODIwZjkwZjQzYzA4NGFiMjMzZDQ1Zjk0NDlmZmE2JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.HjpM82BJhlCJSE2KO0c_VX3ARF7Bk2-0-sKCE7TOEh0)
The Producers communicate with the Dispatcher through their private queues. The Dispatcher scans the Producers' queues using a round-robin algorithm and sorts the messages based on their type. The sorted messages are then inserted into the Dispatcher's queues. The Co-Editors receive the messages from the Dispatcher's queues, perform editing, and pass the edited messages to the Screen Manager through a shared queue. The Screen Manager displays the messages received from the Co-Editors on the screen.
The Producer queues and the Co-Editors' shared queue are implemented as a bounded buffer. The bounded buffer supports two main operations: Push
and Pop
. The Push
operation is used to insert an object into the buffer, while the Pop
operation removes and returns the first object from the buffer.
A thread-safe bounded and unbounded queues implemented using synchronization mechanisms such as mutexes and counting semaphores.
![2](https://private-user-images.githubusercontent.com/103560553/240520048-9a0a549b-a6bc-4221-b4e3-de300186d574.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1OTM1NDMsIm5iZiI6MTczOTU5MzI0MywicGF0aCI6Ii8xMDM1NjA1NTMvMjQwNTIwMDQ4LTlhMGE1NDliLWE2YmMtNDIyMS1iNGUzLWRlMzAwMTg2ZDU3NC5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjE1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxNVQwNDIwNDNaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0xYzg5ZTY4NjljYTFmMDhhZDlkNzY1ODI4NDFjZjFjYjYxZmJiNzU2MGIxOGZmMDg2NTg0MmY4NjlhMWZkNDI0JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.3mlKvbGJd--wmJ1LgdMlE5_4JRdJ1WhdS4HhA1XdN-k)
The system reads a configuration file to determine the number of Producers, the number of strings each Producer should create, and the size of the queues. The format of the configuration file is as follows:
PRODUCER 1 [number of products] queue size = [size]
PRODUCER 2 [number of products] queue size = [size]
...
PRODUCER n [number of products] queue size = [size]
Co-Editor queue size = [size]
For example:
I also added a test configuration file called conf.txt to this repository.
The implementation of the Multithreaded News System consists of several components that work together to process and display news stories. Each component performs specific tasks and operates in its own thread, allowing for concurrent execution. Here are the main components of the system:
Producers:
- Generate strings representing news stories.
- Send the generated stories to the Dispatcher for further processing.
- Operate in their own threads.
Dispatcher:
- Receives news stories from Producers.
- Sorts the stories based on their type.
- Forwards each story to the corresponding Co-Editor for editing.
- Operates in its own thread.
Co-Editors:
- Receive news stories from the Dispatcher.
- Edit the stories according to their assigned type.
- Pass the edited stories to the Screen Manager for display.
- Each Co-Editor operates in its own thread.
Screen Manager:
- Receives edited news stories from the Co-Editors.
- Displays the stories on the screen.
- Operates in its own thread.
The system utilizes multithreading to enable concurrent execution of these components. Each component is implemented as a separate module, with its own set of functions and data structures to fulfill its specific responsibilities.
The main.c file serves as the entry point of the program and coordinates the initialization and execution of the system. It performs tasks such as reading the configuration file, creating the necessary threads for the components, and waiting for all threads to finish execution.
The system directory contains folders for each component, with corresponding .c and .h files that provide the implementation and necessary functions for each component.
I have thoroughly tested the Multi-Threaded News System for memory leaks and errors using Valgrind, a powerful tool for detecting memory management issues. The extensive testing process involved running the system with different scenarios and input data to ensure its stability and reliability.
Overall, the implementation follows a modular and concurrent design, allowing the system to efficiently process news stories and display them on the screen.
To clone and run this program, you'll need Git installed on your computer. From your command line:
// Clone this repository:
git clone https://github.com/TalMizrahii/Multithreaded-News-System
// Go into the repository:
cd Multithreaded-News-System
// Compile using Makefile:
make
// An ex3.ou file will be created. You can run:
make run
// Or directly give the path to the configuration file.
./ex3.out conf.txt
// To clean the executabke file:
make clean