resource dec_may_starve() op increment(amount : int), decrement(amount : int) var num_incs : int := 10, num_decs : int := 10 getarg(1, num_incs); getarg(2, num_decs) process inc(i := 1 to num_incs) var amount : int do true -> nap(int(random(i*1000))) amount := int(random(i*10)) write("age=", age(), "inc", i, "wants to inc by", amount) increment(amount) od end inc process dec(j := 1 to num_decs) var amount : int do true -> nap(int(random(j*1000))) amount := int(random(j*10)) write("age=", age(), "dec", j, "wants to dec by", amount) decrement(amount) od end dec var value : int := 110; getarg(3, value) var run_time : int := 60; getarg(4, run_time) write("simulation starting with", num_incs, "incs,", num_decs, "decs,", value, "value, and", run_time, "run_time") process server do true -> in increment(amount) -> value +:= amount write("age=", age(), "value +:=", amount, "is", value) [] decrement(amount) # never let value be decremented below 100 # and only service decrements when there are # no increments queued, reducing potential # starvation of large decrements st ?increment = 0 and value - amount >= 100 -> value -:= amount write("age=", age(), "value -:=", amount, "is", value) ni od end server nap(1000*run_time); write("must stop now"); stop end dec_may_starve /* ............... Example compile and run(s) % sr -o dec_may_starve dec_may_starve.sr % ./dec_may_starve 3 3 110 3 simulation starting with 3 incs, 3 decs, 110 value, and 3 run_time age= 409 inc 1 wants to inc by 3 age= 413 value +:= 3 is 113 age= 808 dec 1 wants to dec by 3 age= 810 value -:= 3 is 110 age= 868 dec 3 wants to dec by 13 age= 938 inc 1 wants to inc by 4 age= 942 value +:= 4 is 114 age= 943 value -:= 13 is 101 age= 1438 dec 1 wants to dec by 4 age= 1728 inc 1 wants to inc by 9 age= 1730 inc 2 wants to inc by 12 age= 1733 value +:= 9 is 110 age= 1734 value +:= 12 is 122 age= 1736 value -:= 4 is 118 age= 1968 dec 1 wants to dec by 7 age= 1971 value -:= 7 is 111 age= 2068 dec 2 wants to dec by 8 age= 2071 value -:= 8 is 103 age= 2208 inc 3 wants to inc by 28 age= 2211 value +:= 28 is 131 age= 2408 dec 3 wants to dec by 20 age= 2411 value -:= 20 is 111 age= 2458 inc 2 wants to inc by 3 age= 2461 value +:= 3 is 114 age= 2578 inc 1 wants to inc by 0 age= 2581 value +:= 0 is 114 age= 2658 inc 1 wants to inc by 1 age= 2660 value +:= 1 is 115 age= 2708 dec 2 wants to dec by 6 age= 2710 value -:= 6 is 109 age= 2948 dec 1 wants to dec by 4 age= 2950 value -:= 4 is 105 must stop now % ./dec_may_starve 2 4 110 5 simulation starting with 2 incs, 4 decs, 110 value, and 5 run_time age= 744 inc 1 wants to inc by 6 age= 747 value +:= 6 is 116 age= 873 dec 1 wants to dec by 4 age= 876 value -:= 4 is 112 age= 1073 dec 2 wants to dec by 5 age= 1076 value -:= 5 is 107 age= 1093 inc 1 wants to inc by 8 age= 1095 value +:= 8 is 115 age= 1223 inc 2 wants to inc by 19 age= 1226 value +:= 19 is 134 age= 1393 dec 1 wants to dec by 4 age= 1395 value -:= 4 is 130 age= 1563 dec 1 wants to dec by 9 age= 1566 value -:= 9 is 121 age= 1963 inc 1 wants to inc by 2 age= 1966 value +:= 2 is 123 age= 2003 dec 1 wants to dec by 6 age= 2005 value -:= 6 is 117 age= 2033 dec 1 wants to dec by 7 age= 2035 value -:= 7 is 110 age= 2183 dec 2 wants to dec by 1 age= 2186 value -:= 1 is 109 age= 2203 dec 1 wants to dec by 8 age= 2205 value -:= 8 is 101 age= 2583 dec 3 wants to dec by 11 age= 2623 dec 2 wants to dec by 13 age= 2753 inc 2 wants to inc by 5 age= 2757 value +:= 5 is 106 age= 2813 inc 1 wants to inc by 4 age= 2816 value +:= 4 is 110 age= 2863 dec 4 wants to dec by 4 age= 2865 value -:= 4 is 106 age= 2953 dec 1 wants to dec by 5 age= 2956 value -:= 5 is 101 age= 3114 dec 1 wants to dec by 6 age= 3513 inc 1 wants to inc by 3 age= 3516 value +:= 3 is 104 age= 3533 inc 1 wants to inc by 8 age= 3535 value +:= 8 is 112 age= 3537 value -:= 11 is 101 age= 3923 dec 3 wants to dec by 26 age= 3983 inc 1 wants to inc by 7 age= 3986 value +:= 7 is 108 age= 3987 value -:= 6 is 102 age= 4183 inc 1 wants to inc by 8 age= 4185 value +:= 8 is 110 age= 4613 dec 1 wants to dec by 0 age= 4616 value -:= 0 is 110 age= 4703 inc 2 wants to inc by 4 age= 4706 value +:= 4 is 114 age= 4708 value -:= 13 is 101 age= 4835 inc 1 wants to inc by 8 age= 4837 value +:= 8 is 109 age= 4853 inc 1 wants to inc by 1 age= 4855 dec 4 wants to dec by 22 age= 4857 value +:= 1 is 110 age= 4973 inc 1 wants to inc by 7 age= 4975 value +:= 7 is 117 age= 5043 dec 1 wants to dec by 8 age= 5046 value -:= 8 is 109 must stop now */