Linux消息队列应用
- #include"sys/types.h"
- #include "sys/msg.h"
- #include "unistd.h"
- #include"stdio.h"
- void msg_stat(int,struct msqid_ds);
- int main()
- {
- int gflags,sflags,rflags;
- key_t key;
- int msgid;
- int reval;
- struct msgbuf{
- int mtype;
- char mtext[1];
- }msg_sbuf;
- struct msgmbuf{
- int mtype;char mtext[10];
- }msg_rbuf;
- struct msqid_ds msg_ginfo,msg_sinfo;
- char* msgpath="/UNIX/msgqueue";
- key=ftok(msgpath,'a');
- gflags=IPC_CREAT|IPC_EXCL;
- msgid=msgget(key,gflags|00666);
- if(msgid==-1){
- printf("msg create error!\n");
- return;
- }
- /*创建一个消息队列后,输出消息队列默认属性*/
- msg_stat(msgid,msg_ginfo);
- sflags=IPC_NOWAIT;
- msg_sbuf.mtype=10;
- msg_sbuf.mtext[0]='a';
- reval=msgsnd(msgid,&msg_sbuf,sizeof(msg_sbuf.mtext),sflags);
- if (reval==-1)
- {
- printf("message send error!\n");
- }
- /*发送一个消息后,输出消息队列属性*/
- msg_stat(msgid,msg_ginfo);
- rflags=IPC_NOWAIT|MSG_NOERROR;
- reval=msgrcv(msgid,&msg_rbuf,4,10,rflags);
- if (reval==-1)
- {
- printf("Read msg error!\n");
- }
- else
- printf("Read from msg queue %d bytes\n",reval);
- /*从消息队列中读出消息后,输出消息队列属性*/
- msg_stat(msgid,msg_ginfo);
- msg_sinfo.msg_perm.uid=8;
- msg_sinfo.msg_perm.gid=8;
- msg_sinfo.msg_qbytes=16388;
- /************************************************************************/
- /* 此处验证超级用户可以更改消息队列的默认msg_qbytes */
- //注意这里设置的值大于最大默认值
- /************************************************************************/
- reval=msgctl(msgid,IPC_SET,&msg_sinfo);
- if(reval==-1){
- printf("msg set info error!\n");
- return;
- }
- msg_stat(msgid,msg_ginfo);
- /************************************************************************/
- /* 验证设置消息队列属性 */
- /************************************************************************/
- reval=msgctl(msgid,IPC_RMID,NULL);//删除消息队列
- if (reval==-1)
- {
- printf("unlink msg queue error!\n");
- return;
- }
- }
- void msg_stat(int msgid,struct msqid_ds msg_info)
- {
- int reval;
- sleep(1);
- reval=msgctl(msgid,IPC_STAT,&msg_info);
- if (reval==-1)
- {
- printf("get msg info error!\n");
- return;
- }
- printf("\n");
- printf("current number of bytes on queue is %ld \n",msg_info.msg_cbytes);
- printf("number of messages in the queue is %ld \n",msg_info.msg_qnum);
- printf("max number of bytes on queue id %ld \n",msg_info.msg_qbytes);
- /************************************************************************/
- /* 每个消息队列是容量(字节数)都有限制MSGMNB,值的大小因系统而异
- 在创建新的消息队列时,msg_qtytes的默认值就是MSHMNB
- /************************************************************************/
- printf("pid of last msgsnd is %ld\n",msg_info.msg_lspid);
- printf("pid of last msgrcv is %ld \n",msg_info.msg_lrpid);
- printf("last msgcnd time is %s \n",ctime(&(msg_info.msg_stime)));
- printf("last msgrcv time is %s \n",ctime(&(msg_info.msg_rtime)));
- printf("last change time is %s \n",ctime(&(msg_info.msg_ctime)));
- printf("msg uid is %ld \n",msg_info.msg_perm.uid);
- printf("msg gid is %ld \n",msg_info.msg_perm.gid);
- }
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702496
Linux消息队列应用的更多相关文章
- linux消息队列编程实例
转自:linux 消息队列实例 前言: 消息队列就是一个消息的链表.可以把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程可以向其中按照一定的规则添加新消息:对消息队列有读权 ...
- LINUX消息队列实战之一
前言 能说能抄能论皆不算,能写能打才是真功夫. 唠叨 反正我也是一个孤独的程序猿,多说一些奇奇怪怪的唠叨也无妨,第一次写消息队列,书本的东西和实战很不同,根据实战总结的一些注意事项会和大家分享,也敲打 ...
- linux 消息队列的限制
消息队列的系统限制 作者:冯老师,华清远见嵌入式学院讲师. 消息队列是System V的IPC对象的一种,用于进程间通信,会受到系统的限制,本文主要描述了三个限制.第一:议个消息的最大长度:第二:消息 ...
- linux消息队列通信
IPC机制 进程间通信机制(Inter Process Communication,IPC),这些IPC机制的存在使UNIX在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的 ...
- linux消息队列操作
对消息队列的操作无非有以下三种类型: 1. 打开或创建消息队列消息队列的内核持续性要求每一个消息队列都在系统范围内相应唯一的键值,所以,要获得一个消息队列的描写叙述字,仅仅需提供该消息队列的键值就可以 ...
- linux消息队列的使用
消息队列 *消息队列是内核地址空间中的内部链表,通过内核在各个进程之间传递的内容.消息顺序发送到消息队列中,每个消息队列都有IPC标识符唯一地进行标识. msgbuf结构 struct msgbuf{ ...
- Linux消息队列
#include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/ms ...
- Linux 消息队列编程
消息队列.信号量以及共享内存被称作 XSI IPC,它们均来自system V的IPC功能,因此具有许多共性. 键和标识符: 内核中的每一种IPC结构(比如信号量.消息队列.共享内存)都用一个非负整数 ...
- linux 消息队列
消息队列,这个可是鼎鼎大名,经常在某些地方看见大家那个膜拜,那个,嗯,那个... 那就给个完整的例子,大家欣赏就行,我一直认为不用那个,嗯@ 这个队列的最大作用就是进程间通信,你要非搞个持久化,那也行 ...
随机推荐
- MySQL 配置优化
1. 连接请求的变量: A.max_connections 如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量, 当然这建立在机器能支撑的情况下,因为如果连接数越多,介于MySQL ...
- 自定义UITabBar的两种方式
开发中,经常会遇到各种各样的奇葩设计要求,因为apple提供的UITabBar样式单一,只是简单的"图片+文字"样式,高度49又不可以改变.自定义UITabBar成为了唯一的出路. ...
- C++ 合成默认构造函数的真相
对于C++默认构造函数,我曾经有两点误解: 类如果没有定义任何的构造函数,那么编译器(一定会!)将为类定义一个合成的默认构造函数. 合成默认构造函数会初始化类中所有的数据成员. 第一个误解来自于我学习 ...
- bzoj1396: 识别子串
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- UOJ262 【NOIP2016】换教室
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...
- ecshop /goods.php SQL Injection Vul
catalogue . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述2. 漏洞触发条件 0x1: poc http://localhost ...
- linux: shell常用指令归纳
1.软件安装方式: 1)源码安装: ~ wget xxxxxx ~ ./configure ~ make ~ make install 2) yum: ~ yum search : 查找软件包 ~ y ...
- PHP函数-检查某个值是否存在于数组中
函数:in_array -- 检查数组中是否存在某个值定义:bool in_array ( mixed needle, array haystack [, bool strict] )在haystac ...
- HD1556Color the ball(树状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 网站缓存数据到tomcat服务器
通过缓存使相同的数据不用重复加载,降低数据库的访问 public class CacheFilter implements Filter { //实例变量[每线程共享] private Map< ...