liveBook · Manning

Banker's algorithm

As explained in Learn Concurrent Programming with Go

The banker's algorithm is a deadlock-avoidance algorithm that grants resource requests only when the resulting state can still complete all executions within their declared maximum resource demands.

Banker's algorithm avoids deadlock by granting a resource request only when the resulting allocation remains safe: some schedule can still let every execution finish, even at its declared maximum demand. section 11.2.2

Apply the safety check when each execution's resource requirements are known:

  • Record each execution's maximum demand, current holdings, and the number of available resources. section 11.2.2
  • Grant a request only if the allocation leaves a safe state; otherwise suspend the execution until resources become safe to grant. section 11.2.2

Database connection pool

Treat fixed database sessions as limited resources. Granting execution a another session can produce an unsafe state if no execution can then receive enough resources to finish. A practical Go arbitrator instead waits until all requested accounts are available, performs a transfer, releases them, and broadcasts to waiting goroutines. section 11.2.2

How Learn Concurrent Programming with Go covers it

How deep it goes
Defines safe and unsafe states and required inputs, then defers detailed safety calculations for a practical arbitrator.
Examples
Uses railway crossings, database connection pools, and coordinated bank-account transfers.
What it assumes
Assumes basic executions and limited resources while teaching maximum demands, current holdings, availability, and safety.
What you can do after
Apply it via a resource arbitrator that waits, allocates together, releases resources, and resumes suspended goroutines.
How it connects
Connects deadlock avoidance with resource allocation, condition variables, mutexes, and suspended executions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.