简体   繁体   中英

Creation of Multiple Message Queues, with Unique Keys for the queues

We create multiple queues for message passing from one process to other. But sometimes the keys of the message queues get overlapped ( same keys for separate queues). In order to avoid this I thought of using IPC_PRIVATE with the function msgget() which is used in the creation of Message queues.

This link says I should use IPC_PRIVATE, My application is critical so I have to avoid the current situation. syntax of this msgget is int msgget(key_t key, int msgflg); . Please tell me the syntax for msgget with IPC_PRIVATE and wether it will solve my issue ?

Please tell me the syntax for msgget with IPC_PRIVATE

IPC_PRIVATE is a value of type key_t . You pass it as the literal key value:

int flags = /* ... permission bits ... */;
int my_mq = msgget(IPC_PRIVATE, flags);

You do not need to include IPC_CREAT or IPC_EXCL among the flags when you use IPC_PRIVATE ; if you do include them then they will be ignored.

and wether it will solve my issue ?

Every successful call to msgget() with key IPC_PRIVATE creates a new message queue and returns its identifier, so this approach will definitely prevent inadvertent queue sharing. It also, however, makes it harder to perform intentional queue sharing, and that may present its own issue for you. After all, message queues are an IPC mechanism, so it seems likely that you do want certain processes to share queues. Facilitating that is the whole point of key-based queue access.

As an alternative, the usual way to access queues revolves around generating keys with ftok() , based on a distinguishing well-known or computable file name. The file name might be that of a working directory, a central executable, or a configuration file. By appropriate choice of the distinguishing file, different groups of cooperating executables can obtain different keys. The second parameter to ftok() enables cooperating processes to generate (the same) set of up to 256 common keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM