Guest User

Untitled

a guest
Jun 12th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. (de *Buckets . 15) # Number of buckets
  2.  
  3. # E/R model
  4. (class +Bucket +Entity)
  5. (rel key (+Key +Number)) # Key 1 .. *Buckets
  6. (rel val (+Number)) # Value 1 .. 999
  7.  
  8.  
  9. # Start with an empty DB
  10. (call 'rm "-f" "buckets.db") # Remove old DB (if any)
  11. (pool "buckets.db") # Create new DB file
  12.  
  13.  
  14. # Create *Buckets buckets with values between 1 and 999
  15. (for K *Buckets
  16. (new T '(+Bucket) 'key K 'val (rand 1 999)) )
  17. (commit)
  18.  
  19.  
  20. # Pick a random bucket
  21. (de pickBucket ()
  22. (db 'key '+Bucket (rand 1 *Buckets)) )
  23.  
  24.  
  25. # First process
  26. (unless (fork)
  27. (seed *Pid) # Ensure local random sequence
  28. (loop
  29. (let (B1 (pickBucket) B2 (pickBucket)) # Pick two buckets 'B1' and 'B2'
  30. (dbSync) # Atomic DB operation
  31. (let (V1 (; B1 val) V2 (; B2 val)) # Get current values
  32. (cond
  33. ((> V1 V2)
  34. (dec> B1 'val) # Make them closer to equal
  35. (inc> B2 'val) )
  36. ((> V2 V1)
  37. (dec> B2 'val)
  38. (inc> B1 'val) ) ) )
  39. (commit 'upd) # Close transaction
  40. (wait 1) ) ) )
  41.  
  42. # Second process
  43. (unless (fork)
  44. (seed *Pid) # Ensure local random sequence
  45. (loop
  46. (let (B1 (pickBucket) B2 (pickBucket)) # Pick two buckets 'B1' and 'B2'
  47. (unless (== B1 B2) # Found two different ones?
  48. (dbSync) # Atomic DB operation
  49. (let (V1 (; B1 val) V2 (; B2 val)) # Get current values
  50. (cond
  51. ((> V1 V2 0)
  52. (inc> B1 'val) # Redistribute them
  53. (dec> B2 'val) )
  54. ((> V2 V1 0)
  55. (inc> B2 'val)
  56. (dec> B1 'val) ) ) )
  57. (commit 'upd) # Close transaction
  58. (wait 1) ) ) ) )
  59.  
  60. # Third process
  61. (unless (fork)
  62. (loop
  63. (dbSync) # Atomic DB operation
  64. (let Lst (collect 'key '+Bucket) # Get all buckets
  65. (for This Lst # Print current values
  66. (printsp (: val)) )
  67. (prinl # and total sum
  68. "-- Total: "
  69. (sum '((This) (: val)) Lst) ) )
  70. (rollback)
  71. (wait 2000) ) ) # Sleep two seconds
  72.  
  73. (wait)
Add Comment
Please, Sign In to add comment