Consider a RAM organized in blocks. There are multiple processes running on the system. Every application gets below information.
(Thread T, Memory Block M, time t, R/W) which essentially tells that the thread T was using memory block M at time t and operation could be read or write.
Memory conflict is defined as –
– Multiple read operations at the same location are not cause of conflict.
– One write operation between x+5 to x-5 to location M, will be cause of conflict for a thread accessing location M at time x where x is some time in standard unit of time measurement.
– 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 candidate of conflict given one of them does write operation.
You are given with the list of threads accessing memory locations, you have to find all conflicts.
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.
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to sort all threads by memory block and if memory block is same, then by time. Once we have all threads sorted, we can traverse all threads one by one. For every thread being traversed, we simply need to check previous adjacent threads of same block as threads are sorted by time.
Below is C++ implementation of this idea.
threads 3 and 5 conflict. threads 1 and 5 conflict. All other operations are same
Time complexity: The above solution uses sorting to sort threads. Sorting can be done in O(nLogn) time. Then it prints all conflicts. Printing all conflicts takes O(n + m) time where m is number of conflicts. So overall time complexity is O(nLogn + m).
This article is contributed by Gaurav Ahirwar. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to find max memory, free memory and total memory in Java?
- Find the largest multiple of 2, 3 and 5
- Find First element in AP which is multiple of given prime
- Find the largest multiple of 3 | Set 1 (Using Queue)
- Find multiple of x closest to or a ^ b (a raised to power b)
- Quickly find multiple left rotations of an array | Set 1
- Find the smallest binary digit multiple of given number
- Find any one of the multiple repeating elements in read only array
- Find the largest multiple of 3 from array of digits | Set 2 (In O(n) time and O(1) space)
- Introduction to memory and memory units
- Joining Threads in Java
- Sharing a queue among three threads
- Print 1 2 3 infinitely using threads in C
- Output of Java program | Set 16 (Threads)
- Output of Python program | Set 16 (Threads)