Find memory conflicts among multiple threads in C++

C++Server Side ProgrammingProgramming

Concept

Now consider a RAM organized in blocks. In a RAM, there are multiple processes running onthe system. It should be noted that every application gets following information, (Thread T, Memory Block M, time t, R/W) which essentially indicates that the thread T was Implementing memory block M at time t and operation could be either read or write.

Now we define the Memory conflict as −

– Multiple read operations at the same location are not the reason of conflict.

– It should be noted that one write operation between x+5 to x-5 to location M, will be responsible for causing of conflict for a thread accessing location M at time x where x is termed as some time in standard unit of time measurement.

– For example, if thread T1 accessed memory location M at time x+1 and if a thread T2 accesses location M before time x+6, then T1 and T2 are responsible candidate of conflict given one of them does write operation.

We are given with the list of threads accessing memory locations. Our task is to determineall conflicts.

Input

(1, 932, 1, R)
(2, 512, 2, W)
(3, 932, 3, R)
(4, 512, 4, R)
(5, 432, 5, R)
(6, 512, 6, R)
(7, 835, 7, W)
(8, 432, 8, R)

Output

Threads 2 and 4 conflict.
Threads 2 and 6 conflict.
All other operations are safe

Method

Here, the concept is to sort all threads by memory block and if it has been seen that memory block is same, then perform sorting by time. Now once we have all threads sorted, we can traverse all threads one by one. For every thread being visited, we simply need to verify previous adjacent threads of same block because threads are sorted by time.

  • Determine memory conflicts among multiple threads.

  • Merge two sorted arrays in constant space applying Min Heap.

  • Least increment or decrement operations needed to make the array sorted.

  • We count of elements which are not at the correct position.

  • Select X elements from A[] and Y elements from B[] which satisfy the given condition.

Example

 Live Demo

// C++ program to find memory conflicts among threads
#include<bits/stdc++.h>
using namespace std;
/* Shows structure for details of thread */
struct Thread{
   int id1, memblck1, time1;
   char access1;
};
/* Here, compare function needed for sorting threads
We first sort threads on the basis of memory block,
If they are same, then we sort on the basis of time */
bool compare(const Thread& x1, const Thread& y1){
   if (x1.memblck1 == y1.memblck1)
      return x1.time1 < y1.time1;
   else return x1.memblck1 < y1.memblck1;
}
// Shows function to print all conflicts among threads
void printConflicts(Thread arr1[], int n1){
   /* Used to sort the threads first by memblock, then by
   time */
   sort(arr1, arr1+n1, compare);
   /*Perform starting from second thread till last thread*/
   for (int i = 1; i < n1; i++){
      /* Because threads are sorted, We verify further
      for conflict possibility only if there
         memblock is same*/
      if(arr1[i].memblck1 == arr1[i-1].memblck1){
         /* Because threads with same block are sorted in increasing order of access time.
         So we verify possibility conflict from last thread to all previous threads which
         access the same block of memory such that the current thread access under the
         arr1[i-1].time1 + 5.. and if any of them does read operation than conflict occurs
         at any point memblock1 becomes different or current thread moves out of vulnerable
         time of latest previous processed thread, the loop breaks. */
      if (arr1[i].time1 <= arr1[i-1].time1+5){
         int j = i-1; /* Perform starting with previous thread */
         // Determine all previous conflicting threads
         while (arr1[i].memblck1 == arr1[j].memblck1 &&
            arr1[i].time1 <= arr1[j].time1+5 &&
            j >= 0){
               if (arr1[i].access1 == 'W' || arr1[j].access1 =='W'){
                  cout << "Threads " << arr1[j].id1 << " and "<< arr1[i].id1 <<"conflict.\n";
               }
               j--;
            }
         }
      }
   }
   cout << "All other operations are safe\n";
}
// Driver program to test above function
int main(){
   Thread arr1[] = { {1, 932, 1, 'R'}, {2, 512, 2, 'W'},{3, 932, 3, 'R'}, {4, 512, 4, 'R'},{5, 432, 5, 'R'}, {6, 512, 6, 'R'},{7, 835, 7, 'W'}, {8, 432, 8, 'R'}};
   int n1 = sizeof(arr1)/sizeof(arr1[0]);
   printConflicts(arr1, n1);
   return 0;
}

Output

Threads 2 and 4 conflict.
Threads 2 and 6 conflict.
All other operations are safe
raja
Published on 25-Jul-2020 12:45:23
Advertisements