c语言-消息队列

一、消息队列的介绍

    消息队列的实现原理是将消息存储在一个队列中,生产者将消息放入队列的尾部,消费者从队列的头部取出消息进行处理。消息队列通常采用先进先出(FIFO)的方式进行消息的存储和处理。消息队列可以实现异步通信,提高系统的可靠性和可扩展性。

具体实现上,消息队列通常由以下几个组件构成:

消息队列:用于存储消息的队列。

生产者:向消息队列中添加消息的组件。

消费者:从消息队列中取出消息并进行处理的组件。

二、消息队列的测试

手写一个简单的消息队列进行测试。功能比较简单,发送5条数据,读取5条数据。

#include <stdio.h>
#include <stdlib.h>


#define MAXNUM 10
struct myBuf
{
    char *buf;
    int len;
};
struct test
{
    int head;
    int tail;
    struct myBuf idT[MAXNUM];
};
struct test testFifo;
int testFifoInit(void)
{
    int i;
    testFifo.head=0;
    testFifo.tail=0;
    for(i=0;i<MAXNUM;i++)
    {
        testFifo.idT[i].buf=(char *)malloc(100);
        if(testFifo.idT[i].buf == NULL)
            return -1;
         testFifo.idT[i].len=0;


    }
    return 0;
}
int testFifoClose(void)
{
    int i;
    for(i=0;i<MAXNUM;i++)
    {
        if(testFifo.idT[i].buf == NULL)
            continue;
        free(testFifo.idT[i].buf);


    }
    return 0;
}
int testFifoIsEmpty(void)
{
    //加锁
    if(testFifo.head == testFifo.tail )
        return -1;
    else
        return 0;


}
int testFifoIsFull(void)
{
    //加锁
    if((testFifo.head % MAXNUM) != ((testFifo.tail+1)% MAXNUM) )
        return 0;
    else
        return -1;


}
int testFifoPut(char *buf,int len)
{
    //加锁
    if(testFifoIsFull() == -1)
        return -1;
    memset(testFifo.idT[testFifo.tail].buf,0,len);
    memcpy(testFifo.idT[testFifo.tail].buf,buf,len);
    testFifo.idT[testFifo.tail].len=len;
    testFifo.tail = (testFifo.tail+1)%MAXNUM;


}
int testFifoGet(char *buf)
{
    //加锁
    if(testFifoIsEmpty() == -1)
        return -1;
    memcpy(buf,testFifo.idT[testFifo.head].buf,testFifo.idT[testFifo.head].len);
    testFifo.head = (testFifo.head+1)%MAXNUM;
    return 0;


}




struct test testTemp;
int main(int argv,char *argc[])
{
    char bufSend[20]="hello world";
    char bufRx[20]={0};
    int i=0;
    if(testFifoInit()== -1)
    {
        printf("创建失败\n");
    }
    while(1)
    {
        sprintf(bufSend,"hello world %d",i++);
        printf("send=%s\n",bufSend);
       testFifoPut(bufSend,strlen(bufSend));
        if(i%5 == 0)
        {
            while(1)
            {
                    if(testFifoGet(bufRx) == -1)
                        break;
                    printf("rx=%s\n",bufRx);
                    memset(bufRx,0,sizeof(bufRx));
            }
        }
        sleep(2);


    }
    testFifoClose();
    return 0;
}

5e562f7865e6548fb2d6a610c7f5cb63.png

欢迎关注公众号:嵌入式学习与实践

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,可以使用系统提供的消息队列函数集来创建消息队列。以下是一个简单的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #define MAX_MSG_SIZE 1024 struct message { long msg_type; char msg_data[MAX_MSG_SIZE]; }; int main(int argc, char* argv[]) { int msgid; key_t key; struct message msg; // 生成一个唯一的key if ((key = ftok(".", 'a')) == -1) { perror("ftok"); exit(EXIT_FAILURE); } // 创建消息队列 if ((msgid = msgget(key, 0666 | IPC_CREAT)) == -1) { perror("msgget"); exit(EXIT_FAILURE); } // 发送消息 msg.msg_type = 1; strcpy(msg.msg_data, "Hello, world!"); if (msgsnd(msgid, &msg, sizeof(msg), 0) == -1) { perror("msgsnd"); exit(EXIT_FAILURE); } // 接收消息 if (msgrcv(msgid, &msg, sizeof(msg), 0, 0) == -1) { perror("msgrcv"); exit(EXIT_FAILURE); } printf("Received message: %s\n", msg.msg_data); // 删除消息队列 if (msgctl(msgid, IPC_RMID, NULL) == -1) { perror("msgctl"); exit(EXIT_FAILURE); } return 0; } ``` 在上述代码中,首先使用 `ftok` 函数生成一个唯一的key,然后使用 `msgget` 函数创建消息队列。发送消息使用 `msgsnd` 函数,接收消息使用 `msgrcv` 函数。最后使用 `msgctl` 函数删除消息队列。 在使用消息队列时,需要注意以下几个问题: - 消息队列的key需要保证唯一性; - 发送和接收的消息结构体需要一致; - 发送和接收时需要指定消息类型; - 消息队列的大小是有限制的,需要根据实际需求设置合适的大小; - 需要注意消息队列的并发访问问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值