github源码指引:共享内存、数据结构与算法:作为基础的数组

初级代码游戏的专栏介绍与文章目录-CSDN博客

我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。

这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。


相关专题:共享内存、数据结构与算法_初级代码游戏的博客-CSDN博客

源码位置:shmfc

基础:github源码指引:源码结构、编译、运行_github编译-CSDN博客

目录

一、运行示例

二、示例代码


一、运行示例

shmfc目录下执行run.sh,显示命令表。系统重启后初次运行需要先执行命令0-1创建主共享内存(执行完毕输入b返回上一级命令表)。

命令20执行本示例代码。

复制代码
[08-27 08:44:24][应用][信息][shmfc_t.cpp             : 851(main)][  0.00]
----------------------------------------
命令表:(q=exit)
0 管理
1 test_BinaryPool
3 test_CMultiProcessServer
4 test_CMultiProcessServer_view
5 test_CMultiProcessServer_speed
6 test_CMultiProcessServer_direct
7 test_RebuildSet
12 UDP测试客户端
13 UDP测试服务端
14 CStressTesting
15 CTestCSimpleMultiProcess_mutex atomic
16 CTest_hash
17 T_SHM_HASH
20 T_ARRAY
88 test_CMyRWMutex
89 test_T_SHM_SET_GROUP
90 test_shm_IActiveObject
91 test_ShmMultiMap
99 test_ParseFromXml
........................................
----------------------------------------
请选择命令:(q=exit default=20):

[08-27 08:44:30][应用][信息] 用户输入的是:20

运行输出:

复制代码
[08-27 08:44:30][应用][信息][shmArray.h              : 960(AttachToShm)][  0.00]test 连接共享内存成功 PI_N 1 PART 0 shmid = 32769 p 0x7ff9eaabc000
[08-27 08:44:30][应用][信息][shmEnv.cpp              : 624(GetShmConfig)][  0.00]未配置 SHM_CONFIG default.test
[08-27 08:44:30][应用][出错][shmEnv.h                : 304(GetRegFromDb)][  0.00]共享内存 default test 0 不存在,可能的原因:主机重启或手工删除
[08-27 08:44:30][应用][信息][shmArray.h              :1024(_DestoryShm)][  0.00]test 0 连接到共享内存失败
[08-27 08:44:30][应用][信息][shmEnv.cpp              : 624(GetShmConfig)][  0.00]未配置 SHM_CONFIG default.test
[08-27 08:44:30][应用][信息][shmArray.h              :1074(_CreateShm)][  0.00]default test 0 未配置的最大记录数
[08-27 08:44:30][应用][信息][shmArray.h              :1076(_CreateShm)][  0.00]使用默认值 1024
[08-27 08:44:30][应用][信息][shmArray.h              : 395(_CreateShmIfNeed)][  0.00]创建新共享内存成功,id = 65537
[08-27 08:44:30][应用][信息][shmArray.h              :1129(_CreateShm)][  0.00]基础共享内存创建完成
[08-27 08:44:30][应用][信息][shmArray.h              : 960(AttachToShm)][  0.00]test 连接共享内存成功 PI_N 1 PART 0 shmid = 65537 p 0x7ff9eaabc000
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]0
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]1
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]2
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]3
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]4
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]5
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]6
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]7
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]8
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]9
。。。。。。
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]93
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]94
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]95
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]96
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]97
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]98
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]99
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第0个数据:10000 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第1个数据:10001 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第2个数据:10002 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第3个数据:10003 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第4个数据:10004 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第5个数据:10005 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第6个数据:10006 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第7个数据:10007 abc
。。。。。。
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第97个数据:10097 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第98个数据:10098 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第99个数据:10099 abc
[08-27 08:44:30][应用][信息][shmIActiveObject.h      : 457(RunCmdUI)][  0.01]
test

b:返回上一层
1:创建 2:连接(只读) 3:连接(可写) 4:断开 5:禁用互斥 6:清除数据 7:创建私有 8:删除共享内存 9:显示 10:数据
11:从数据库加载 12:保存到数据库
21:从目录加载 22:保存到目录 23:导出为文本文件
31:从目录加载到私有内存 32:销毁私有内存
97:repair 98:check 99:ToDo 100:shell(q=exit ):

测试最后进入本块共享内存的管理界面,直接回车显示基本信息:

复制代码
[08-27 08:52:37][应用][信息] 用户输入的是:
[08-27 08:52:37][应用][信息][shmIActiveObject.h      : 467(RunCmdUI)][  0.01]
test
SHM NAME PI CSW TOTAL BLOCK R_LENGTH CAPACITY CAPACITY SIZE SIZE    % OPERATION DUAL RESULT OPERATION DUAL RESULT
-------- -- --- ----- ----- -------- -------- -------- ---- ---- ---- --------- ---- ------ --------- ---- ------
test      1 CSW 18.7K     1       16     1024     1.0K  100 100  9.0%
-------- -- --- ----- ----- -------- -------- -------- ---- ---- ---- --------- ---- ------ --------- ---- ------

test是共享内存的名字,代码里设定的,全系统唯一。

PI是内部使用的索引,进程内唯一。

CSW,C表示已连接,S表示是共享内存(而不是私有内存),W表示可写。

BLOCK表示分块数,超出容量会自动增加一块连接在一起(虚拟)。

TOTAL是总字节数,R_LENGTH是记录字节数,容量和大小都是记录数(各两个,精确值和简明值)。

二、示例代码

示例代码:

cpp 复制代码
//
// Copyright (c) ct  All rights reserved.
// 版权所有 ct 保留所有权利
//

#pragma once


#include <sstream>
#include "../env/env.h"
#include "shmArray.h"

class CTestT_ARRAY
{
public:
	struct DemoData : public CActiveObjectBase
	{
		long n = 0;
		sstring<8> s;

		//用于需要排序的场合
		bool operator < (CDemoData const& tmp)const { return n < tmp.n; }
		//某些场合也需要等于
		bool operator == (CDemoData const& tmp)const { return n == tmp.n; }

		friend ostream& operator << (ostream& o, DemoData const& d)
		{
			return o << d.n << " " << d.s.c_str();
		}
		//关键字的hash值,用于分块场合,应保证hash值的最后一部分仍然是平均分布的
		long keyhash()const { return n; }

		//用于输出数据的场合
		string& toString(string& str)const
		{
			char buf[2048];
			sprintf(buf, "%ld %s", n, s.c_str());
			return str = buf;
		}
		//用于表格输出
		static bool AddTableColumns(CHtmlDoc::CHtmlTable2& table)
		{
			table.AddCol("N", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			table.AddCol("S", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			return true;
		}
		bool AddTableData(CHtmlDoc::CHtmlTable2& table)const
		{
			table.AddData(n);
			table.AddData(s.c_str());
			return true;
		}
	};

	static int test_T_ARRAY(int argc, char** argv)
	{
		T_ARRAY<DemoData, PI_TEST_1, CDemoData > a("test", 1);
		a.DestoryShm();
		if (!a.CreateShm())return __LINE__;
		if (!a.Attach(false))return __LINE__;
		for (int i = 0; i < 100; ++i)
		{
			DemoData tmp;
			tmp.n = 10000 + i;
			tmp.s = "abc";
			T_ARRAY<DemoData, PI_TEST_1, CDemoData >::HANDLE h;
			a.Add(tmp, h);
			thelog << h.handle << endi;
		}
		for (int i = 0; i < a.Size(); ++i)
		{
			string str;
			thelog << "第" << i << "个数据:" << a.Get(i)->toString(str) << endi;
		}
		a.RunCmdUI();

		return 0;
	}
};

代码不复杂,先定义了记录结构,然后声明了一个共享内存类型。


(这里是文档结束)

相关推荐
mortimer1 小时前
安装NVIDIA Parakeet时,我遇到的两个Pip“小插曲”
python·github
爱装代码的小瓶子2 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
爱喝矿泉水的猛男2 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao2 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先
YouQian7723 小时前
Traffic Lights set的使用
算法
心之语歌4 小时前
Spring AI MCP 客户端
人工智能·spring·github
go54631584654 小时前
基于深度学习的食管癌右喉返神经旁淋巴结预测系统研究
图像处理·人工智能·深度学习·神经网络·算法
aramae5 小时前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
大锦终5 小时前
【算法】前缀和经典例题
算法·leetcode
想变成树袋熊5 小时前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理