I have created a message queue and the sender part successfully creates and sends the message to the message queue.
I have used IPC_PRIVATE as key in msgget() on the sender side.
Now my question is, what key to use in msgget() on the receiver side ?
Using IPC_PRIVATE on the receiver side as key in msgget() does not receive message and fails.
I should also mention that msgsnd() in the sender part indicates an error (returns -1), but when printing with perror(), the output is Success and the message is sent to the message queue successfully and can be seen using ipcs -q command at the terminal. I don’t know why this happens.
if(msgsnd(msqid,&msgp,88,IPC_NOWAIT) == 0)
{
perror("\nsend : msgsnd FAIL");
msgctl(msqid,IPC_RMID,buf);
return 1;
}
Output :
send : msgsnd FAIL: Success
You are going to have to use a common key value between your two independent processes … using
IPC_PRIVATEmeans you are not planning on sharing the queue between two processes unless the secondary process has been forked from the first process. Because of the forking operation, the child will inherent the queue identifier from the parent process, so usingIPC_PRVATEin that scenario is okay. But because usingIPC_PRIVATEcreates a unique key-value for every call its used in, for scenarios where you have two completely independent processes, such as a server/client relationship, you will need to create a common key … it can either be a “magic number” that you share between all the processes that is already not in-use by another queue, shared memory segment, etc., or you can create a key off a common file in the filesystem by usingftok().