When using Message Queuing, call the
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct MYMSG
{
Long MyType;
Char even[32];
};
#define VALUE (key_t) 0x1fff
int main ()
{
struct mymsg msg;
int msgid,res;
MsgId = Msgget (VALUE, 0666 | Ipc_creat);
Res =msgsnd (MsgId, &msg, sizeof (msg), 0);
}
After executing the above procedure, there is no error.
Use Ipcs-q to view the message queue, which contains 0.
Looking for a long time, the original is the use of msgsnd () function, the structure of the MyType is not assigned. Causes the message queue not to enter.
Add this sentence to the Msgget () function:
Msg.mytype = 1;
The compile run can see messages in the message queue.
There is also an issue to be aware of using the msgsnd () function, which is if you memset (&msg, 0, sizeof (msg)); mytype=0
This is not the case, mytype=0 can only be MSGRCV () when set, meaning to receive all messages.