key_t type max length or boundaries value

Hello,

In shared memory, when using shmget function, first parameter is ket_t key.

I know it is an integer type, but length of it is system dependent. That means may not be have integer's ranges.

What is range of key_t in Linux? Is it different in distros, for example in ubuntu & fedora?

I doubt it would be different between Linux distributions. On my 64 bit machine, key_t is 4 bytes. It is trivial to test that on a 32 bit machine as well (I am guessing it is also 4 bytes). Now whether it represents a signed value, or an unsigned value is another question.

You do not worry about possible values for a key.

You assign it a value with the ftok() function. The system creates it for you.

You should not "think up" a number and assign it. The value is based on the path argument and the id argument of ftok(). The id's lowest 8 bits of an integer only are used, so it needs to be non-zero and 1-255. Just any old higher number can be ignored and should not be used, eg. 8192 would yield zero when only the lowest 8 bits are used.

1 Like