linux 消息队列
消息队列,这个可是鼎鼎大名,经常在某些地方看见大家那个膜拜,那个,嗯,那个。。。
那就给个完整的例子,大家欣赏就行,我一直认为不用那个,嗯@
这个队列的最大作用就是进程间通信,你要非搞个持久化,那也行,随你高兴喽!
——————————————————————————————————————
// 进程间通信,通过消息队列
//msgqueue-server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/msg.h>
#include <signal.h>
#define MSG_FILE "/etc/passwd"
struct msg_form
{
long mtype;
char mtext[256];
};
static sig_atomic_t run =1;
void sigint(int signum)
{
if (SIGINT==signum)
{
run = 0;
}
}
int main()
{
signal(SIGINT, sigint);
int msqid;
key_t key;
struct msg_form msg;
int len;
if (0>(key=ftok(MSG_FILE, 'z')))
{
perror("ftok error!\n");
exit(1);
}
printf("MSG -server key is %0x\n", key);
if (-1==(msqid=msgget(key, IPC_CREAT|0777)))
{
perror("msgget error!\n");
exit(1);
}
printf("msqid is %0x\n", msqid);
printf("pid is %0x\n", getpid());
while (run)
{
len = msgrcv(msqid, &msg, 256, 888, IPC_NOWAIT);
if (0<len)
{
printf("MSG -server receive msg type is %0x\n", (unsigned int)msg.mtype);
printf("MSG -server receive msg is %s\n", msg.mtext);
msg.mtype = 999;
sprintf(msg.mtext, "Hello, I'm server %0x\n", getpid());
msgsnd(msqid, &msg, sizeof(msg.mtext), 0);
}
}
printf("MSG -server quit!\n");
if (-1==msgctl(msqid, IPC_RMID, 0))
{
printf("msqid %0x is rmed with error %d %s\n", msqid, errno, strerror(errno));
exit(1);
}
return 0;
}
// msgqueue-client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/msg.h>
#include <signal.h>
#define MSG_FILE "/etc/passwd"
struct msg_form
{
long mtype;
char mtext[256];
};
int main()
{
int msqid;
key_t key;
struct msg_form msg;
int isnd, len;
if (0>(key=ftok(MSG_FILE, 'z')))
{
perror("ftok error!");
exit(1);
}
printf("MSG -client key is %0x\n", key);
if (-1==(msqid=msgget(key, 0777)))
{
perror("msgget error");
exit(1);
}
printf("msqid is %0x\n", msqid);
printf("pid is %0x\n", getpid());
msg.mtype = 888;
sprintf(msg.mtext, "Hello, I'm client %0x\n", getpid());
isnd = msgsnd(msqid, &msg, sizeof(msg.mtext), IPC_NOWAIT);
if (0==isnd)
{
len = msgrcv(msqid, &msg, 256, 999, 0);
if (0<len)
{
printf("MSG -client receive msg type is %0x\n", (unsigned int)msg.mtype);
printf("MSG -client receive msg is %s\n", msg.mtext);
}
}
return 0;
}
// result
# ./msgqueue-server &
[1] 2588
MSG -server key is 7a011886
msqid is 28000
pid is a1c
# ./msgqueue-client &
[2] 2592
MSG -client key is 7a011886
msqid is 28000
pid is a20
MSG -server receive msg type is 378
MSG -server receive msg is Hello, I'm client a20
MSG -client receive msg type is 3e7
MSG -client receive msg is Hello, I'm server a1c
[2]+ Done ./msgqueue-client
e# jobs
[1]- Running ./msgqueue-server &
# fg 1
./msgqueue-server
^CMSG -server quit!
Finally:
用好了,还是挺强大的啊
linux 消息队列的更多相关文章
- linux消息队列编程实例
转自:linux 消息队列实例 前言: 消息队列就是一个消息的链表.可以把消息看作一个记录,具有特定的格式以及特定的优先级.对消息队列有写权限的进程可以向其中按照一定的规则添加新消息:对消息队列有读权 ...
- LINUX消息队列实战之一
前言 能说能抄能论皆不算,能写能打才是真功夫. 唠叨 反正我也是一个孤独的程序猿,多说一些奇奇怪怪的唠叨也无妨,第一次写消息队列,书本的东西和实战很不同,根据实战总结的一些注意事项会和大家分享,也敲打 ...
- linux 消息队列的限制
消息队列的系统限制 作者:冯老师,华清远见嵌入式学院讲师. 消息队列是System V的IPC对象的一种,用于进程间通信,会受到系统的限制,本文主要描述了三个限制.第一:议个消息的最大长度:第二:消息 ...
- linux消息队列通信
IPC机制 进程间通信机制(Inter Process Communication,IPC),这些IPC机制的存在使UNIX在进程通信领域手段相当丰富,也使得程序员在开发一个由多个进程协作的任务组成的 ...
- Linux消息队列应用
#include"sys/types.h" #include "sys/msg.h" #include "unistd.h" #includ ...
- 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结构(比如信号量.消息队列.共享内存)都用一个非负整数 ...
随机推荐
- kubenetes 环境的塔建
最近听我朋友说他们公司准备上云,全线把服务迁到 k8s 上面,一下感觉,我们就 lower 了不少,之前服务器一直跑的就是 docker ,想想弄到 k8s 应该还是没有啥,于是我们也开始改造了 参考 ...
- Python中防止sql注入的方法详解
SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库.下面这篇文章主要给大家介绍了关于Python中 ...
- JavaScript之扑朔迷离的this
JavaScript这门语言中,最令人迷惑的地方有三个,闭包.this.原型.针对大多数人,可以利用词法作用域等避开this的坑,但是我们不能一直生活在舒适区,要敢于打破砂锅问到底,对我们来说也是一种 ...
- Android代码编译环境配置 “Gerrit和Git环境配置”
Gerrit和Git环境配置可以参考<git&gerrit操作指导> 步骤1. 先在Gerrit中创建新的账户: 步骤2. 在新的客户端上生成密钥(可以使用的是生成的公钥): 步骤 ...
- C#创建ActiveX
因为最近的项目可能会用到调用外部设备,读取信息.为了和现有的BS系统兼容,并以较小的代价满足需求,于是想到了使用ActiveX技术(也有人建议使用Silverlight),这技术虽然比较早了,但还是能 ...
- VS2015编译FFMPEG,修改FFmpeg缓冲区大小解决实时流解码丢包问题,FFmpeg错误rtsp流地址卡死的问题,设置超时
之前尝试过很多网上利用Windows编译FFmpeg的文章,都没有办法编译X64位的FFmpeg,有些教程中有专门提到编译64位的FFmpeg需要下载mingw-w64-install,但是编译的过程 ...
- HTML(五)
HTML5新结构标签 h5新增的主要语义化标签如下: 1.header 页面头部.页眉2.nav 页面导航3.article 一篇文章4.section 文章中的章节5.aside 侧边栏6.foot ...
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习5
#include <iostream>using namespace std;const MAXSIZE=12;const year=3;int main(){ char *month[M ...
- [LeetCode] Domino and Tromino Tiling 多米诺和三格骨牌
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may ...
- Docker ElK安装部署使用教程
一.简介 1.核心组成 ELK由Elasticsearch.Logstash和Kibana三部分组件组成: Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引 ...