Search code examples
clinuxshared-memoryftok

how to determine ftok shared memory collision


I'm trying to debug a problem with some legacy code. While trying to understand what I'm looking at, I found that it builds two unique shared memory space using ftok. I looked online to see what it does and I stumbled upon this link. I looked deeper and discovered that ftok does not guarantee unique keys and there's a small possibility that the spaces might collide.

ftok() collisions

So, I'm stuck trying to determine whether this is the case for me or not. How do I determine whether it's really an shared memory space collision problem? It can be a horrible memory leak in the legacy executables...


Solution

  • How do I determine whether it's really an shared memory space collision problem?

    Easy: call ftok() with the same parameters as your real code, and compare the two ints that are returned for equality.

    So as long as key returned are unique the shared memory space generated are also free from collisions?

    So long as the keys are distinct, the two memory segments are completely independent.

    The application is free to put any values into the two segments it desires. If the application wants to put values into e.g. segment1 that point to values in segment2, it's up to the application to keep all of the references straight.