{"id":25450118,"url":"https://github.com/rajnandinithopte/advanced-systems_multithreading-token-bucket-filter","last_synced_at":"2026-01-23T07:37:51.767Z","repository":{"id":275677896,"uuid":"926840410","full_name":"rajnandinithopte/Advanced-Systems_Multithreading-Token-Bucket-Filter","owner":"rajnandinithopte","description":"This project implements a multi-threaded token bucket filter using mutexes, condition variables, and FIFO queues to regulate packet transmission. It ensures efficient packet flow control, synchronization, and statistical tracking, supporting both deterministic and trace-driven execution modes.","archived":false,"fork":false,"pushed_at":"2025-02-04T00:18:09.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-16T09:08:35.551Z","etag":null,"topics":["conditionvariables","fcfs-scheduling","memory-management","multithreading","mutex-lock","networkcongestion","performance-analysis","pthreads","realtimesystem","sigint","synchronization-mechanisms","tokenbucket"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rajnandinithopte.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-04T00:13:57.000Z","updated_at":"2025-02-04T00:39:39.000Z","dependencies_parsed_at":"2025-02-04T01:22:49.280Z","dependency_job_id":"c5fec4e7-f577-4914-bb34-f18864053431","html_url":"https://github.com/rajnandinithopte/Advanced-Systems_Multithreading-Token-Bucket-Filter","commit_stats":null,"previous_names":["rajnandinithopte/advanced-systems_multithreading-token-bucket-filter"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/rajnandinithopte/Advanced-Systems_Multithreading-Token-Bucket-Filter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FAdvanced-Systems_Multithreading-Token-Bucket-Filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FAdvanced-Systems_Multithreading-Token-Bucket-Filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FAdvanced-Systems_Multithreading-Token-Bucket-Filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FAdvanced-Systems_Multithreading-Token-Bucket-Filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rajnandinithopte","download_url":"https://codeload.github.com/rajnandinithopte/Advanced-Systems_Multithreading-Token-Bucket-Filter/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rajnandinithopte%2FAdvanced-Systems_Multithreading-Token-Bucket-Filter/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28683792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T05:48:07.525Z","status":"ssl_error","status_checked_at":"2026-01-23T05:48:07.129Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["conditionvariables","fcfs-scheduling","memory-management","multithreading","mutex-lock","networkcongestion","performance-analysis","pthreads","realtimesystem","sigint","synchronization-mechanisms","tokenbucket"],"created_at":"2025-02-17T21:22:13.432Z","updated_at":"2026-01-23T07:37:51.733Z","avatar_url":"https://github.com/rajnandinithopte.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🔷 Advanced Systems: Multithreading Token Bucket Filter\n\n## 🔶 Overview\nThis project implements a **multi-threaded traffic shaper** that simulates a **token bucket filter** for regulating packet transmission. The system consists of multiple **concurrent threads**, including a **packet arrival thread, token arrival thread, and server threads**, that work together to process and transmit packets in an efficient and synchronized manner. \n\nThe implementation focuses on:\n- **Multithreading using `pthread`** to manage packet arrivals, token generation, and processing.\n- **Synchronization using mutexes (`pthread_mutex_t`) and condition variables (`pthread_cond_t`)** to manage shared resources.\n- **A doubly-linked FIFO queue to manage packets awaiting service.**\n- **Statistical tracking for packet delays, queueing times, and system efficiency.**\n\n---\n\n## 🔷 Libraries Used\n- **stdio.h** – Standard input/output operations.\n- **stdlib.h** – Memory allocation and process control.\n- **string.h** – String handling and manipulation.\n- **pthread.h** – Multithreading support (thread creation, mutexes, condition variables).\n- **semaphore.h** – Synchronization primitives.\n- **unistd.h** – Sleep, process control, and system calls.\n- **errno.h** – Error handling.\n- **sys/time.h** – High-precision time tracking for event timing.\n\n---\n\n## 🔷 System Architecture\n\n### 🔶 Token Bucket Filter\nThe **token bucket filter** controls the rate at which packets are processed, preventing bursts of traffic from overwhelming the system.\n\n- The **bucket holds up to `B` tokens** at any time.\n- **Tokens arrive at a rate of `r` tokens per second**.\n- If the bucket is **full**, incoming tokens are **discarded**.\n- When a packet arrives, it **requests `P` tokens** from the bucket:\n  - If enough tokens are available, the packet is moved to **Queue 2 (Q2)** for processing.\n  - If not, the packet **waits in Queue 1 (Q1)** until enough tokens accumulate.\n\n### 🔶 Packet Processing Flow\n1. **Packet Arrival**:\n   - Packets arrive at a rate of **λ packets per second**.\n   - Each packet requires **P tokens** before it can proceed.\n   - Packets initially wait in **Queue 1 (Q1)**.\n\n2. **Token Bucket Check**:\n   - Tokens are deposited in the bucket at a fixed rate **r**.\n   - If a packet in **Q1** has enough tokens, it is moved to **Queue 2 (Q2)** for service.\n\n3. **Server Processing**:\n   - Two **server threads (`S1` and `S2`)** fetch packets from **Q2**.\n   - Packets are processed **in a First-Come-First-Serve (FCFS) manner**.\n   - Each packet has a **service time based on `μ` (mean service time)**.\n\n4. **Completion and Statistics**:\n   - Packet completion timestamps are recorded.\n   - The system tracks **queueing times, service times, and total response times**.\n\n---\n\n## 🔷 Multi-Threading Implementation\n\n### 🔶 Threads Used:\n- **Packet Arrival Thread** (`pthread`):\n  - Generates packets at the specified **arrival rate (λ)**.\n  - Places packets in **Queue 1 (Q1)** and waits for token availability.\n\n- **Token Bucket Thread** (`pthread`):\n  - Deposits tokens into the bucket at a rate of **r tokens per second**.\n  - Checks if any packet in **Q1** can proceed to **Q2**.\n\n- **Server Threads (`S1 \u0026 S2`)**:\n  - Dequeue packets from **Q2** and process them using **random service times**.\n  - Maintain per-server statistics (average processing time, utilization).\n\n- **Signal Handler Thread (`SIGINT`)**:\n  - Captures **Ctrl+C** for **graceful shutdown**.\n  - Ensures all resources are released before exiting.\n\n---\n\n## 🔷 Additional Details\n\n### 🔶 1. More Information on Thread Synchronization and Mutex Usage\n- The system uses **mutexes (`pthread_mutex_t`)** to ensure **thread-safe access** to shared resources like **Q1, Q2, and the token bucket**.\n- **Condition variables (`pthread_cond_t`)** are used for **signaling between threads**, particularly for waiting on packets and tokens.\n- The `pthread_cond_wait()` and `pthread_cond_broadcast()` calls ensure that **threads are woken up efficiently** when resources become available.\n\n### 🔶 2. More Explanation on FIFO Queue (Doubly-Linked List)\n- The **MyList doubly-linked list** is used as a FIFO queue for **Q1 and Q2**.\n- Queue operations include:\n  - `MyListAppend()` → Adds a new packet at the end of the queue.\n  - `MyListUnlink()` → Removes a processed packet from the queue.\n  - `MyListFirst()` and `MyListLast()` → Fetch the head and tail elements.\n- This ensures **O(1) complexity** for **enqueue and dequeue operations**.\n\n### 🔶 3. Packet and Token Arrival Handling\n- Packet arrival follows a **Poisson process** with an **inter-arrival time λ** (adjusted using `usleep()`).\n- Tokens arrive **periodically** at a rate **r**.\n- **Overflow Conditions**:\n  - If a **packet requires more tokens than available** in the bucket (`B`), it is **dropped**.\n  - If the **token bucket is full** when a new token arrives, the token is **discarded**.\n\n### 🔶 4. Trace-Driven vs. Deterministic Execution Mode\n- The program can either generate packets **deterministically** (based on provided `λ, μ, r` values) or read packet events from a **trace file** (`-t tsfile` mode).\n- The **trace file must be correctly formatted** with three values per line (**inter-arrival time, tokens required, and service time**).\n- **Validations performed**:\n  - **Line format correctness** (no missing or extra values).\n  - **Correct sequence of packets** (monotonic timestamps).\n  - **Leading/trailing whitespace issues**.\n\n### 🔶 5. Signal Handling and Graceful Shutdown\n- **SIGINT (Ctrl+C) handling** ensures that:\n  - No new packets or tokens are generated after receiving **SIGINT**.\n  - Remaining packets in **Q1 and Q2** are removed, and logs are updated.\n  - Threads are **properly canceled** to prevent memory leaks.\n\n### 🔶 6. More Details on Statistics Computation\n- The system tracks **detailed performance metrics**, including:\n  - **Average inter-arrival time** of packets.\n  - **Average service time** and **total utilization** of each server.\n  - **Time spent by packets in Q1, Q2, and the system overall**.\n  - **Packet and token drop probabilities**.\n  - **Variance and Standard Deviation Calculations**:\n    - The program computes **system time variance** and derives the **standard deviation** to analyze **performance variability**.\n    - This helps evaluate the **effectiveness of the token bucket filter** under different loads.\n\n### 🔶 7. Error Handling and Robustness\n- The program includes **extensive error handling**, such as:\n  - **Invalid command-line arguments** (missing values, negative values).\n  - **Incorrect trace file formats** (malformed lines, incorrect values).\n  - **Memory allocation failures** (gracefully handled to prevent crashes).\n  - Errors print **detailed messages** and **terminate execution safely** with **clear diagnostics**.\n\n---\n\n📌 **Note:**  \nThe code for this project **cannot be made publicly available** due to academic restrictions. However, it can be shared **privately upon request** for review or discussion.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frajnandinithopte%2Fadvanced-systems_multithreading-token-bucket-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frajnandinithopte%2Fadvanced-systems_multithreading-token-bucket-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frajnandinithopte%2Fadvanced-systems_multithreading-token-bucket-filter/lists"}