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

    }

  • 相关阅读:
    DELPHI 画报表 画表头 stringgrid控件
    蜂巢 Thinking in Agile 我们需要怎样的软件过程(1)
    小博一周年了 将开源进行到底
    Windows Mobile下实现图片的3D效果
    蜂巢 Thinking in Agile 我们需要怎样的软件过程(2)
    Windows 中各种 dll 的导出功能
    以下代码中的两个sizeof用法有问题吗?
    sizeof和strlen
    以下反向遍历array数组的方法有什么错误?
    找错题
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3206400.html
Copyright © 2020-2023  润新知