Linux进程间通信--消息队列

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

源文件:

#include
#include
#include
#include
#include
#include

#include "msg_queue.h"


#define QUEUE_KEY "./"

//获取消息队列的ID
static int get_msg_id(int queue)
{
    int key = ftok(QUEUE_KEY, queue);
    int msgid = msgget(key, IPC_CREAT|0777);
    if(msgid < 0)
    {
        perror("msgget error:");
        return -1;
    }
    return msgid;    
}

//将消息发送到指定的队列中
int send_msg(int queue, char* msg, int msg_len)
{
    int msgid = get_msg_id(queue);
    if(msgid < 0)
    {
        perror("get_msg_id error:");
        return -1;
    }

    //printf("msgid = %d__\n",msgid);

    struct msgbuf send_buf;
    memset(&send_buf, 0x00, sizeof(send_buf));
    send_buf.mtype = queue;
    memcpy(&send_buf.mtext, msg, msg_len);
        
    if(msgsnd(msgid, &send_buf, msg_len, IPC_NOWAIT) < 0)
    {
        perror("msgsnd error");
        return -1;
    }
    return 0;
}

//阻塞接收指定的消息队列里的消息
int recv_msg(int queue, char* msg, int* msg_len)
{
    int msgid = get_msg_id(queue);
    if(msgid < 0)
    {
        perror("get_msg_id error:");
        return -1;
    }

    struct msgbuf recv_buf;
    int recv_len;
//    recv_len = msgrcv(msgid,(void*)&recv_buf, sizeof(recv_buf), 0, IPC_NOWAIT|MSG_NOERROR);
    recv_len = msgrcv(msgid,(void*)&recv_buf, sizeof(recv_buf), 0, MSG_NOERROR);
    if(recv_len < 0)
    {
        perror("msgrcv error:");
        return -1;
    }

    *msg_len = recv_len;
    memcpy(msg, recv_buf.mtext, recv_len);
    
    return 0;    
}

//查询指定消息队列里有多少消息
int get_msg_qnum(int queue, long int* msg_qnum)
{
    struct msqid_ds buf;
    int msgid = get_msg_id(queue);
    if(msgid < 0)
    {
        perror("get_msg_id error:");
        return -1;
    }
    
    //printf("msgid = %d\n",msgid);

    int ret = msgctl(msgid, IPC_STAT,&buf);//获取消息队列信息
    if(ret < 0)
    {
        perror("msgctl error:");
        return -1;
    }

    //printf("msg_qnum = %d\n",buf.msg_qnum);
    
    *msg_qnum = buf.msg_qnum;
    return 0;
}
 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

头文件

#ifndef _MSG_QUEUE_H
#define _MSG_QUEUE_H

#define MAIN_QUEUE  'a'
#define QT_QUEUE    'b'
#define SIG_QUEUE   'c'
#define MSG_MAX_LENGTH 1024

struct msgbuf{
    long mtype;
    char mtext[MSG_MAX_LENGTH];
}; 

int send_msg(int queue, char* msg, int msg_len);
int recv_msg(int queue, char* msg, int* msg_len);
int get_msg_qnum(int queue, long int* msg_qnum);

#endif //_MSG_QUEUE_H
 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

测试代码

#include "msg_queue.h"
#include
#include

#define QUEUE_TYPE SIG_QUEUE

int main(void)
{
    char queue_buf[] = {MAIN_QUEUE, QT_QUEUE, SIG_QUEUE};
    int queue_element_count = 3;//sizeof(queue_buf)/sizeof(char);
    char recv_buf[MSG_MAX_LENGTH];
    char send_buf[MSG_MAX_LENGTH];
    int msg_len;

    int i,j;
    //向指定的三个队列里每个队列发送10条消息
    for(i = 0; i < queue_element_count; i++)
    {
        for(j = 0; j < 10; j++)
        {
            sprintf(send_buf, "hello world%d_%d",i,j);
            send_msg(queue_buf[i],send_buf,strlen(send_buf)+1);   
        }
    }

    //查询三个队列里有多少个消息,并将所有消息都取出
    long int msg_qnum;
    for(i = 0; i < queue_element_count; i++)
    {
        get_msg_qnum(queue_buf[i],&msg_qnum);
        printf("\n\n***********queue type %c %ld***********\n",queue_buf[i],msg_qnum);
        for(j = 0; j < msg_qnum; j++)
        //for(j = 0; j < 11; j++)
        {
            recv_msg(queue_buf[i], recv_buf,&msg_len);
            printf("msg:%s %d\n",recv_buf, msg_len);
        }
    }
#if 0
    get_msg_qnum(QUEUE_TYPE,&msg_qnum);
    printf("msg_qnum = %ld\n",msg_qnum);
    for(;msg_qnum > 0; msg_qnum--)
    {
        recv_msg(QUEUE_TYPE, recv_buf, &msg_len);
        printf("msg:%s %d\n",recv_buf, msg_len);
    }
    get_msg_qnum(QUEUE_TYPE,&msg_qnum);
    printf("msg_qnum = %ld\n",msg_qnum);

#endif
    return 0;
}
 

你可能感兴趣的:(Linux系统编程)