/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. XML的序列化与反序列化

    开发时会把数据持久化成xml格式,当然可以用xmlwriter来实现,不过感觉不方便,而且很繁琐.推荐使用直接序列化.反序列化对象的方法来处理. 直接上代码: public static class ...

  2. Atitit.ALT+TAB没反应车and 点击任务栏程序闪烁可是不能切换

    Atitit.ALT+TAB没反应车and 点击任务栏程序闪烁可是不能切换 1. 可能你的Alt+Tab键被别人禁用了,试下以下的方法: 1 2. 为什么要禁用Alt+Tab 1 3. ALT+TAB ...

  3. 数据结构--画画--最小生成树(Prim算法)

    通信网络的最小生成树配置,它是使右侧的生成树值并最小化.经常使用Prim和Kruskal算法.看Prim算法:以防万一N={V,{E}}它是在通信网络,TE它是N设置边的最小生成树.从算法U={u0} ...

  4. 【android】WebView缓存数据收集

    Android WebView 缓存 Android高手进阶教程(二十四)之---Android WebView的缓存!!! Android webView 缓存 Cache + HTML5离线功能 ...

  5. ANDROID定义自己的观点——模仿瀑布布局(源代码)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 简单介绍: 在自己定义view的时候,事实上非常easy,仅仅须要知道3步骤: 1.測量- ...

  6. 远程控制编写之屏幕传输 MFC实现 屏幕截图 发送bmp数据 显示bmp图像

    远程控制编写之屏幕传输  MFC实现  屏幕截图 发送bmp数据 显示bmp图像: 一 : 首先要了解bmp图像的结构 详情请看我转载的一篇文章http://blog.csdn.net/hnust_x ...

  7. CreateFont详细解释

    CFont * f;    f = new CFont;    f->CreateFont(10, // nHeight         0, // nWidth         0, // n ...

  8. Android项目--浅析系统通讯录中的那些方法

    系统通讯录,以前的版本虽然过时了,不过有些东西还是可以用. 1.开启系统联系人添加 /** 添加联系人 */ Intent intent = new Intent(Intent.ACTION_INSE ...

  9. Android 5.0之应用中实现材料设计—Material Design

    上午的时候在刷Google+,看到了Abraham Williams转发了一篇强文,是Android Developers网站新发的一篇博客—Implementing Material Design ...

  10. select省市联动选择城市 asp.net mvc4

    本文在 http://www.cnblogs.com/darrenji/p/3606703.html(感谢博主的分享)基础上加入全国各省市,从文件中读取全国省市县,组成省市联动的选择标签 在Model ...