Skip to content
Advertisement

How many maximum shared memory keys can be created using ftok() in linux?

I am creating share memory keys using ftok(). I want to create 500 share memory keys.As per man page of ftok() it is mentioned that low order 8 bits are significant.

Does it mean that we can only generate 256 maximum share memory keys? If yes, Then how to generate more than 256 keys?

Because in argument ftok() is defined to take int argument for proj_id:-

key_t ftok(const char *pathname, int proj_id);

So it should generate 2^0 to 2^31 unique keys with same path name.

Advertisement

Answer

The maximum number of keys that can be created by ftok is not limited by the integer argument to ftok. It depends on both the integer argument and the path argument also. Even if only the low-order 8-bits of int argument are significant, the number of paths are in theory innumerable.

The key returned by ftok is of the type key_t which is __S32_TYPE and this is an int. So this can go from 2^0 to 2^31 on an implementation where int has 4 bytes.

As per the man page for ftok:

The ftok() function shall return the same key value for all paths that name the same file, when called with the same id value, and return different key values when called with different id values or with paths that name different files existing on the same file system at the same time. It is unspecified whether ftok() shall return the same key value when called again after the file named by path is removed and recreated with the same name.

But as others have pointed out in the comments, it is not advisable to create so many processes.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement