Linux消息队列
来源:互联网 发布:中国电信云计算北京 编辑:程序博客网 时间:2024/05/20 05:06
由于项目需要用到消息队列,所以这里开始学习了下:
消息队列是进程间通信的一种实现方法,此外还有共享内存,信号量。
进程间通信就是两个main函数交互的过程,因此在程序之外开辟一片开阔的空间供两个程序交互使用根据这个区域的数据结构及开辟的方法有了以上的三种方法。
下面介绍下消息队列:
数据结构用到了
struct ipc_ids {
int in_use;
unsigned short seq;
unsigned short seq_max;
struct rw_semaphore rw_mutex;
struct idr ipcs_idr;
};
struct msg_queue {
struct kern_ipc_perm q_perm;
time_t q_stime; /* last msgsnd time */
time_t q_rtime; /* last msgrcv time */
time_t q_ctime; /* last change time */
unsigned long q_cbytes; /* current number of bytes on queue */
unsigned long q_qnum; /* number of messages in queue */
unsigned long q_qbytes; /* max number of bytes on queue */
pid_t q_lspid; /* pid of last msgsnd */
pid_t q_lrpid; /* last receive pid */
struct list_head q_messages;
//struct list_head 是linux内核实现的双向链表结构,已经实现了一定的操作
struct list_head q_receivers;
struct list_head q_senders;
//这里面用三个双链表来记录消息的本身,发送者和接收者
};
struct kern_ipc_perm
{
spinlock_t lock;
int deleted;
int id;
key_t key;
uid_t uid;
gid_t gid;
uid_t cuid;
gid_t cgid;
mode_t mode;
unsigned long seq;
void *security;
};
消息队列的操作
1、 打开(获取)或创建消息队列
2、 读写操作
3、 获得或设置消息队列属性
具体对应如下API(只列出常用的)
int proj_id=5;
char *pathname="/";
key_t key=ftok(pathname,proj_id);
1)int msgget(key_t key, int msgflg) -->获取或创建
2)int msgrcv(int msqid, struct msgbuf *msgp, int msgsz, long msgtyp, int msgflg);
-->消息的接收(截获)
3)int msgsnd(int msqid, struct msgbuf *msgp, int msgsz, int msgflg);
-->消息的发送
4)int msgctl(int msqid, int cmd, struct msqid_ds *buf);
-->消息的控制(状态获取,属性设置,队列的销毁)
具体怎么用不在这里列出,可以参考前面的程序和manpages
另外在使用消息队列时,要通过一结构体告知系统其具体的类型
这一步是需要自己定义的,
struct msgbuf{
long mtype; //消息的类型
char mtext[1]; //消息的内容
};
两个程序的交互当然要用到两个程序了,下面的测试程序
recv.c
- Linux消息队列编程
- linux消息队列
- linux消息队列操作
- linux IPC-消息队列
- linux消息队列函数
- Linux消息队列操作
- linux 消息队列
- linux IPC-消息队列
- Linux的消息队列
- Linux消息队列
- Linux消息队列
- linux消息队列
- linux消息队列
- Linux消息队列
- linux 消息队列 实例
- linux操作系统消息队列
- linux 消息队列 实例
- 自学Linux -- 消息队列
- poj 2146 一道 求最短编辑距离的dp
- MATLAB实用函数及技巧整理
- Matlab常用工具箱介绍(英汉对照)
- VS2010 编译出现 FileTracker : error FTK1011
- 给程序计时的方法1
- Linux消息队列
- 同步和异步的区别
- Fibonacci Sequence
- 联想本装系统stop:0X000007B错误[转]
- 【转】minigui 1.6.10 + tslib 触摸屏较正方案
- 素数检验算法
- nfs挂载根文件系统
- MAC OS X系统安装.
- ARM开发板如何挂载NFS开发环境的方法步骤(mini2440无盘启动)