1N, Tp, Tc = 2, 3, 3
2
3def Tproduce(nm):
4 while not (heap.empty > 0): # P(empty)
5 sys_sched()
6 heap.empty -= 1
7 sys_sched()
8
9 sys_write('(')
10 sys_sched()
11
12 heap.fill += 1 # V(fill)
13
14def Tconsume(nm):
15 while not (heap.fill > 0): # P(fill)
16 sys_sched()
17 heap.fill -= 1
18 sys_sched()
19
20 sys_write(')')
21 sys_sched()
22
23 heap.empty += 1 # V(empty)
24
25def main():
26 heap.fill = 0
27 heap.empty = N
28
29 for i in range(Tp):
30 sys_spawn(Tproduce, f'Tp{i+1}')
31 for i in range(Tc):
32 sys_spawn(Tconsume, f'Tc{i+1}')
33
34# Outputs:
35# (()())
36# (())()
37# ()(())
38# ()()()