semaphore.csp 572 B

12345678910111213141516171819202122232425262728293031323334
  1. define Nproc 4
  2. P ==
  3. [
  4. i = 1 ->
  5. sem!init(1)
  6. []
  7. i /= 1 -> skip
  8. ];
  9. *[ true ->
  10. sem!P ();
  11. print ("ressource : " + ressource + "\n");
  12. ressource := ressource + i;
  13. sleep(10000);
  14. sem!V ()
  15. ]
  16. SEMAPHORE ==
  17. val : integer := 0;
  18. *[
  19. (i:1..Nproc) proc[i]?V() -> val := val+1 /*print("(" + val + ")" + "\n")*/
  20. []
  21. (i:1..Nproc) val >0; proc[i]?P () -> val := val -1
  22. []
  23. (i:1..Nproc) proc[i]?init (val) -> skip
  24. ]
  25. MAIN ==
  26. ressource : integer := 0;
  27. [
  28. proc [i:1..Nproc] :: P
  29. ||
  30. sem :: SEMAPHORE
  31. ]