/* SR program to simulate the critical section problem usage (if stored in file CS.sr): sr CS.sr a.out */ global CS op CSenter(id: int) {call}, # must be called CSexit() # may be invoked by call or send body CS process arbitrator do true -> # wait for some process to try to enter (prefer smallest index) in CSenter(id) by id -> write("user", id, "in its CS at", age()) ni # wait for that process to exit receive CSexit() od end end resource main() import CS var numusers, rounds: int getarg(1, numusers); getarg(2, rounds) process user(i := 1 to numusers) fa j := 1 to rounds -> call CSenter(i) nap(int(random()*100)) # sleep for a random period send CSexit() nap(int(random()*1000)) # sleep for a longer random period af end end