您的位置:首页 > 运维架构 > Linux

linux 消息队列例子

2013-07-21 23:49 357 查看
/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)

{

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: