消息队列
来源:互联网 发布:在淘宝产品搜索量最高 编辑:程序博客网 时间:2024/06/11 02:11
http://blog.csdn.net/ethan_novice/article/details/7024066
http://blog.csdn.net/ropenyuan/article/details/5902349
1、消息队列的基本概念
2、消息队列的分类
3、和消息队列相关的结构
struct msg_queue {
};
结构msqid_ds用来设置或返回消息队列的信息,存在于用户空间;
struct msqid_ds{
};
4、
1.
名称::
msgget
功能:
创建消息队列
头文件:
#include <sys/types.h>
#include <sys/msg.h>
#inlcude <sys/ipc.h>
函数原形:
int msgget(key_t key,int msgflag);
参数:
key
flag
返回值:
若成功则为消息队列描述字若出错则为-1。
ipc_perm结构按我们以前说的进行初始化。该结构中mode成员按flag中的相应权限位设置。
msg_qnum,msg_lspid,msg_lrpid,msg_stime,msg_rtime都设置为0。
msg_ctime设置为当前时间。
msg_qbytes设置为系统限制值。
5、获得和修改消息队列属性,删除消息队列
2.
名称::
msgctl
功能:
对消息队列进行多种操作
头文件:
#include <sys/msg.h>
函数原形:
int msgctl(int msqid, int cmd,struct msqid_ds *buf);
参数:
msqid
cmd
buf
返回值:
若成功返回0,若出错返回-1。
IPC_STAT:该命令用来获取消息队列信息,返回的信息存贮在buf指向的msqid_da结构中;
IPC_SET:该命令用来设置消息队列的属性,要设置的属性存储在buf指向的msqid_ds结构中;可设置属性包括:msg_perm.uid、msg_perm.gid、msg_perm.mode以及msg_qbytes,同时,也影响msg_ctime成员。
IPC_RMID:删除msqid_ds标识的消息队列.
6、用消息队列发送和接收消息
3.
名称::
msgsnd
功能:
将数据放到消息队列上
头文件:
#include <sys/types.h>
#include <sys/msg.h>
#inlcude <sys/ipc.h>
函数原形:
int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg);
参数:
msqid
msgp
msgsz
msgflg
返回值:
若成功则为0,若出错则为-1。
名称::
msgrcv
功能:
从队列中取出消息
头文件:
#include <sys/types.h>
#include <sys/msg.h>
#inlcude <sys/ipc.h>
函数原形:
int msgrcv(int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg);
参数:
msqid
msgp
msgsz
msgtyp
msgflg
返回值:
若成功则为消息数据部分的长度,若出错则为-1。
该系统调用从msgid代表的消息队列中读取一个消息,并把消息存储在msgp指向的msgbuf结构中。
7.消息队列与管道以及有名管道的比较
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <stdio.h>
#define KEY 75
void msg_stat(int,struct msqid_ds);
int main()
{
int msgid;
int sflags,rflags;
struct msqid_ds msg;
struct msgbuf{
}msg_buf;
msgid=msgget(KEY,IPC_CREAT);
msg_stat(msgid,msg);
sflags=IPC_NOWAIT;
msg_buf.mtypes=10;
msg_buf.mtext[0]=’a’;
msgsnd(msgid,&msg_buf,sizeof(msg_buf.mtext),sflags);
msg_stat(msgid,msg);
rflags=IPC_NOWAIT|MSG_NOERROR;
msgrcv(msgid,&msg_buf,4,10,rflags);
msg.msg_perm.uid=8;
msg.msg_perm.gid=8;
msg.msg_qbytes=16388;
msgctl(msgid,IPC_SET,&msg);
msg_stat(msgid,msg);
msgctl(msgid,IPC_RMID,NULL);
}
void msg_stat(int msgid,struct msqid_ds msg)
{
printf("current number of bytes on queue is %d\n",msg_info.msg_cbytes);
printf("number of messages in queue is %d\n",msg_info.msg_qnum);
printf("max number of bytes on queue is %d\n",msg_info.msg_qbytes);
printf("pid of last msgsnd is %d\n",msg_info.msg_lspid);
printf("pid of last msgrcv is %d\n",msg_info.msg_lrpid);
printf("last msgsnd time is %s", ctime(&(msg_info.msg_stime)));
printf("last msgrcv time is %s", ctime(&(msg_info.msg_rtime)));
printf("last change time is %s", ctime(&(msg_info.msg_ctime)));
printf("msg uid is %d\n",msg_info.msg_perm.uid);
printf("msg gid is %d\n",msg_info.msg_perm.gid);
}
用消息队列实现进程间通信的例子:
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#define MSGKEY 75
#define MAXLINE 100
struct msgform
{
long mtype;
pid_t pid;
}msg;
int msgqid;
void client()
{
}
main( )
{
}
#include <sys/msg.h>
#include <sys/ipc.h>
#define MSGKEY 75
struct msgform
{
long mtype;
pid_t pid;
}msg;
int msgqid;
void server( )
{
while(msg.mtype!=5);
}
main( )
{
}
./server&
./client
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- 消息队列
- <>消息队列
- 消息队列
- tftp on busybox usage
- 【C++】PAT(advanced level)1046. Shortest Distance (20)
- MAC下使用PhoneGap
- oracle内置函数整理
- WPF几何图形和图画学习笔记
- 消息队列
- supermap学习系列(六)——上一篇的另一种版本(鼠标单击或者移动,高亮显示并弹出对话框)
- 新思想,新起点
- MVCPaging MultiTable 多个Table
- MyISAM和InnoDB选择
- C# Math.Round()函数问题
- 用Java过滤掉不能作为xml内容的特殊字符
- error C3859: 超过了 PCH 的虚拟内存范围
- ant配置与使用