/author:DriverMonkey
//phone:13410905075

//mail:bookworepeng@Hotmail.com

//qq:196568501

#include <pthread.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

#include <string.h>

#include <iostream>

#define MAX_SEND_SIZE 80

#define RETURN_MSG_TYPE 0XAA

#define SEND_MSG_TYPE 0X55

using namespace std;

struct mymsgbuf {

long mtype;

char mtext[MAX_SEND_SIZE];

};

static void *thread_GUI(void *arg);

static void *thread_logic(void *arg);

static int init_message(unsigned char key);

static void send_message(int qid,

                            struct mymsgbuf *qbuf,

                            long type,

                            const void *text,

                            int size);

static int read_message(int qid, struct mymsgbuf *qbuf, long type);

static void remove_queue(int qid);

static int message_id = 0;

int main ()

{

    pthread_t thread_GUI_id = 0;

    pthread_t thread_logic_id = 0;

message_id = init_message('g');

    

    pthread_create (&thread_GUI_id, NULL, &thread_GUI, NULL);

    pthread_create (&thread_logic_id, NULL, &thread_logic, NULL);

pthread_join (thread_GUI_id, NULL);

    pthread_join (thread_logic_id, NULL);

 

    return 0;

}

static void *thread_GUI(void *arg)

{

    int sleep_count = 0;

    mymsgbuf send_buf;

    

    sleep_count = 10;

    char send_v = 0;

while(sleep_count--)

    {

        send_v++;

        send_message(message_id, &send_buf , SEND_MSG_TYPE, &send_v,sizeof(send_v));

        //cout<<"thead_GUI: sleep_count = "<<sleep_count<<endl;

        //sleep(1);

    }

}

static void *thread_logic(void *arg)

{

    int sleep_count = 0;

    mymsgbuf recive_buf;

sleep_count = 10;

    while(sleep_count--)

    {

        //cout<<"thread_logic: sleep_count = "<<sleep_count<<endl;

        read_message(message_id,&recive_buf, SEND_MSG_TYPE);

        //sleep(1);

    }

}

int init_message(unsigned char key)

{

    int id = 0;

    

    key = ftok(".", key);

id = msgget(key, IPC_CREAT|0777);

    if(id == (-1))

        while(1);// should never in

        

    return id;

}

void send_message(int qid,

                            struct mymsgbuf *qbuf,

                            long type,

                            const void *text,

                            int size)

{

    qbuf->mtype = type;

    memcpy(qbuf->mtext, text,size);

    cout<<"send = " <<(int)qbuf->mtext[0]<<endl;

    if((msgsnd(qid, (struct msgbuf *)qbuf,size,NULL) == -1))

        while(1);//shoud never in

qbuf->mtype = type;

    msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, RETURN_MSG_TYPE, 0);

    cout<<"send return= " << (int)qbuf->mtext[0]<<endl;

    cout<<qbuf->mtext<<endl;

}

int read_message(int qid, struct mymsgbuf *qbuf, long type)

{

    int read_size = 0;

    static int temp = 100;

    

    qbuf->mtype = type;

    temp++;

    read_size = msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);

    cout<<"read = " << (int)qbuf->mtext[0]<<endl;

    char const *return_message = "message_ok";

    strcpy(qbuf->mtext, return_message);

    

    qbuf->mtext[0] = temp++;

    qbuf->mtype = RETURN_MSG_TYPE;

    msgsnd(qid, (struct msgbuf *)qbuf,strlen(return_message)+1,NULL);

    cout<<"read return ="<<(int)qbuf->mtext[0]<<endl;

}

void remove_queue(int qid)

{

}

linux 消息队列例子的更多相关文章

  1. linux消息队列编程实例

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

  2. LINUX消息队列实战之一

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

  3. linux 消息队列的限制

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

  4. linux 消息队列

    消息队列,这个可是鼎鼎大名,经常在某些地方看见大家那个膜拜,那个,嗯,那个... 那就给个完整的例子,大家欣赏就行,我一直认为不用那个,嗯@ 这个队列的最大作用就是进程间通信,你要非搞个持久化,那也行 ...

  5. linux消息队列通信

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

  6. Linux消息队列应用

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

  7. linux消息队列操作

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

  8. linux消息队列的使用

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

  9. Linux消息队列

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

随机推荐

  1. Windows7下搭建Django运行环境

    一直都是在Linux环境下搭建django的运行环境,开学因为需要叫前端的同学帮忙修改模板,所以需要在Windows下搭建起运行环境,想来PHP倒是有不少集成开发环境,Python倒是少的可怜…只在w ...

  2. 快速构建Windows 8风格应用3-打包发布应用

    原文:快速构建Windows 8风格应用3-打包发布应用 本篇博文主要介绍如何打包应用程序,成功后如何部署应用程序,最后介绍如何发布应用程序到应用商店中. 如何打包Windows 8风格应用程序呢? ...

  3. Mybatis之动态构建SQL语句

    今天一个新同事问我,我知道如何利用XML的方式来构建动态SQL,可是Mybatis是否能够利用注解完成动态SQL的构建呢?!!答案是肯定的,MyBatis 提供了注解,@InsertProvider, ...

  4. javascript实现代码高亮

    javascript实现代码高亮-wangHighLighter.js 1. 引言 (先贴出wangHighLighter.js的github地址:https://github.com/wangfup ...

  5. Spring IOC 之ApplicationContext的其他功能

    正如上面章节所介绍的那样, org.springframework.beans.factory 包提供了管理和操作beans的 基本功能. org.springframework.context包增加 ...

  6. leetcode第18题--Letter Combinations of a Phone Number

    Problem: Given a digit string, return all possible letter combinations that the number could represe ...

  7. 如何使用C API来操作UCI

    https://forum.openwrt.org/viewtopic.php?pid=183335#p183335 Compiling UCI as stand alone with an exam ...

  8. JAVA开发语言基础

    很多时候我们都不知道,为什么android最开始要用java作为基础语言 看看知乎上都是怎么回答的 为什么java最初成为android开发的基础语言 ------------------------ ...

  9. gradle测试与线上打包

    首先,第一反应理所当然的是profile : <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  10. ant svn

    <!-- 检出代码 这里使用 export 不是checkout 二者区别 checkout会svn相关信息文件检出,export只是检出最新的文件--> <target name= ...