1. #include"sys/types.h"
  2. #include "sys/msg.h"
  3. #include "unistd.h"
  4. #include"stdio.h"
  5. void msg_stat(int,struct msqid_ds);
  6. int main()
  7. {
  8. int gflags,sflags,rflags;
  9. key_t key;
  10. int msgid;
  11. int reval;
  12. struct msgbuf{
  13. int mtype;
  14. char mtext[1];
  15. }msg_sbuf;
  16. struct msgmbuf{
  17. int mtype;char mtext[10];
  18. }msg_rbuf;
  19. struct msqid_ds msg_ginfo,msg_sinfo;
  20. char* msgpath="/UNIX/msgqueue";
  21. key=ftok(msgpath,'a');
  22. gflags=IPC_CREAT|IPC_EXCL;
  23. msgid=msgget(key,gflags|00666);
  24. if(msgid==-1){
  25. printf("msg create error!\n");
  26. return;
  27. }
  28. /*创建一个消息队列后,输出消息队列默认属性*/
  29. msg_stat(msgid,msg_ginfo);
  30. sflags=IPC_NOWAIT;
  31. msg_sbuf.mtype=10;
  32. msg_sbuf.mtext[0]='a';
  33. reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);
  34. if (reval==-1)
  35. {
  36. printf("message send error!\n");
  37. }
  38. /*发送一个消息后,输出消息队列属性*/
  39. msg_stat(msgid,msg_ginfo);
  40. rflags=IPC_NOWAIT|MSG_NOERROR;
  41. reval=msgrcv(msgid,&msg_rbuf,4,10,rflags);
  42. if (reval==-1)
  43. {
  44. printf("Read msg error!\n");
  45. }
  46. else
  47. printf("Read from msg queue %d bytes\n",reval);
  48. /*从消息队列中读出消息后,输出消息队列属性*/
  49. msg_stat(msgid,msg_ginfo);
  50. msg_sinfo.msg_perm.uid=8;
  51. msg_sinfo.msg_perm.gid=8;
  52. msg_sinfo.msg_qbytes=16388;
  53. /************************************************************************/
  54. /* 此处验证超级用户可以更改消息队列的默认msg_qbytes                     */
  55. //注意这里设置的值大于最大默认值
  56. /************************************************************************/
  57. reval=msgctl(msgid,IPC_SET,&msg_sinfo);
  58. if(reval==-1){
  59. printf("msg set info error!\n");
  60. return;
  61. }
  62. msg_stat(msgid,msg_ginfo);
  63. /************************************************************************/
  64. /* 验证设置消息队列属性                                                 */
  65. /************************************************************************/
  66. reval=msgctl(msgid,IPC_RMID,NULL);//删除消息队列
  67. if (reval==-1)
  68. {
  69. printf("unlink msg queue error!\n");
  70. return;
  71. }
  72. }
  73. void msg_stat(int msgid,struct msqid_ds msg_info)
  74. {
  75. int reval;
  76. sleep(1);
  77. reval=msgctl(msgid,IPC_STAT,&msg_info);
  78. if (reval==-1)
  79. {
  80. printf("get msg info error!\n");
  81. return;
  82. }
  83. printf("\n");
  84. printf("current number of bytes on queue is %ld \n",msg_info.msg_cbytes);
  85. printf("number of messages in the queue is %ld \n",msg_info.msg_qnum);
  86. printf("max number of bytes on queue id %ld \n",msg_info.msg_qbytes);
  87. /************************************************************************/
  88. /* 每个消息队列是容量(字节数)都有限制MSGMNB,值的大小因系统而异
  89. 在创建新的消息队列时,msg_qtytes的默认值就是MSHMNB
  90. /************************************************************************/
  91. printf("pid of last msgsnd is %ld\n",msg_info.msg_lspid);
  92. printf("pid of last msgrcv is %ld \n",msg_info.msg_lrpid);
  93. printf("last msgcnd time is %s \n",ctime(&(msg_info.msg_stime)));
  94. printf("last msgrcv time is %s \n",ctime(&(msg_info.msg_rtime)));
  95. printf("last change time is %s \n",ctime(&(msg_info.msg_ctime)));
  96. printf("msg uid is %ld \n",msg_info.msg_perm.uid);
  97. printf("msg gid is %ld \n",msg_info.msg_perm.gid);
  98. }

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702496

Linux消息队列应用的更多相关文章

  1. linux消息队列编程实例

    转自:linux 消息队列实例 前言: 消息队列就是一个消息的链表.可以把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程可以向其中按照一定的规则添加新消息:对消息队列有读权 ...

  2. LINUX消息队列实战之一

    前言 能说能抄能论皆不算,能写能打才是真功夫. 唠叨 反正我也是一个孤独的程序猿,多说一些奇奇怪怪的唠叨也无妨,第一次写消息队列,书本的东西和实战很不同,根据实战总结的一些注意事项会和大家分享,也敲打 ...

  3. linux 消息队列的限制

    消息队列的系统限制 作者:冯老师,华清远见嵌入式学院讲师. 消息队列是System V的IPC对象的一种,用于进程间通信,会受到系统的限制,本文主要描述了三个限制.第一:议个消息的最大长度:第二:消息 ...

  4. linux消息队列通信

    IPC机制 进程间通信机制(Inter Process Communication,IPC),这些IPC机制的存在使UNIX在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的 ...

  5. linux消息队列操作

    对消息队列的操作无非有以下三种类型: 1. 打开或创建消息队列消息队列的内核持续性要求每一个消息队列都在系统范围内相应唯一的键值,所以,要获得一个消息队列的描写叙述字,仅仅需提供该消息队列的键值就可以 ...

  6. linux消息队列的使用

    消息队列 *消息队列是内核地址空间中的内部链表,通过内核在各个进程之间传递的内容.消息顺序发送到消息队列中,每个消息队列都有IPC标识符唯一地进行标识. msgbuf结构 struct msgbuf{ ...

  7. Linux消息队列

    #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/ms ...

  8. Linux 消息队列编程

    消息队列.信号量以及共享内存被称作 XSI IPC,它们均来自system V的IPC功能,因此具有许多共性. 键和标识符: 内核中的每一种IPC结构(比如信号量.消息队列.共享内存)都用一个非负整数 ...

  9. linux 消息队列

    消息队列,这个可是鼎鼎大名,经常在某些地方看见大家那个膜拜,那个,嗯,那个... 那就给个完整的例子,大家欣赏就行,我一直认为不用那个,嗯@ 这个队列的最大作用就是进程间通信,你要非搞个持久化,那也行 ...

随机推荐

  1. MySQL 配置优化

    1.   连接请求的变量: A.max_connections 如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量, 当然这建立在机器能支撑的情况下,因为如果连接数越多,介于MySQL ...

  2. 自定义UITabBar的两种方式

    开发中,经常会遇到各种各样的奇葩设计要求,因为apple提供的UITabBar样式单一,只是简单的"图片+文字"样式,高度49又不可以改变.自定义UITabBar成为了唯一的出路. ...

  3. C++ 合成默认构造函数的真相

    对于C++默认构造函数,我曾经有两点误解: 类如果没有定义任何的构造函数,那么编译器(一定会!)将为类定义一个合成的默认构造函数. 合成默认构造函数会初始化类中所有的数据成员. 第一个误解来自于我学习 ...

  4. bzoj1396: 识别子串

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  5. UOJ262 【NOIP2016】换教室

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  6. ecshop /goods.php SQL Injection Vul

    catalogue . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述2. 漏洞触发条件 0x1: poc http://localhost ...

  7. linux: shell常用指令归纳

    1.软件安装方式: 1)源码安装: ~ wget xxxxxx ~ ./configure ~ make ~ make install 2) yum: ~ yum search : 查找软件包 ~ y ...

  8. PHP函数-检查某个值是否存在于数组中

    函数:in_array -- 检查数组中是否存在某个值定义:bool in_array ( mixed needle, array haystack [, bool strict] )在haystac ...

  9. HD1556Color the ball(树状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. 网站缓存数据到tomcat服务器

    通过缓存使相同的数据不用重复加载,降低数据库的访问 public class CacheFilter implements Filter { //实例变量[每线程共享] private Map< ...