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. 用 unoconv 将 xls 转换成 csv

    在 Linux 下,用 unoconv 将 xls 转换成 csv. unoconv -f csv -v input.xlsx

  2. glyphicon halflings regular ttf 报错

    一个web项目 用了bootstrap chrome开f12报错提示glyphicon halflings regular ttf找不到 为什么找不到,肯定又是path出了问题 找到bootstrap ...

  3. Linux安装后的基本配置

    1.换源 http://mirrors.zju.edu.cn http://mirrors.aliyun.com http://mirrors.ustc.edu.cn ubuntu替换/etc/apt ...

  4. 【BZOJ-4668】冷战 并查集 + 按秩合并 + 乱搞

    4668: 冷战 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 37  Solved: 24[Submit][Status][Discuss] Des ...

  5. 【poj2774】 Long Long Message

    http://poj.org/problem?id=2774 (题目链接) 题意 给出两个只包含小写字母的字符串,求出最长连续公共子串. solution 第一次用后缀数组,感觉有点神...才发现原来 ...

  6. 在打开vs解决方案时,怎样让所以打开的项目自动折叠

    使用VS 2010中的扩展性,搜PowerCommands,PowerCommands扩展在Visual Studio 2010中添加了数十个有用的的命令, Collapse Projects(折叠项 ...

  7. Spring BeanUtils的用法

    package test; import java.util.Date; import org.springframework.beans.BeanUtils; import test.basic.B ...

  8. 64位CentOS源码编译方式安装wine

    说明:本文仅作本人笔记的之用,仅供参考.可能因不同环境而不同. 1. 从官网下载最新版的wine-1.6.2.tar.gz 2. 安装相关的包(这里是我安装的,可能由于不同系统已经安装的包不同而不一样 ...

  9. 编写 unix和 windows的 Scala 脚本

    编写 unix和 windows的 Scala 脚本 今天在看<Scala 编程>的时候看到附录了,里面提到了怎么在 unix 和 windows 下面编写 scala 脚本. 之前我也一 ...

  10. scala break & continue

    Scala没有提供break和continue,我们可以自己实现一个,参考例子: import util.control.Breaks._ object BreakDemo { def main(ar ...