📜  查找多个线程之间的内存冲突

📅  最后修改于: 2021-05-07 18:59:02             🧑  作者: Mango

考虑一个按块组织的RAM。系统上正在运行多个进程。每个应用程序都会获得以下信息。

(线程T,内存块M,时间t,R / W)从本质上讲,线程T在时间t正在使用内存块M,并且可以读取或写入操作。

内存冲突定义为–
–在同一位置的多次读取操作不会引起冲突。
–在x + 5到x-5之间对位置M进行一次写操作将导致线程在时间x访问位置M发生冲突,其中x是标准时间单位。
–示例–如果线程T1在时间x + 1访问了存储单元M,并且线程T2在时间x + 6之前访问了存储单元M,则T1和T2是冲突的候选对象,因为其中之一进行了写操作。

给出了访问内存位置的线程列表,您必须查找所有冲突。

例子-

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

Output:
Thread 1 & 3 conflict with thread 5
All other operations are safe. 

强烈建议您最小化浏览器,然后自己尝试。
想法是按内存块对所有线程排序,如果内存块相同,则按时间排序。一旦所有线程排序完毕,便可以一一遍历所有线程。对于每个遍历的线程,我们只需要检查同一块的先前相邻线程,因为线程是按时间排序的。

以下是此想法的C++实现。

// C++ program to find memory conflicts among threads
#include
using namespace std;
  
/* Structure for details of thread */
struct Thread
{
    int id, memblck, time;
    char access;
};
  
/* 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& x, const Thread& y)
{
    if (x.memblck == y.memblck)
        return x.time < y.time;
    else return x.memblck < y.memblck;
}
  
// Function to print all conflicts among threads
void printConflicts(Thread arr[], int n)
{
    /* Sort the threads first by memblock, then by
       time */
    sort(arr, arr+n, compare);
  
    /*start from second thread till last thread*/
    for (int i = 1; i < n; i++)
    {
        /* As threads are sorted, We check further
           for conflict possibility only if there
           memblock is same*/
        if(arr[i].memblck == arr[i-1].memblck)
        {
  
            /* As threads with same block are sorted in increasing order
               of access time. So we check possibility conflict from last
               thread to all previous threads which access the same block
               of memory such that the current thread access under the
               arr[i-1].time + 5.. and if any of them does read operation
               than conflict occurs. at any point memblock becomes different
               or current thread moves out of vulnerable time of latest
               previous processed thread, the loop breaks.
            */
            if (arr[i].time <= arr[i-1].time+5)
            {
                int j = i-1; /* start with previous thread */
  
                // Find all previous conflicting threads
                while (arr[i].memblck == arr[j].memblck &&
                        arr[i].time <= arr[j].time+5 &&
                        j >= 0)
                {
                    if (arr[i].access == 'W' || arr[j].access == 'W')
                    {
                        cout << "threads " << arr[j].id << " and "
                             << arr[i].id << " conflict.\n";
                    }
                    j--;
                }
            }
        }
    }
  
    cout << "All other operations are same\n";
}
  
// Driver program to test above function
int main()
{
    Thread arr[] = { {1, 512, 1, 'R'}, {2, 432, 2, 'W'},
                     {3, 512, 3, 'R'}, {4, 932, 4, 'R'},
                     {5, 512, 5, 'W'}, {6, 932, 6, 'R'},
                     {7, 835, 7, 'R'}, {8, 432, 8, 'R'}
                   };
  
    int n = sizeof(arr)/sizeof(arr[0]);
    printConflicts(arr, n);
    return 0;
}

输出:

threads 3 and 5 conflict.
threads 1 and 5 conflict.
All other operations are same

时间复杂度:以上解决方案使用排序对线程进行排序。排序可以在O(nLogn)时间中完成。然后打印所有冲突。打印所有冲突需要O(n + m)时间,其中m是冲突数。因此,总体时间复杂度为O(nLogn + m)。