How to resolve the algorithm Atomic updates step by step in the Run BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Atomic updates step by step in the Run BASIC programming language
Table of Contents
Problem Statement
Define a data type consisting of a fixed number of 'buckets', each containing a nonnegative integer value, which supports operations to: In order to exercise this data type, create one set of buckets, and start three concurrent tasks:
The display task need not be explicit; use of e.g. a debugger or trace tool is acceptable provided it is simple to set up to provide the display. This task is intended as an exercise in atomic operations. The sum of the bucket values must be preserved even if the two tasks attempt to perform transfers simultaneously, and a straightforward solution is to ensure that at any time, only one transfer is actually occurring — that the transfer operation is atomic.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Atomic updates step by step in the Run BASIC programming language
Source code in the run programming language
DIM bucket(10)
FOR i = 1 TO 10 : bucket(i) = int(RND(0)*100) : NEXT
a = display(" Display:") ' show original array
a = flatten(a) ' flatten the array
a = display(" Flatten:") ' show flattened array
a = transfer(3,5) ' transfer some amount from 3 to 5
a = display(a;" from 3 to 5:") ' Show transfer array
end
FUNCTION display(a$)
print a$;" ";chr$(9);
for i = 1 to 10
display = display + bucket(i)
print bucket(i);chr$(9);
next i
print " Total:";display
END FUNCTION
FUNCTION flatten(f)
f1 = int((f / 10) + .5)
for i = 1 to 10
bucket(i) = f1
f2 = f2 + f1
next i
bucket(10) = bucket(10) + f - f2
END FUNCTION
FUNCTION transfer(a1,a2)
transfer = int(rnd(0) * bucket(a1))
bucket(a1) = bucket(a1) - transfer
bucket(a2) = bucket(a2) + transfer
END FUNCTION
You may also check:How to resolve the algorithm Priority queue step by step in the Lasso programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the ActionScript programming language
You may also check:How to resolve the algorithm Null object step by step in the Slate programming language
You may also check:How to resolve the algorithm IBAN step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the Tailspin programming language