消息队列,这个可是鼎鼎大名,经常在某些地方看见大家那个膜拜,那个,嗯,那个。。。

那就给个完整的例子,大家欣赏就行,我一直认为不用那个,嗯@

这个队列的最大作用就是进程间通信,你要非搞个持久化,那也行,随你高兴喽!

——————————————————————————————————————

// 进程间通信,通过消息队列

//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 消息队列的更多相关文章

  1. linux消息队列编程实例

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

  2. LINUX消息队列实战之一

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

  3. linux 消息队列的限制

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

  4. linux消息队列通信

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

  5. Linux消息队列应用

    #include"sys/types.h" #include "sys/msg.h" #include "unistd.h" #includ ...

  6. linux消息队列操作

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

  7. linux消息队列的使用

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

  8. Linux消息队列

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

  9. Linux 消息队列编程

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

随机推荐

  1. python---二叉树遍历

    重学. # coding = utf-8 # 二叉树遍历 class Node: """节点类""" def __init__(self, ...

  2. [转] 从零构建 vue2 + vue-router + vuex 开发环境到入门,实现基本的登录退出功能

    这是一个创建于 738 天前的主题,其中的信息可能已经有所发展或是发生改变. 前言 vue2 正式版已经发布将近一个月了, 国庆过后就用在了公司的两个正式项目上, 还有一个项目下个月也会采用 vue2 ...

  3. codeforces 502 g The Tree

    题解: 一道优秀的题目 有几种做法: 1.维护后缀和 刚开始我想的是维护前缀和 然后用$sum[x]-sum[y]>=dep[x]-dep[y]$来做 但是这样子树赋值为0这个操作就很难进行了 ...

  4. Servlet(三):生命周期、常用方法、常见错误

    Servlet的生命周期:从第一次调用,到服务器关闭.如果在web.xml 中配置了load-on-startup则是从服务器开启到服务器关闭. 注意: * init方法是对Servlet进行初始化的 ...

  5. redis初步入门(1)

    一.redis是一款高性能NOSQL系列的非关系型的数据库,其是用C语言开发的一个开源高性能键值对(key-value)数据库. 二.redis的应用场景 1.缓存(数据查询.短连接.新闻内容.商品内 ...

  6. C# .net 中 Timeout 的处理及遇到的问题

    C# 中 Timeout 的处理 前言 最近在项目中要实现一个功能,是关于 Timeout 的,主要是要在要在 TCP 连接建立的时间 和 整个请求完成的时间,在这两个时间层面上,如果超出了设置的时间 ...

  7. 根据文件大小自动判断单位B,KB,MB,GB

    <php> /** * 文件大小格式化 * @param integer $size 初始文件大小,单位为byte * @return array 格式化后的文件大小和单位数组,单位为by ...

  8. jquery提示sucess

    这是学习笔记. 今天做东西的时候,想把体验做好,于是打算再ajax success字段中添加函数实现提示sucess. 用了jquery的fadeIn 跟fadeOut,再fadeIn的callbac ...

  9. Vue使用中常见问题

    1.安装sass时报未找到 1.原因应该同时安装:1.npm install --save-dev sass-loader    2.npm install --save-dev node-sass ...

  10. Java当中的异常2

    1.throw的作用 如果一行有可能代码抛出Execption对象或者check exception 就必须对这行代码进行处理 2.throws的作用 Throws表明这个类或者方法可能会产生一个指定 ...