CSE Roadmap

Core tier · about 4 hours · 7 parts

Part 3 of 7 · 2 min read

Resources

In plain words: A deadlock needs something to wait for. Theory calls these things resources, and the word covers more than locks. A resource is a lock, a printer, a file, a slot in a table, a block of memory, a network connection, or an open record in a database. A thread asks for a resource, uses it, and gives it back. That three-step pattern (request, use, release) is the only way threads touch a resource in the standard model.

Two kinds of resource matter. A reusable resource is used by one thread at a time and is not used up: when the thread releases it, another thread can take it. Locks, printers and memory belong here. A consumable resource is created by one thread and destroyed by another. Messages, signals and keyboard input belong here. Most of this level is about reusable resources, because locks are the common cause. Page 6 returns to consumable ones.

Some resources come in several identical units. Ten tape drives are ten instances of one resource type: a thread that asks for "a tape drive" is happy with any of them. A mutex has exactly one instance. The count matters, because a deadlock among single-instance resources is easier to find than one among many (page 2).

A request can end in two ways. If an instance is free, the thread gets it at once. If none is free, the thread blocks. Blocked means the kernel has taken the thread off the processors and put it to sleep until the resource is released (Level 4, page 3, showed this for futexes). The sleeping thread does not use any processor time. This is why a deadlocked program shows 0% processor use and looks "idle" while it is stuck.

Kind Example Instances After use
Reusable A mutex 1 Released, same as before
Reusable A pool of 10 database connections 10 Returned to the pool
Consumable A message in a queue Created as needed Gone after it is read

Resource instance

A resource instance is one identical unit of a resource type: a thread that requests the type may be given any free instance, and a type with a single instance can serve only one thread at a time.

Example: a pool of 3 database connections is one resource type with 3 instances, while a mutex is a type with 1 instance.

Check yourself: 1. Is a mutex a reusable or a consumable resource? And a message in a pipe? 2. A system has 4 identical printers. How many resource types and how many instances is that? 3. Why does a deadlocked program use almost no processor time?