A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Counting Semaphores and Resource Pools

College Depth 105 in the knowledge graph ☐ I know this ☆ Set as goal
5topics build on this
412prerequisites beneath it
See this on the map →
Semaphores→→Producer-Consumer Problem: Classic Synchronization
semaphores resource-management synchronization

Core Idea

Counting semaphores have integer values (≥0) representing available resources. wait() decrements (blocks if 0); signal() increments and wakes a waiting thread. Counting semaphores express resource constraints naturally and manage pools of identical resources such as buffer slots or thread pools.

How It's Best Learned

Use counting semaphores to implement a bounded buffer, thread pool, or resource pool manager.

Explainer

You already understand binary semaphores and mutexes — they protect critical sections by allowing exactly one thread in at a time. But many real problems involve managing a pool of identical resources rather than enforcing mutual exclusion. Consider a database connection pool with 10 connections: up to 10 threads can use connections simultaneously, but the 11th must wait. A mutex is too restrictive (only one thread at a time), and using 10 separate mutexes would be awkward. A counting semaphore solves this naturally by generalizing the binary concept: instead of toggling between 0 and 1, it maintains an integer count representing the number of available resources.

A counting semaphore supports two atomic operations. wait() (also called P, down, or acquire) decrements the counter. If the counter is already 0, the calling thread blocks until another thread increments it. signal() (also called V, up, or release) increments the counter and, if any threads are blocked on wait(), wakes one of them. Initialize the semaphore to the number of available resources — 10 for a connection pool, N for a bounded buffer with N slots — and the semaphore automatically enforces the capacity limit. Each wait() claims one resource; each signal() returns one. The semaphore never goes negative (blocked threads are queued, not counted), so the count always reflects the true number of available resources.

The classic application is the bounded buffer (producer-consumer) problem. You use two counting semaphores: `empty` initialized to the buffer size (number of empty slots) and `full` initialized to 0 (number of filled slots). A producer calls `wait(empty)` to claim an empty slot, writes data, then calls `signal(full)` to announce a filled slot. A consumer calls `wait(full)` to claim a filled slot, reads data, then calls `signal(empty)` to announce a newly empty slot. If the buffer is full, producers block on `wait(empty)`; if it is empty, consumers block on `wait(full)`. The two semaphores work in tandem, each counting what the other consumes. You still need a mutex to protect the actual buffer data structure from concurrent access, but the counting semaphores handle the capacity logic — a clean separation of concerns that makes the synchronization pattern both correct and readable.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → Process Creation: fork() and exec() → Process Termination and Resource Cleanup → Process States and State Transitions → Process Model Formalization → Context Switching and CPU Dispatch → CPU Scheduling Fundamentals → Round-Robin (RR) Scheduling → First-Come-First-Served (FCFS) Scheduling → Scheduling Fairness and Starvation Prevention → Thread Scheduling and Coordination → Semaphores → Counting Semaphores and Resource Pools

Longest path: 106 steps · 412 total prerequisite topics

Prerequisites (1)

Leads To (1)