消息队列

消息队列是消息的链式队列,模型如下:

包括两种数据结构:

msqid_ds消息队列数据结构

msg消息队列数据结构

struct msg_msg{
struct list_head m_list;
long m_type; //消息类型
int m_ts; //消息大小
struct msg_msgseg* next; //下一个消息位置
void *security; //真正消息位置
};

在/usr/include/linux/msg.h文件中定义了队列大小的限制。不同的Linux版本值不同

#define MSGMNI 16  //最大消息队列个数
#define MSGMAX 8192 //消息队列中每个消息最大为8192字节
#define MSGMNB 16384 //每个消息队列最大为16384字节

int msgget (key_t __key, int __msgflg):创建消息队列

  第一个参数:由ftok创建的key值

  第二个参数:低位确定消息队列的访问权限,最终值为perm&~umask. ??不懂

高位包含 #define IPC_CREAT  00001000    //如果key不存在,则创建,存在返回ID

#define IPC_EXCL  00002000     //如果key存在,返回失败

#define IPC_NOWAIT  00004000  //如果需要等待,直接返回错误

int msgctl (int __msqid, int __cmd, struct msqid_ds *__buf) :控制消息队列属性

  第一个参数:消息队列标识符,即msgget的返回值

  第二个参数:执行的控制命令,包括

  •     IPC_STAT 2:读取消息队列属性。取得队列的msqid_ds结构,放入第三个参数
  •     IPC_SET 1:设置消息队列属性。只有有效用户ID等于msg_perm.cuid或msg_perm.uid的进程或者超级用户特权进程可以设置。只有超级用户可以增加msg_qbytes。
  •     IPC_RMID: 删除消息队列。立即生效。仍使用这一消息队列的其他进程在下一次试图对此队列操作时,会出错返回EIDRM。同样只有两种用户可以使用该命令。
  •     IPC_INFO: 读取消息队列基本情况

  第三个参数:存储读取或需要修改的消息队列的属性

int msgsnd (int __msqid, __const void * __msgp, size_t __msgsz, int __msgflg) :发送信息到消息队列,成功返回0,否则-1.同时msg_qnum递增1,msg_lspid设置为调用进程的进程ID,msg_stime设置为当前时间。

  第一个参数:指定的消息队列标识符

  第二个参数:指向用户定义缓冲区

struct msgbuf{
long mtype; //消息类型
char mtext[]; //消息内容,在使用时自己重新定义此结构
};

  第三个参数:接收信息的大小

  第四个参数:指定达到系统限制时采取的操作。设置为IPC_NOWAIT,在需要等待时立即返回错误EAGAIN;设置为0,阻塞调用进程

int msgrcv (int __msqid, void *__msgp, size_t __msgsz, long int __msgtyp, int __msgflg) :从队列中取消息

具体说明太长了,上图:

例子,发送接收消息,读取限制信息

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/msg.h>
#define BUFSIZE 128
struct msg_buf //自定义消息结构体
{
long type;
char msg[BUFSIZE];
};
int main(int argc, char *argv[])
{
key_t key;
int msgid;
struct msg_buf msg_snd, msg_rcv;
struct msginfo buf;
char *ptr = "helloworld";
memset(&msg_snd, '\0', sizeof(struct msg_buf));
memset(&msg_rcv, '\0', sizeof(struct msg_buf));
msg_rcv.type = ;
msg_snd.type = ;
memcpy(msg_snd.msg, ptr, strlen(ptr));
if((key = ftok(".",'A')) == -)
{
perror("ftok");
exit(EXIT_FAILURE);
}
if((msgid=msgget(key,|IPC_CREAT)) == -)
{
perror("msgget");
exit(EXIT_FAILURE);
}
printf("msgsnd_return = %d\n", msgsnd(msgid,(void *)&msg_snd, strlen(msg_snd.msg), ));
msgctl(msgid, MSG_INFO, &buf);
printf("buf.msgmax=%d\n", buf.msgmax);
printf("buf.msgmnb=%d\n", buf.msgmnb);
printf("buf.msgpool=%d\n", buf.msgpool);
printf("buf.semmap=%d\n", buf.msgmap);
printf("buf.msgmni=%d\n", buf.msgmni);
printf("buf.msgssz=%d\n", buf.msgssz);
printf("buf.msgtql=%d\n", buf.msgtql);
printf("buf.msgseg=%d\n", buf.msgseg); printf("msgrcv_return = %d\n", msgrcv(msgid, (void *)&msg_rcv, BUFSIZE,msg_rcv.type,));
printf("rev msg:%s\n", msg_rcv.msg);
printf("msgctl_return=%d\n",msgctl(msgid,IPC_RMID,));
}

下面是照书上抄的消息队列实现实时通信的例子

在我的电脑上无法接收消息????

send端

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/msg.h>
struct msgbuf{
int type;
char ptr[];
};
int main(int argc, char *argv[])
{
key_t key;
key = ftok(argv[], );
int msgid;
msgid = msgget(key, IPC_CREAT|);
if(msgid == -)
{
perror("msgget");
exit(EXIT_FAILURE);
}
printf("key = %d, msgid = %d\n", key, msgid);
pid_t pid;
pid = fork();
if(pid == )
{
struct msgbuf data; //书上是在while循环里每次malloc,后面free 但是我那样写发现每次msgsnd都失败,说identitier removed
while()
{
printf("pls input msg to send:");
char buf[];
fgets(buf, , stdin);
data.type = ;
memcpy(data.ptr, buf, strlen(buf) + );
if(msgsnd(msgid, (void *)&data, strlen(buf) + , ) == -)
{
perror("msgsnd");
exit(EXIT_FAILURE);
}
sleep();
}
}
else
{
struct msgbuf mybuf;
while()
{
memset(&mybuf, '\0', sizeof(mybuf));
if(msgrcv(msgid, &mybuf, , , ) == -)
{
perror("msgrcv");
exit(EXIT_FAILURE);
}
printf("recv msg:%s\n", mybuf.ptr);
}
}
}

receive端代码类似,就是发送和接收的信号类型不一样。

最新文章

  1. HTML5 – 3.加强版ol
  2. Shell displays color output
  3. matrix_2015_1 138 - ZOJ Monthly, January 2015
  4. 【转】在Ubuntu下编译Android源码并运行Emulator
  5. maven, sesame, openrdf, eclipse 的初始学习
  6. C语言中命名空间的实现
  7. JSTL解析——004——core标签库03
  8. [转帖]完美解决NVIDIA最新显卡驱动无法安装。(修改教程篇,各机型都可以)
  9. Linux -atime、mtime、ctime
  10. Win7里面如何把这一堆图标放进那个右下角的小三角框框
  11. asp.net -mvc框架复习(6)-基于MVC实现简单计算器
  12. sklearn.model_selection 的 train_test_split作用
  13. OpenCV环境搭建(一)
  14. 【JVM系列】一步步解析java执行内幕
  15. 动态规划———最长公共子序列(LCS)
  16. linux crontab定时器
  17. 《深入.NET平台和C#编程》内部测试题-笔试试卷答案
  18. Error: failed to execute &#39;C:\Keil\ARM\ARMCC&#39;的解决办法
  19. OAuth和OpenID的区别
  20. EditPlus 3安装的配置操作

热门文章

  1. 【HTML&amp;CSS】 第一章:DTD文档声明
  2. ARC下,不显式指定任何属性关键字时,默认的关键字都有哪些
  3. OVGap 原生与JS交互
  4. Windows Server 2008 R2 集群(OpenService “RemoteRegistry” 失败)笔记
  5. JavaScript中最常用的55个经典技巧,没事的时候看看,拓展解决问题的思路
  6. python 多版本的兼容
  7. 用Linkedhashmap的LRU特性及SoftReference软引用构建二级缓存
  8. windows系统查找文件-通配符的使用
  9. 后端model传入前端JSP页面中的值判断后再取值
  10. sql server获取后天距离某一日期还有多少周的写法