Skip to content

imoroc/OS-Concurrency-Producer-Consumer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OS Concurrency: Multi-Threaded Producer-Consumer (Operating Systems Fundamentals)

C POSIX Threads Linux Academic

REFACTOR: This repository contains the evolved, production-ready version of my Operating Systems university assignment. The code has been completely refactored to enforce strict memory safety (zero memory leaks), prevent race conditions, and implement an $O(1)$ thread-safe linked list for concurrent data aggregation.


About the Project

This project demonstrates advanced system-level programming in C. It implements a highly synchronized Producer-Consumer architecture using the POSIX API. The system is designed to parse, filter, and process large datasets of binary strings using multiple concurrent threads while strictly avoiding deadlocks and race conditions.

The architecture is divided into two distinct execution phases:

  1. Multi-Processing (Data Filtering): The parent process uses fork() and execvp() to spawn an independent child process (data_filter.c) that sanitizes the raw input file, cross-platform carriage returns, and invalid characters.
  2. Multi-Threading (Data Processing): The parent process launches a complex synchronization network utilizing pthread and sem_t counting semaphores to distribute the workload.

Execution Results (Multithreaded Aggregation)

Execution Results


Memory Analysis (Valgrind Verified - 0 Leaks)

Valgrind Memory Analysis

Tech Stack

  • Language: C (C99 Standard)
  • APIs: POSIX Threads (pthreads), POSIX Semaphores (semaphore.h), UNIX Standard (unistd.h)
  • Build System: GNU Make

Key Technical Features

  • Bounded Circular Buffer: A dynamically allocated shared buffer protected by Mutexes and Empty/Filled counting semaphores to regulate the flow between the Producer and the Consumers.
  • Thread-Safe Queue (Linked List): A dynamic linked list implementation featuring $O(1)$ tail-insertion for Consumers and $O(1)$ head-extraction for the Aggregator thread, fully protected by its own distinct Mutex.
  • Poison Pill Shutdown: Implements a deterministic shutdown sequence where the Producer injects EOF "poison pills" into the buffer, allowing Consumers to exit gracefully without hanging.
  • Memory Safety (Valgrind Verified): Strict lifecycle management of dynamically allocated memory (malloc/free), ensuring zero memory leaks even when threads dynamically discard data based on parity logic.

System Architecture Flow

  1. data_filter Process: Reads input.txt, strips invalid characters/carriage returns, and outputs clean binary strings to filtered.txt.
  2. Producer Thread: Reads filtered.txt and pushes valid binary strings into the Circular Buffer.
  3. Consumer Threads (N): Extract strings from the buffer, convert them to decimal integers, and calculate cumulative sums based on Thread ID parity. The partial sums are pushed into a Shared Linked List.
  4. Aggregator Thread: Pops the partial sums from the Linked List, calculates the final aggregated total, and writes the output to results.txt.

How to Run Locally

This project requires a UNIX-like environment (Linux, macOS, or WSL on Windows) with gcc and make installed. A sample input.txt dataset is provided in the repository.

1. Clone the repository:

git clone [https://github.com/YOUR_USERNAME/OS-Concurrency-Producer-Consumer.git](https://github.com/YOUR_USERNAME/OS-Concurrency-Producer-Consumer.git)
cd OS-Concurrency-Producer-Consumer

2. Compile the project using Make:

make clean && make
(This will safely compile both the data_filter and os_concurrency native executables).

3. Run the architecture (e.g., 4 threads, buffer size 20):

./os_concurrency input.txt filtered.txt 4 20 results.txt

4. View the multi-threaded aggregation results:

cat results.txt

Original Authors

-Iván Moro Cienfuegos y Diego Martín García

About

A robust multi-threaded Producer-Consumer implementation in C using POSIX threads, semaphores, and inter-process communication.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors