Skip to content
DeepForgeby svx
All problems
MediumData Structuresds-275

RCU Grace Period Mock

Problem

Simulate RCU grace periods.

operations contains "read_lock" and "read_unlock" (values[i] is the thread id) or "synchronize". A synchronize at a given moment must wait for every reader that is active then, so record how many readers are blocking it (0 when it completes immediately).

Return the list of blocking counts.

Hint

A grace period lasts only while readers that started before it remain active.

Test cases

case 1
in: [["read_lock","read_lock","synchronize","read_unlock","synchronize"],[1,2,0,1,0]]
expected: [2,1]
case 2
in: [["synchronize"],[0]]
expected: [0]
case 3
in: [["read_lock","read_unlock","synchronize"],[1,1,0]]
expected: [0]
case 4
in: [["read_lock","synchronize","read_unlock","synchronize"],[3,0,3,0]]
expected: [1,0]
Starter codePython
def rcu_grace_period(operations, values):
    # Your code here
    pass

4 test cases · run your code against them in the editor