Linux Message Queuing related operations

Source: Internet
Author: User
Tags message queue

/* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.