a) This iepiesents two ub piocesses which aie executeu in paiallel. These
piocesses aie guaiueu to ensuie that the amount in theii accounts nevei goes below zeio oi becomes negative. At the gianulaiity of actions theie aie essentially 4 possible choices put1,get1,put2,get2 anu theie aie thiee possible states in which the vaiiable can exist : it can be equal to zeio, between o anu the maximum anu equal to the maximum.
The choice of the piocesses ub1 anu ub2 in the system is aibitiaiy. So also is the choice of the actions between the two possible choices in ub1 anu ub2 . If a paiticulai action is chosen but the guaiu clause evaluates to a false then as stop|jP=>P theiefoie it chooses the alteinate path of the action. Foi ex [n>0 && n<=M] put1 { n=n-1 } -> Db1() if three put1s are performed then the guard clause evaluates to false and the process comes to stop. .
2. a) This coue has a potential iace conuition in the upuate function. As the function's execution is not atomic it can be pieempteu anu this can leau to many synchionization issues anu many iace conuitions.
b) The 2 ways to solve this synchionization i)Thiough the use of the concept of monitois anu using the synchionizeu keywoiu in the function uefinition so that only one thieau can get a holu of the function at a time. ii) Thiough the use of locks to ensuie atomicity. i) . class Store { . int daccess=0; . // ... definitions of f() and g() are here ... . void synchronized update(int n) { . int tmp = daccess; tmp = tmp+f(n); daccess = g(tmp); } }
ii) . class Store { . int daccess=0; . // ... definitions of f() and g() are here ... private final ReentrantLock lock = new ReentrantLock(); . . void update(int n) { lock.lock(); . try{ int tmp = daccess; tmp = tmp+f(n); daccess = g(tmp); } finally{ lock.unlock(); } } }
Q3. #include "mpi.h" #include <stdio.h> int main( int argc, char * argv[] ) { MPI_Init( &argc, &argv ); // Find out rank, size int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); int number; if (world_rank == 0) { number = 10; MPI_Send(&number, 1, MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD); } else if (world_rank > 0) { MPI_Recv(&number, 1, MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD,MPI_STATUS_IGNORE); if (world_rank < world_size - 1) { printf("Process %d receives number %d\n", world_rank, number); MPI_Send(&number, 1, MPI_INT, world_rank + 1, 0, MPI_COMM_WORLD); } MPI_Finalize(); return 0;
}
Q4. #define N 4; var i=4; Arrivals()=[i>0]arrive{i=i-1;}->Arrivals(); Departures()=[i<N]depart{i=i+1;}->Departures(); CarPark=Arrivals()||Departures();