zoukankan      html  css  js  c++  java
  • linux 消息队列例子

    /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)
    {

    }

  • 相关阅读:
    Python中的unittest和logging
    android短彩信附件机制
    Android Mms之:深入MMS支持
    Android 源码阅读之SMS,MMS
    深度分析:Android4.3下MMS发送到附件为音频文件(音频为系统内置音频)的彩信给自己,添加音频-发送彩信-接收彩信-下载音频附件-预览-播放(一,添加附件)
    使用adb命令对手机进行截屏保存到电脑,SDCard
    Android中检测字符编码(GB2312,ASCII,UTF8,UNICODE,TOTAL——ENCODINGS)方法(二)
    解决新建短信时,输入“+86”,然后输入联系人名字“1”,按删除键之后,联系人变为“1”,删除操作为达到预期结果
    解决:People下面选择分享可见联系人,选择多个联系人后通过短信分享,短信中只显示一个联系人
    解决:短信添加录音附件,录音,没有录音时间限制,超出彩信最大限制也正常录音
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3206400.html
Copyright © 2011-2022 走看看