/* Send Message Queue */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct MYMSG
{
Long Mtype; /* Message type, must be > 0 */
Char mtext[32];
};
#define KEY (key_t) 0x1fff
Int
Main (int argc, char *argv[])
{
key_t key;
int msgid;
int res;
key = key;
struct mymsg msg;
printf ("Send to parsing process ... \ n");
strcpy (msg.mtext, "haha");
/* Create message Queue */
MsgId = Msgget (Key, 0666 | Ipc_creat);
if (MsgId = =-1)
{
Perror ("Msgget");
}
Msg.mtype = 1;
res = MSGSND (msgid, &msg, sizeof (struct mymsg), 0);
printf ("fa Ok Msg.mtext =%s\n", msg.mtext);
return 0;
}
========================================================
/* Receive Message Queuing */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
struct MYMSG
{
Long mtype;/* message type, must be > 0 */
Char mtext[32]; /* Message Data */
};
#define KEY (key_t) 0x1fff
int main (int argc, char *argv[])
{
key_t key;
int msgid;
int res;
key = key;
struct mymsg msg;
/* Create message Queue */
MsgId = Msgget (Key, 0666 | Ipc_creat);
if (MsgId = =-1)
{
Perror ("Msgget");
}
Msg.mtype = 1;
/* Libnids Grab Package Info */
int i=1;
while (i--)
{
res = MSGRCV (msgid, &msg, sizeof (struct mymsg), 0, 0);
printf ("shou ok, Msg.mtext =%s\n", msg.mtext);
}
return 0;
}
==========================================================
/* Delete Message Queuing */
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
/* key for Message Queuing communication */
#define KEY (key_t) 0x1fff
int Rmqueue (key_t key)
{
int MsgId, RES;
MsgId = Msgget (key, 0);
res = Msgctl (msgid, Ipc_rmid, 0);
return 0;
}
int main (int argc, char *argv[])
{
key_t key;
key = key;
Rmqueue (key);
return 0;
}
==========================================================
/* Get status information for Message Queuing */
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define NIDS_PRO_KEY (key_t) 0x1fff
int main ()
{
key_t key;
key = Nids_pro_key;
int id = msgget (key,0);
if (id = =-1) perror ("Shmget"), Exit (-1);
struct Msqid_ds ds;
Msgctl (ID,IPC_STAT,&DS);
printf ("key=%x\n", Ds.msg_perm.__key);
printf ("mode=%o\n", Ds.msg_perm.mode);
printf ("Current Total size=%d\n", ds.__msg_cbytes);
printf ("Current msg count=%d\n", ds.msg_qnum);
printf ("System max allow MSG size=%d\n", ds.msg_qbytes);
}
Linux Message Queuing related operations