From the POSIX documentation for msgsnd
:
The application shall ensure that the argument msgp points to a user-defined buffer that contains first a field of type long specifying the type of the message, and then a data portion that holds the data bytes of the message. The structure below is an example of what this user-defined buffer might look like:
struct mymsg { long mtype; /* Message type. */ char mtext[1]; /* Message text. */ }
You're not following that, so your code can't work. Add an long
field at the start of your structure, and set it to 2
since that's what your receiver is expecting as a message type.
Also move that structure definition to a header file so that you can share it between your two pieces of code rather than duplicate it.
if(msgqid=msgget(MsgKey, 0600 | IPC_CREAT)<0)
Add parens around the assignment (in both pieces of code):
if((msgqid=msgget(MsgKey, 0600 | IPC_CREAT)) < 0)
<
and the other comparison operators have higher precedence than the assignment operator, so your code is incorrect without the parens - it assigns the result of the comparison to msgqid
.
And finally, main returns an int
, never void
.