A11 Oct 16
A11 Oct 16
Main Process :
1. The main process takes two positive integers, m and n, as inputs.
2. Create a shared memory and create a two-dimensional matrix M(m,n) in that memory where M is a
matrix of size m x n.
3. Fill each element of the matrix M with a random integer in the range [1,1000].
4. Print “Random Matrix M of size (m,n) is created in shared memory.” where m,n will be the
dimension of matrix M.
5. Print all the values of the matrix M ( in m x n format) in the main process.
6. Create two static integer arrays A and B of size 1000 in the shared memory.
7. Print “Shared Arrays A and B are created”.
8. Spawn 6 (six) “Order Processes” and 3 (three) “Chaos Processes”.
9. Print “I am order” and “I am chaos” on the successful creation of order process and chaos processes
respectively.
10. The order and chaos processes will perform their operations which will be discussed below.
11. After all the order and chaos processes operations are done, print the final matrix M (in a m x n
format) in the main process, destroy the shared memory and terminate the program.
Chaos Process :
1. The chaos process will choose a random cell from the matrix M and update the value with a random
number from 1 to 1000. That means you have to take three random integers i,j,k where i, and j are
the cell row and element numbers respectively and k is the random element with which the cell
needs to be updated i.e. M[i][j] = k.
2. It will print “Chaos: Updated element at cell i x j with value k.” (print appropriate values of i, j, k in
the printed message).
3. The chaos process will store the changed row number i in array A in some index. The corresponding
index of array A in array B will be initialized with macro UNPROCESSED. (Just an example: use
#define UNPROCESSED and initialize with value -1)
4. After each change, the chaos process will sleep for two seconds.
5. After 30 such updates, the chaos process will print “CHAOS PROCESS ENDS” and exit.
6. Ensure that two (out of three) chaos processes can work concurrently in the matrix given they are
not working on the same element.
Order process :
1. Each of the 6 order processes will run the same code. Details start below.
2. The order process will check the shared arrays A and B for new unprocessed entries. If there is any,
then one order process will lock the shared arrays A and B and read one changed row number. Mark
it with a macro PROCESSED and then unlock the arrays A and B.
3. It will print “Order: Detected updated element at row i” (Print the appropriate value of ‘i’ in the
printed message)
4. Next, an order process will take the changed row in the 2D matrix M, sort the changed row in
non-decreasing order and unlock the row of M.
5. It will print “Order: row i is sorted now” (Print the appropriate value of ‘i’ in the printed message).
6. It will then print “older row i: ” [row values before sorting, space separated] (Print the appropriate
value of ‘i’ in the printed message).
7. It will then print “new row i:” [row values after sorting, space separated] (Print the appropriate
value of ‘i’ in the printed message).
8. Then the order processes will check the shared arrays A and B for more unprocessed elements.
9. After processing thirty such updates, order processes will print “ORDER PROCESS ENDS” and
exit.
10. Ensure that five of the six order processes can work concurrently on A, B and M when they are
working on different indices.