当前位置: 技术问答>linux和unix
怎么查询消息队列中每条消息的状态
来源: 互联网 发布时间:2015-10-17
本文导语: 用msgctl能将消息队列的基本信息取得,但我想取得队列中的每条消息的状态(如:msgtype,消息的长度等信息),怎么处理呢? | #include #include #include #include struct mymsgbuf { long mtype; //...
用msgctl能将消息队列的基本信息取得,但我想取得队列中的每条消息的状态(如:msgtype,消息的长度等信息),怎么处理呢?
|
#include
#include
#include
#include
struct mymsgbuf
{
long mtype; // Message type
int request; // Work request number
double salary; // Employee's salary
};
/*打开或者创建一个消息队列*/
int open_queue( key_t keyval )
{
int qid;
if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
{
return(-1);
}
return(qid);
}
/*往消息队列发送一条消息*/
int send_message( int qid, struct mymsgbuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct mymsgbuf) - sizeof(long);
if((result = msgsnd( qid, qbuf, length, 0)) == -1)
{
return(-1);
}
return(result);
}
/*从消息队列中读消息,成功读取后,该消息会被删除*/
int read_message( int qid, long type, struct mymsgbuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct mymsgbuf) - sizeof(long);
if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
{
return(-1);
}
return(result);
}
/*修改队列存取模式*/
int change_queue_mode( int qid, char *mode )
{
struct msqid_ds tmpbuf;
/* Retrieve a current copy of the internal data structure */
msgctl( qid,IPC_STAT, &tmpbuf);
/* Change the permissions using an old trick */
sscanf(mode, "%ho", &tmpbuf.msg_perm.mode);
/* Update the internal data structure */
if( msgctl( qid, IPC_SET, &tmpbuf) == -1)
{
return(-1);
}
return(0);
}
/*删除消息队列*/
int remove_queue( int qid )
{
if( msgctl( qid, IPC_RMID, 0) == -1)
{
return(-1);
}
return(0);
}
main()
{
int qid;
key_t msgkey;
struct mymsgbuf msg;
/* Generate our IPC key value */
msgkey = ftok(".", 'm');
/* Open/create the queue */
if(( qid = open_queue( msgkey)) == -1)
{
perror("open_queue");
exit(1);
}
/* Load up the message with arbitrary test data */
msg.mtype = 1; /* Message type must be a positive number! */
msg.request = 1; /* Data element #1 */
msg.salary = 1000.00; /* Data element #2 (my yearly salary!) */
/* Bombs away! */
if((send_message( qid, &msg )) == -1)
{
perror("send_message");
exit(1);
}
if((read_message(qid, 0, &msg )) == -1)
{
perror("read_mesage");
exit(1);
}
printf("%ld %d %fn",msg.mtype, msg.request, msg.salary);
}
#include
#include
#include
struct mymsgbuf
{
long mtype; // Message type
int request; // Work request number
double salary; // Employee's salary
};
/*打开或者创建一个消息队列*/
int open_queue( key_t keyval )
{
int qid;
if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
{
return(-1);
}
return(qid);
}
/*往消息队列发送一条消息*/
int send_message( int qid, struct mymsgbuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct mymsgbuf) - sizeof(long);
if((result = msgsnd( qid, qbuf, length, 0)) == -1)
{
return(-1);
}
return(result);
}
/*从消息队列中读消息,成功读取后,该消息会被删除*/
int read_message( int qid, long type, struct mymsgbuf *qbuf )
{
int result, length;
/* The length is essentially the size of the structure minus sizeof(mtype) */
length = sizeof(struct mymsgbuf) - sizeof(long);
if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
{
return(-1);
}
return(result);
}
/*修改队列存取模式*/
int change_queue_mode( int qid, char *mode )
{
struct msqid_ds tmpbuf;
/* Retrieve a current copy of the internal data structure */
msgctl( qid,IPC_STAT, &tmpbuf);
/* Change the permissions using an old trick */
sscanf(mode, "%ho", &tmpbuf.msg_perm.mode);
/* Update the internal data structure */
if( msgctl( qid, IPC_SET, &tmpbuf) == -1)
{
return(-1);
}
return(0);
}
/*删除消息队列*/
int remove_queue( int qid )
{
if( msgctl( qid, IPC_RMID, 0) == -1)
{
return(-1);
}
return(0);
}
main()
{
int qid;
key_t msgkey;
struct mymsgbuf msg;
/* Generate our IPC key value */
msgkey = ftok(".", 'm');
/* Open/create the queue */
if(( qid = open_queue( msgkey)) == -1)
{
perror("open_queue");
exit(1);
}
/* Load up the message with arbitrary test data */
msg.mtype = 1; /* Message type must be a positive number! */
msg.request = 1; /* Data element #1 */
msg.salary = 1000.00; /* Data element #2 (my yearly salary!) */
/* Bombs away! */
if((send_message( qid, &msg )) == -1)
{
perror("send_message");
exit(1);
}
if((read_message(qid, 0, &msg )) == -1)
{
perror("read_mesage");
exit(1);
}
printf("%ld %d %fn",msg.mtype, msg.request, msg.salary);
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。