Message Queue function, queue Function

Source: Internet
Author: User

Message Queue function, queue Function

1. Definition of Message Queue struct

Typedef struct {
Uid_t uid;/* owner's user id */
Gid_t gid;/* owner's group id */
Udi_t cuid;/* creator's user id */
Gid_t cgid;/* creator's group id */
Mode_t mode;/* read-write permissions 0400 MSG_R 0200 MSG_W */
Ulong_t seq;/* slot usage sequence number */
} Ipc_perm;

Typedef stuct {
Struct ipc_perm msg_perm;/* read_write perms */
Struct msg * msg_first;/* ptr to first message on queue */
Struct msg * msg_last;/* ptr to last message on queue */
Msglen_t msg_cbytes;/* used bytes current on queue */
Msgqnum_t msg_qnum;/* current num of message on queue */
Msglen_t msg_qbytes;/* max # of bytes allowed on queue */
Pid_t msg_lspid;/* pid of last msgsnd ()*/
Pid_t msg_lrpid;/* pid of last msgrcv ()*/
Time_t msg_stime;/* time of last msgsnd ()*/
Time_t msg_rtime;/* time of last msgrcv ()*/
Time_t msg_ctime;/* time of last msgctl ()*/
} Msqid_ds;

Typedef struct
{
Long mtype;
Char mbuf [MSGLEN];
} Message;

2. Create a message queue:

/*************************************** ************
Function:
Int msgget (ket_t key, int oflag );
Explain:
Create or view a message queue
Return:
A int indemo-
Include:
Sys/msg. h
Introduction:
Oflag: 0400 msg_r
0200 msg_w
0600 msg_wr
Ipc_creat: NO exist and then creat a queue
Exist: reference a queue
Ipc_creat | ipc_excl: NO exist and then creat a queue
Exist: return error
**************************************** ************/
# Include <stdio. h>
# Include <sys/msg. h>
# Include <stdlib. h>

Int MsgGet (int key)
{
Int ret;
Ret = msgget (key, 0600 | IPC_CREAT );
// Ret = msgget (key, 0600 | IPC_CREAT | IPC_EXCL );
If (ret <0)
Perror ("creat msgid error ");
Printf ("msgid = % d/n", ret );
System ("ipcs-q-I ret ");
Return ret;
}
Int main (int argc, char * agrv [])
{
Int key;
Printf ("pleasse input msgkey :");
Scanf ("% d", & key );
MsgGet (key );
Return 0;
}

3. Send the message msgsnd to the Message Queue

/*************************************** **************************************** ****
Function:
Int msgsnd (int msqid, const void * ptr, size_t length, int flag)
Explain:
Send a message to a queue
Return:
Len: send message len;
Include:
Sys/msg. h
Introduction:
Flag: 0: if queue full wait: 1> space for storing new messages
2> message queues identified by msqid are deleted from the system (EIDRM error is returned)
3> the call thread is interrupted by a captured signal (an EINTR error is returned)
IPC_NOWAIT: If there is no space to store new messages, the function returns immediately
1> the specified queue contains too many bytes.
2> too many messages exist in the system scope
**************************************** **************************************** *********/
# Include "typemsg. h"
Int MsgSnd (int msqid, char * buf, int len, int flag)
{
Int ret;
Ret = msgsnd (msqid, buf, len, flag );
If (ret <0)
Perror ("msgsnd error ");
System ("ipcs-q ");
Return ret;
}

Int main ()
{
Int msqid, len, stype;
Message msgb;
Memset (& msgb, 0, sizeof (Message ));
Printf ("msgsnd: please input msqid :");
Scanf ("% d", & msqid );
Printf ("please input msgtype :");
Scanf ("% d", & stype );
Msgb. mtype = stype;
Strcpy (msgb. mbuf, "zhangweia ");
MsgSnd (msqid, (char *) & msgb, sizeof (Message), 0 );
Return 0;
}

4. Get the message msgrcv from the queue

/*************************************** ******************************
Function:
Int msgrcv (int msqid, const void * ptr, size_t msglen, long type, int flag)
Explain:
Recv message order by type
Msgrcv error: Argument list too long --> the length of msglen is smaller than the length of the message in the message body.
Para:
Ptr: point to message struct
Msglen: the size of the Data Part in the buffer directed by ptr. This is the maximum data volume that the function can return.
Type: message type;

1> 0: return the earliest message in the queue.
2> greater than 0: return the first message of type in the message queue.
3> less than 0: return the first message of the smallest type in the message queue whose type is smaller than or equal to the absolute value of type.
Flag: 0 <wait> when there is no message or the message type does not match, the thread waits
Response: 1> A message of the requested type can be obtained.
2> the msqid message queue is deleted by the system and an EIDRM is returned.
3> the call thread is interrupted by a captured signal.
IPC_NOWAIT: If no data exists, an ENOMSG error is returned immediately.
MSGNOERROR: When the received message data is longer than msglen, the system obtains the truncated data. Otherwise, the E2BIG error is returned.
Return:
Message len
**************************************** *****************************/
# Include "typemsg. h"
Int MsgRcv (int msqid, char * buf, int msglen, long type, int flag)
{
Int ret;
Ret = msgrcv (msqid, buf, msglen, type, flag );
If (ret <0)
Perror ("msgrcv error ");
System ("ipcs-q ");
Return ret;

}

Int main ()
{
Int msqid, len;
Long ttype;
Message mbuf;
Printf ("msgrcv: please input recv msqid :");
Scanf ("% d", & msqid );
MsgRcv (msqid, (char *) & mbuf, 8900,0, IPC_NOWAIT );
Printf ("recv message = % s/n", mbuf. mbuf );
Put_String (unsigned char *) & mbuf, sizeof (Message ));
Return 0;
}

6. Message Queue control msgctl

/*************************************** *******************
Function:
Int msgctl (int msqid, int cmd, struct msqid_ds * buff)
Explain:
Cdm: IPC_RMID; delete msqid
IPC_SET:
IPC_STAT: return msqid stat

**************************************** *****************/
# Include "typemsg. h"
Int MsgCtl (int msqid, int cmd, struct msqid_ds * buff)
{
Int ret;
Ret = msgctl (msqid, cmd, buff );
If (ret <0)
{
Perror ("msgctl error ");
Return-1;
}
Return 0;
}

Int main ()
{
Int msqid, type;
Struct msqid_ds info;
Printf ("please input msqid/nand type (1: icp_rmid; 2: ipc_stat )");
Scanf ("% d", & msqid, & type );
If (type = 1)
{
MsgCtl (msqid, IPC_RMID, NULL );
Printf ("delete queue success: % d/n", msqid );
} Else if (type = 2)
{
MsgCtl (msqid, IPC_STAT, & info );
Printf ("get queue stat: % d/n", msqid );
}
Return 0;

}

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.