2. Mice, rats and a cat

Chapter 1’s processes were polite: they queued up and took turns. Real systems are messier. Urgent jobs jump the line, machines break down in the middle of a task, and a supervisor pulls a worker off one job to do another. This chapter is about processes that compete and interfere.

The scenario is deliberately playful (it is the C library’s tutorial 2), but every mechanism in it shows up in serious models:

  • Five mice and two rats compete for a pile of 20 cheese cubes.

  • Each animal repeatedly picks a random amount and a random priority, and tries to grab that much cheese.

  • Mice acquire politely: if there isn’t enough, they wait.

  • Rats preempt: they snatch cheese from lower-priority holders, who lose everything they were holding.

  • A cat naps, wakes at random and chases a random rodent, interrupting whatever it was doing.

The complete script is tutorial/tut_2_1.py. Drawn with cimba.diagrams.process_graph(), the model is seven foraging processes, one per rodent, all acquiring, preempting and releasing one shared pile of cheese. The cat’s interrupts go through process handles, which the static diagram can’t follow.

        ---
config:
  layout: elk
---
flowchart LR
  subgraph g0["cheesegame: CheeseGame"]
    n0["cheese · Resource"]
    n1(["cat"])
  end
  subgraph g1["cheesegame.rodents[0]: Rodent"]
    n2(["forage"])
  end
  subgraph g2["cheesegame.rodents[1]: Rodent"]
    n3(["forage"])
  end
  subgraph g3["cheesegame.rodents[2]: Rodent"]
    n4(["forage"])
  end
  subgraph g4["cheesegame.rodents[3]: Rodent"]
    n5(["forage"])
  end
  subgraph g5["cheesegame.rodents[4]: Rodent"]
    n6(["forage"])
  end
  subgraph g6["cheesegame.rodents[5]: Rodent"]
    n7(["forage"])
  end
  subgraph g7["cheesegame.rodents[6]: Rodent"]
    n8(["forage"])
  end
  n0 -->|preempt| n2
  n0 -->|acquire| n2
  n2 -->|release| n0
  n0 -->|preempt| n3
  n0 -->|acquire| n3
  n3 -->|release| n0
  n0 -->|preempt| n4
  n0 -->|acquire| n4
  n4 -->|release| n0
  n0 -->|preempt| n5
  n0 -->|acquire| n5
  n5 -->|release| n0
  n0 -->|preempt| n6
  n0 -->|acquire| n6
  n6 -->|release| n0
  n0 -->|preempt| n7
  n0 -->|acquire| n7
  n7 -->|release| n0
  n0 -->|preempt| n8
  n0 -->|acquire| n8
  n8 -->|release| n0
    

A resource with capacity

The cheese pile is a Resource, a pool of identical units that processes acquire and release:

class CheeseGame(cb.Model):
    rodents: list[Rodent]
    cheese: cb.Resource = cb.Resource(capacity=CHEESE_AMOUNT)
    ...

Giving the field a value on the class sets its configuration. Here the pool has 20 units. (Each model instance still gets its own resource, and each trial its own native copy.)

The resource verbs, all called from compiled code, are:

Call

Meaning

acquire(n=1)

Take n units, waiting until they are free. Returns a signal.

preempt(n=1)

Take n units, grabbing them from lower-priority holders if necessary. Returns a signal.

release(n=1)

Give n units back.

held(process)

How many units a process holds right now.

available()

Free units right now.

mean_in_use()

Time-weighted mean of units in use during the measurement window.

A plain Resource(capacity=1) is a single server or a mutex. A larger capacity models a pool of workers, berths, tugboats or licenses.

A population of rodents

Each rodent is its own model, so it can have its own process and its own settings:

class Rodent(cb.Model):
    game: cb.Ref["CheeseGame"]
    is_rat: cb.Param[int] = 0
    process_pointer: int = 0

    def __init__(self, game, is_rat):
        self.game = game
        self.is_rat = is_rat

Three new kinds of field appear:

game: cb.Ref["CheeseGame"]

A reference to another model instance, here back to the game that owns the rodent. The string form lets you refer to a class defined later in the file. In compiled code, self.game.cheese follows the reference to the game’s resource.

is_rat: cb.Param[int] = 0

A parameter: a value fixed for the whole trial. Each rodent sets its own in __init__. Parameters can also be swept in an experiment.

process_pointer: int = 0

A plain field: a per-trial value that processes may change. It starts at 0 in every trial.

The game creates the rodents in its constructor and keeps them in a collection, a plain list annotated as list[Rodent]:

    def __init__(self):
        self.rodents = ([Rodent(self, 0) for _ in range(NUM_MICE)] +
                        [Rodent(self, 1) for _ in range(NUM_RATS)])

A list[Model] field holds children: they belong to the game and appear in the model tree as cheesegame.rodents[0], cheesegame.rodents[1] and so on. Every rodent’s forage process starts automatically. Seven instances, one class, compiled once.

Tip

If a list should point to models that live elsewhere in the tree rather than own them, annotate it as list[cb.Ref[Rodent]].

Priorities, preemption and signals

Here’s the heart of a rodent’s life:

    @cb.process
    def forage(self):
        me = cb.this_process()
        self.process_pointer = me.pointer
        held = 0
        while True:
            if self.is_rat:
                amount = cb.random.dice(3, 10)
                me.priority_set(cb.random.dice(-5, 15))
                signal = self.game.cheese.preempt(amount)
            else:
                amount = cb.random.dice(1, 5)
                me.priority_set(cb.random.dice(-10, 10))
                signal = self.game.cheese.acquire(amount)

cb.this_process() returns a Process handle for the running process. With it, the rodent sets its own priority before every grab. Priority decides who is served first among waiting processes, and who may be robbed by preempt: a rat can only take cheese from holders with lower priority. (A single request can also carry its own priority, acquire(priority=p), without changing the process’s; see the Resource rules.)

Notice that acquire and preempt return a value. Every blocking call in Cimba returns a signal that tells you why the process resumed:

Value

Meaning

Typical cause

0

success

The call completed normally.

-1

preempted

A higher-priority process took this process’s resource units.

-2

interrupted

Another process called interrupt() on this one.

-3

stopped

The process was stopped.

-4

cancelled

The wait was cancelled.

-5

timeout

A timeout expired.

> 0

user signal

Your own code: an interrupt with a custom code, or a process timer (chapter 3).

It’s good practice to name the ones you use as module constants, for example PREEMPTED = -1. The rodent inspects the signal and keeps score:

            new_held = int(self.game.cheese.held(me))
            if self.is_rat:
                if signal == 0:
                    self.game.rats_grabbed += amount
                elif signal == -1:
                    self.game.rats_preempted += 1
                else:
                    self.game.rats_interrupted += 1
                if held + amount > new_held:
                    self.game.rats_stolen += held + amount - new_held
            else:
                if signal == 0:
                    self.game.mice_grabbed += amount
                elif signal == -1:
                    self.game.mice_preempted += 1
                else:
                    self.game.mice_interrupted += 1
                if held + amount > new_held:
                    self.game.mice_stolen += held + amount - new_held
            held = new_held

            signal = cb.hold(cb.random.exponential(1.0))

cb.hold() returns a signal too. A rodent napping with its cheese can be preempted while holding, and it finds out when hold returns -1 instead of 0. A preempted process loses all its units of that resource, which is why the model compares held before and after.

Interrupting another process

The cat has to reach a specific rodent’s process. Each rodent stores its process handle’s pointer in a field as soon as it starts (self.process_pointer = me.pointer), and the cat rebuilds a handle from it:

    @cb.process
    def cat(self):
        while True:
            cb.hold(cb.random.exponential(5.0))
            while True:
                cb.hold(cb.random.exponential(1.0))
                index = cb.random.dice(0, NUM_MICE + NUM_RATS - 1)
                pointer = self.rodents[index].process_pointer
                if pointer:
                    signal = -2 if cb.random.bernoulli(0.5) else cb.random.dice(10, 100)
                    cb.Process(pointer).interrupt(signal, 0)
                    self.cat_chases += 1
                if not cb.random.bernoulli(0.5):
                    break

cb.Process(pointer).interrupt(signal, priority) wakes the target from whatever blocking call it is in. The call returns early with signal, here either -2 (the generic “interrupted”) or a random user code between 10 and 100. Unlike preemption, an interrupt doesn’t take resources away. The rodent keeps its cheese, but its wait is cut short.

Randomness that isn’t an input

This model has no Input fields. The amounts, priorities, nap lengths and coin flips all use cimba.random:

amount = cb.random.dice(3, 10)
me.priority_set(cb.random.dice(-5, 15))
signal = cb.hold(cb.random.exponential(1.0))

How do you choose between the two?

  • Use an input (cb.Input) for variability that represents the real world: arrivals, service times, demand, lead times, failures. Those are the things you will want to fit to data, replay from records, resample or sweep. Each input gets its own random stream, so its values don’t shift when other parts of the model draw more or fewer numbers.

  • Use cimba.random for model-internal randomness: routing coin flips, tie-breaking, synthetic behavior that no dataset will ever describe. These draws come from the trial’s default stream.

cimba.random functions only work inside compiled model code. On the host, before or after a run, use numpy.random.default_rng(seed).

Running the game

    @cb.on_end
    def game_stats(self):
        self.cheese_in_use = self.cheese.mean_in_use()

The counters are outputs that the rodents and the cat count into directly (self.game.mice_grabbed += amount): an integer output starts every trial at 0. The on_end hook computes the one result that needs the whole trial, the mean cheese in use.

def main() -> None:
    game = CheeseGame()
    start = time.perf_counter()
    results = cb.Experiment(
        game, replications=10, window=cb.Window(duration=DURATION),
        seed=20260612,
    ).run()
    elapsed = time.perf_counter() - start
$ uv run python tutorial/tut_2_1.py
cimba 3.0.0; 5 mice, 2 rats, 20 cheese cubes
10 trials in 1.69 s
mice 371807.8 278260.2 68044.3 14415.4
rats 359480.6 183410.5 16131.9 5545.6
cat chases: 28527.4

Each row reads grabbed, stolen, preempted, interrupted, averaged over ten replications of 100,000 time units. The rats, only two of them, grab nearly as much as five mice and are preempted far less often. The final assertion checks that every rodent’s own bookkeeping matches the resource’s record of what it holds: accounting_errors is zero in every trial.

What you learned

  • Resource(capacity=n) models a pool; acquire waits, preempt takes from lower-priority holders.

  • Ref[M] points to another model; list[M] holds child models.

  • A Param is fixed per trial, a plain field changes during it, and an Output is a field that is also a result: count into it directly.

  • Every blocking call returns a signal that says why the process woke up.

  • cb.this_process() and cb.Process(pointer) give you handles to set priorities and interrupt other processes.

  • Real-world variability belongs in inputs; model-internal randomness can use cimba.random.

In 3. An amusement park, customers stop being counters and become individuals that come and go during the trial.