- 浏览: 782112 次
- 性别:
- 来自: 深圳
-
文章分类
- 全部博客 (1045)
- 数据结构 (36)
- UML与设计模式 (42)
- c++ (87)
- rust (36)
- Qt (41)
- boost模板元编程 (43)
- Linux (77)
- 汇编 (4)
- 其它 (2)
- 烹饪 (3)
- unix c / socket (73)
- 软件工程 (4)
- shell (53)
- Python (37)
- c++ primer 5th(c++11) (22)
- 数据库/MySQL (27)
- 数据存储 (4)
- lisp (7)
- git (4)
- Utility (3)
- CDN与DNS (54)
- Http (53)
- php (7)
- nginx/lua/openresty (41)
- redis (11)
- TCP/IP (16)
- 互联网 (6)
- kernel (2)
- go (34)
- 区块链 (43)
- 比特股 (13)
- 以太坊 (23)
- 比特币 (23)
- 密码学 (10)
- EOS (53)
- DAG (1)
- docker (1)
- filecoin (7)
- solidity (65)
- ipfs (8)
- 零知识证明 (1)
- openzeppelin (3)
- java (1)
- defi (7)
- Ton (0)
最新评论
msgget函数用于创建一个新的消息队列或访问一个已存在的消息队列
IPC_NOWAIT标志使得msgsend调用非阻塞:如果没有存放新消息的可用空间,该函数就马上返回.这个条件可能发生的情况包括:
1.在指定的队列中已有太多的字节
2.在系统范围存在太多的消息
如果这两个条件中有一个存在,而且IPC_NOWAIT标志已指定,msgsnd就返回一个EAGAIN错误.如果这两个条件中有一个存在,但是IPC_NOWAIT标志未指定,那么调用纯种被投入睡眠.直到:
1.具备存放新消息的空间
2.由msqid标识的消息队列从系统中删除(这种情况下返回一个EIDRM错误)
3.调用线程被某个捕获的信号反中断(这种情况下返回一个EINTR错误)
msgctl函数提供一个消息队列上的各种控制操作
http://dash1982.iteye.com/blog/296583
MsgQueue.h
MsgQueue.cpp
test
IPC_NOWAIT标志使得msgsend调用非阻塞:如果没有存放新消息的可用空间,该函数就马上返回.这个条件可能发生的情况包括:
1.在指定的队列中已有太多的字节
2.在系统范围存在太多的消息
如果这两个条件中有一个存在,而且IPC_NOWAIT标志已指定,msgsnd就返回一个EAGAIN错误.如果这两个条件中有一个存在,但是IPC_NOWAIT标志未指定,那么调用纯种被投入睡眠.直到:
1.具备存放新消息的空间
2.由msqid标识的消息队列从系统中删除(这种情况下返回一个EIDRM错误)
3.调用线程被某个捕获的信号反中断(这种情况下返回一个EINTR错误)
msgctl函数提供一个消息队列上的各种控制操作
http://dash1982.iteye.com/blog/296583
MsgQueue.h
#ifndef MSGQUEUE_H #define MSGQUEUE_H namespace Utility { #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) #define COMMON_MESSAGE 1 struct Message { long msgType; int bufLen; int addr;//地址 int reserved1; int reserved2; }; //初始化消息队列 int initMessageQueue(const char* msgfile,int& msgId,int msgType); //关闭消息队列 int closeMessageQueue(int& msgId); //清空消息 void clearMessage(int key,int& msgId,int msgType); //发送普通消息 int sendMessage(const Message &msg,int msgId); }//end of namespace Utility #endif // MSGQUEUE_H
MsgQueue.cpp
#include "MsgQueue.h" #include <sys/msg.h> #include <string.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <errno.h> #include <iostream> using namespace std; namespace Utility { /* 清空消息 */ void clearMessage(int key,int& msgId,int msgType) { int ret = 0; msgId = msgget(key, 0660); if (msgId<0){ return; } while (1) { if(msgType==COMMON_MESSAGE) { Message msg; bzero(&msg,sizeof(msg)); ret=msgrcv(msgId, &msg, sizeof(msg), 0, IPC_NOWAIT); if(ret<0){ break; } } } } /* 初始化消息队列 */ int initMessageQueue(const char* msgFile,int & msgId,int msgType) { //删除文件,避免引起不必要的问题,不删除可能会造成msgsend时卡死或发送失败的现象 unlink(msgFile); msgId = -1; int fid = open(msgFile, O_RDWR | O_CREAT, FILE_MODE); if(fid<0) { cout << "创建文件:" << msgFile << "失败!" << endl; return fid; } close(fid); int key = ftok(msgFile, 'a'); if(key<0) { cout << "创建消息队列失败!" << endl; return key; } if((msgId = msgget(key, IPC_CREAT | IPC_EXCL | 0660))==-1) { clearMessage(key,msgId,msgType); if (msgId<0) { cout << "创建消息队列失败!" << strerror(errno) << endl; return msgId; } } cout << "初始化消息队列成功.msgFile:" << msgFile << ",msgId:" << msgId << endl; return 0; } /* 关闭消息队列 */ int closeMessageQueue(int& msgId) { int tmpMsgId = msgId; int ret = msgctl(msgId, IPC_RMID, 0); if(ret<0){ cout << "关闭消息队列失败" << endl; }else{ cout << "关闭消息队列成功,msgId:" << tmpMsgId << endl; } return ret; } /* 发送普通信息 */ int sendMessage(const Message &msg,int msgId) { int ret = msgsnd(msgId, &msg, sizeof(msg), IPC_NOWAIT); if(ret<0){ cout << "向消息队列发送消息失败,msgId:" << msgId << endl; }else{ cout << "向消息队列发送消息成功,msgId:" << msgId << ",长度:" << msg.bufLen << endl; } return ret; } }//end of namespace Utility
test
#include "MsgQueue.h" #include <pthread.h> #include <string.h> #include <sys/ipc.h> #include <sys/msg.h> #include <unistd.h> #include <iostream> using namespace std; using namespace Utility; int g_msgId = 0; class A { public: int value; }; void* testRecvMessage(void*) { Message msg;//收数据的结构体 char rcvbuf[100]; bzero(rcvbuf,100); int ret = 0; while(true) { ret = msgrcv(g_msgId, &msg, sizeof(msg), 0, IPC_NOWAIT); if (-1 != ret) { if(msg.msgType==COMMON_MESSAGE) { A** a = (A**)(msg.addr); cout << "bufLen:" << msg.bufLen << ",value:" << (*a)->value << endl; delete *a; *a = NULL; msg.addr = 0; } break; }else { usleep(1000); } } pthread_detach(pthread_self()); return 0; } int main() { pthread_t pid; pthread_create(&pid,NULL,testRecvMessage,NULL); int msgType = COMMON_MESSAGE; initMessageQueue("/tmp/test1.msg",g_msgId,msgType); A* a = new A; a->value = 10; Message message; bzero(&message,sizeof(message)); message.msgType = COMMON_MESSAGE; message.bufLen = sizeof(message); message.addr = (int)&a; sendMessage(message,g_msgId); sleep(5); closeMessageQueue(g_msgId); } 初始化消息队列成功.msgFile:/tmp/test1.msg,msgId:32769 向消息队列发送消息成功,msgId:32769,长度:20 bufLen:20,value:10 关闭消息队列成功,msgId:32769
发表评论
-
滑动窗口与拥塞控制
2017-09-29 14:47 772滑动窗口:用作流量控制: 1.发送端和接收端各维护一个独立的发 ... -
rawsocket发送tcp包
2017-09-25 11:52 997testTcp.h #ifndef TESTTCP_H ... -
柔性数组
2017-09-20 09:53 525#include <iostream> #i ... -
rawsocket发送dns包
2017-09-14 15:18 628#include <stdio.h> #in ... -
rawsocket发送icmp包
2017-09-06 09:54 676#include <stdio.h> #in ... -
查看glibc版本
2016-12-07 14:13 562strings /lib64/libc.so.6 |grep ... -
netfilter
2016-10-04 20:31 506http://blog.chinaunix.net/uid-2 ... -
gdb的简单使用
2016-09-19 15:18 431注意编译的时间加上-g参数 1.调试运行 gdb a.out ... -
TCP各状态的意义
2016-03-01 14:06 440各个状态的意义如下: LISTEN - 侦听来自远方TCP端 ... -
trim
2015-05-14 14:45 383/* trim from start */ ... -
log
2015-03-30 17:42 468log.h #ifndef __LOG_H__ #def ... -
TIMEWAIT与CLOSEWAIT
2015-03-17 11:32 677http://blog.csdn.net/kobejayand ... -
TcpClient
2015-01-16 17:46 484TcpClient.h #ifndef TCPCLIENT ... -
信号量
2014-09-26 10:20 699#include <semaphore.h> ... -
共享内存
2014-09-17 10:39 615SharedMemory.h #ifndef SHARED ... -
记录锁
2014-09-11 16:32 460#include <errno.h> #in ... -
popen
2014-09-06 14:50 695#include <stdio.h> #in ... -
大小端存储
2014-09-05 09:29 627#include <stdio.h> i ... -
条件变量(cond)
2014-09-01 11:02 938#include <pthread.h> # ... -
读写锁
2014-09-01 10:33 753#include <pthread.h> # ...
相关推荐
此文档是C#开发的消息队列系统,适用于消息队列入门与新手。 在Windows 7 上安装消息队列的步骤 打开“控制面板”。 单击“程序”,然后在“程序和功能”下, 单击“打开或关闭 Windows 功能”。 -或者-单击“经典...
WebLogic数据库和消息队列的配置 本文档将详细介绍WebLogic数据库和消息队列的配置方法,主要包括配置数据库连接池和数据源的步骤。 一、配置数据库连接池 在WebLogic中,数据库连接池是通过JDBC(Java Database ...
在IT领域,尤其是在多线程编程中,异步消息队列是一种常见的设计模式,用于实现高效、非阻塞的消息通信。本项目名为"C++ 跨平台 异步消息队列",显然它提供了一个用C++编写的跨平台解决方案,用于在不同线程间安全地...
在IT行业中,消息队列(Message Queue,MQ)是一种常用于分布式系统中解耦组件、提高系统可扩展性和可靠性的技术。在C#编程中,我们可以利用Microsoft Message Queuing(MSMQ)库来实现消息队列的发送和接收。本文将...
标题 "tp5.1消息队列 think-queue" 指的是使用ThinkPHP5.1框架集成的消息队列组件——think-queue。消息队列在软件开发中扮演着重要角色,它允许应用程序异步处理耗时任务,提高系统响应速度和整体性能。think-queue...
### ucOS消息队列使用详解 #### 一、配置ucOS消息队列 为了使用ucOS中的消息队列功能,首先需要对系统进行相应的配置。这些配置主要在`OS_CFG.H`头文件中完成。 ##### 配置项解析 1. **`OS_Q_EN`**: 定义是否启用...