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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值