Memo when multi-process exclusion is performed when the executable file is different in C language
If you don't care too much, the key used in shmget was IPC_PRIVATE, but in multi-process, shared memory is created for each. In that state, fork after creating mutex works well, but if the executable file is different, it cannot be excluded well.
Therefore, by specifying the key with shmget, the same shared memory can be used for another executable file.
ftok will create a key with an existing file path and a proj_id greater than or equal to 0.
Use the created key to shmget.
If IPC_CREAT | IPC_EXCL is specified, the shared memory for key will be created if it does not exist, and an error will occur if it already exists. This makes it possible to determine whether or not to perform the subsequent initialization-related processing.
Create a mutex object as usual.
Lock / unlock where needed, as usual.
--If the executable file is different, you must specify the shared memory key. --If you have only one executable file, you can just use pthread_mutexattr_setpshared. --IPC_PRIVATE can be used.
--Confirmation of probability of ftok collision
Recommended Posts