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 消息队列
消息队列,这个可是鼎鼎大名,经常在某些地方看见大家那个膜拜,那个,嗯,那个... 那就给个完整的例子,大家欣赏就行,我一直认为不用那个,嗯@ 这个队列的最大作用就是进程间通信,你要非搞个持久化,那也行 ...
随机推荐
- 用 unoconv 将 xls 转换成 csv
在 Linux 下,用 unoconv 将 xls 转换成 csv. unoconv -f csv -v input.xlsx
- glyphicon halflings regular ttf 报错
一个web项目 用了bootstrap chrome开f12报错提示glyphicon halflings regular ttf找不到 为什么找不到,肯定又是path出了问题 找到bootstrap ...
- Linux安装后的基本配置
1.换源 http://mirrors.zju.edu.cn http://mirrors.aliyun.com http://mirrors.ustc.edu.cn ubuntu替换/etc/apt ...
- 【BZOJ-4668】冷战 并查集 + 按秩合并 + 乱搞
4668: 冷战 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 37 Solved: 24[Submit][Status][Discuss] Des ...
- 【poj2774】 Long Long Message
http://poj.org/problem?id=2774 (题目链接) 题意 给出两个只包含小写字母的字符串,求出最长连续公共子串. solution 第一次用后缀数组,感觉有点神...才发现原来 ...
- 在打开vs解决方案时,怎样让所以打开的项目自动折叠
使用VS 2010中的扩展性,搜PowerCommands,PowerCommands扩展在Visual Studio 2010中添加了数十个有用的的命令, Collapse Projects(折叠项 ...
- Spring BeanUtils的用法
package test; import java.util.Date; import org.springframework.beans.BeanUtils; import test.basic.B ...
- 64位CentOS源码编译方式安装wine
说明:本文仅作本人笔记的之用,仅供参考.可能因不同环境而不同. 1. 从官网下载最新版的wine-1.6.2.tar.gz 2. 安装相关的包(这里是我安装的,可能由于不同系统已经安装的包不同而不一样 ...
- 编写 unix和 windows的 Scala 脚本
编写 unix和 windows的 Scala 脚本 今天在看<Scala 编程>的时候看到附录了,里面提到了怎么在 unix 和 windows 下面编写 scala 脚本. 之前我也一 ...
- scala break & continue
Scala没有提供break和continue,我们可以自己实现一个,参考例子: import util.control.Breaks._ object BreakDemo { def main(ar ...