怎么查询消息队列中每条消息的状态

wind123 2005-08-30 04:02:37
用msgctl能将消息队列的基本信息取得,但我想取得队列中的每条消息的状态(如:msgtype,消息的长度等信息),怎么处理呢?
...全文
426 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hundlom 2005-09-01
  • 打赏
  • 举报
回复
System V消息队列的遗失特征是无法窥探一个消息,不能实现网络编程的功能(recv 的MSG_PEEK)请问楼主要消息的什么状态?
yyy790601 2005-09-01
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <linux/ipc.h>
#include <linux/msg.h>

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 %f\n",msg.mtype, msg.request, msg.salary);
}
  • 打赏
  • 举报
回复
msgctl只能查看消息的状态而已,
想取得队列中的每条消息的状态需要使用msgrcv
wind123 2005-08-30
  • 打赏
  • 举报
回复
只是查看消息的状态而已
sharkhuang 2005-08-30
  • 打赏
  • 举报
回复
没发取吧
wind123 2005-08-30
  • 打赏
  • 举报
回复
我的意思是用msgctl怎么取
yyy790601 2005-08-30
  • 打赏
  • 举报
回复
http://oslinux.blogchina.com/oslinux/1908498.html
sharkhuang 2005-08-30
  • 打赏
  • 举报
回复
msgrcv
wind123 2005-08-30
  • 打赏
  • 举报
回复
怎么取
sharkhuang 2005-08-30
  • 打赏
  • 举报
回复
取出来哦

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧