Linux实现循环队列与共享内存的循环队列

cpp 复制代码
//#ifndef __PUBLIC_HH
#define __PUBLIC_HH 1

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/sem.h>

using namespace std;

template <class TT, int MaxLength>

class squeue
{
    private:
        bool m_inited;
        TT m_data[MaxLength];
        int m_head;
        int m_tail;
        int m_length;
        squeue(const squeue &) = delete;
        squeue &operator = (const squeue &) = delete;
    public:
        squeue()
        {
            init();
        }
        
        void init()
        {
            if(m_inited != true)
            {
                m_head = 0;
                m_tail = MaxLength - 1;
                m_length = 0;
                memset(m_data, 0, sizeof(m_data));
                m_inited = true;
            }
        }
        
        bool push(const TT &ee)
        {
            if(full() == true)
            {
                cout << "循环队列已满,入队失败。 \n";
                return false;
            }
            m_tail = (m_tail + 1) % MaxLength;
            m_data[m_tail] = ee;
            m_length ++;
            return true;
        }
        
        int size()
        {
            return m_length;
        }
        
        bool empty()
        {
            return m_length == 0;
        }
        
        bool full()
        {
            return m_length == MaxLength;
        }
        
        TT& front()
        {
            return m_data[m_head];
        }
        
        TT& back()
        {
            return m_data[m_tail];
        }
        
        bool pop()
        {
            if(empty() == true)
                return false;
            m_head = (m_head + 1) % MaxLength;
            m_length --;
            return true;
        }
        
        void printqueue()
        {
            for(int ii = 0; ii < size(); ii ++ )
                cout << "m_data[" << (m_head + ii) % MaxLength << "],value = " << m_data[(m_head + ii) % MaxLength] << endl;
        }
};
cpp 复制代码
// 演示循环队列
#include "_public.h"

int main ()
{
    using ElemType = int;
    squeue<ElemType, 5> QQ;
    ElemType ee;
    
    cout << "元素(1, 2, 3) 入队。\n";
    ee = 1; QQ.push(ee);
    ee = 2; QQ.push(ee);
    ee = 3; QQ.push(ee);
    
    cout << "队列的长度是" << QQ.size() << endl;
    
    QQ.printqueue();
    
    ee = QQ.front(); QQ.pop(); cout << "出队的元素值为" << ee << endl;
    ee = QQ.front(); QQ.pop(); cout << "出队的元素值为" << ee << endl;
    
    cout << "队列的长度是" << QQ.size() << endl;
    
    QQ.printqueue();
    
    cout << "元素(11, 12, 13, 14, 15) 入队。\n";
    ee = 11; QQ.push(ee);
    ee = 12; QQ.push(ee);
    ee = 13; QQ.push(ee);
    ee = 14; QQ.push(ee);
    ee = 15; QQ.push(ee);
    
    cout << "队列的长度是" << QQ.size() << endl;
    
    QQ.printqueue();
    
    return 0;
}
cpp 复制代码
// 演示基于共享内存的循环队列
#include "_public.h"

int main ()
{
    using ElemType = int;
    
    int shmid = shmget(0x5005, sizeof(squeue<ElemType, 5>), 0640 | IPC_CREAT); // 初始化共享内存
    
    if(shmid == -1)
    {
        cout << "shmget(0x5005) failed. \n";
        return -1;
    }
    // 定义模板类的指针QQ,指向共享内存的首地址
    squeue<ElemType, 5> *QQ = (squeue<ElemType, 5> *) shmat(shmid, 0, 0); // 把共享内存连接到当前进程的地址空间
    
    if(QQ == (void *)-1)
    {
        cout << "shmat() failed. \n";
        return -1;
    }
    
    QQ->init(); // 初始化循环队列。
    
    ElemType ee; // 创建一个数据元素
    
    cout << "元素(1, 2, 3) 入队。\n";
    ee = 1; QQ->push(ee); // 在上个例子中,QQ是对象,这里QQ是指针。
    ee = 2; QQ->push(ee);
    ee = 3; QQ->push(ee);
    
    cout << "队列的长度是" << QQ->size() << endl;
    
    QQ->printqueue();
    
    ee = QQ->front(); QQ->pop(); cout << "出队的元素值为" << ee << endl;
    ee = QQ->front(); QQ->pop(); cout << "出队的元素值为" << ee << endl;
    
    cout << "队列的长度是" << QQ->size() << endl;
    
    QQ->printqueue();
    
    cout << "元素(11, 12, 13, 14, 15) 入队。\n";
    ee = 11; QQ->push(ee);
    ee = 12; QQ->push(ee);
    ee = 13; QQ->push(ee);
    ee = 14; QQ->push(ee);
    ee = 15; QQ->push(ee);
    
    cout << "队列的长度是" << QQ->size() << endl;
    
    QQ->printqueue();
    
    shmdt(QQ); // 把共享内存从当前进程分离

    return 0;
}
相关推荐
让我们一起加油好吗11 分钟前
【C++】封装红黑树模拟实现 set 和 map
linux·c++·set·map·红黑树
暴富奥利奥26 分钟前
完成docker方式的ros环境配置
linux·学习·docker·容器
秃头菜狗28 分钟前
十四、运行经典案例 wordcount
大数据·linux·hadoop
ManageEngineITSM39 分钟前
IT 服务自动化的时代:让效率与体验共进
运维·数据库·人工智能·自动化·itsm·工单系统
Bug退退退1231 小时前
Java 网络流式编程
java·服务器·spring·sse
QotomPC1 小时前
软件定义的理想硬件平台:Qotom Q30900SE/UE系列在AIO服务器与边缘网关中的实践
运维·服务器
望获linux1 小时前
【实时Linux实战系列】实时系统的可观测性:Prometheus 与 Grafana 集成
大数据·linux·服务器·开发语言·网络·操作系统
捷智算云服务1 小时前
H200服务器维修服务体系构建:捷智算的全链条保障方案
运维·服务器
hweiyu001 小时前
Linux 命令:mount
linux·运维·服务器
zhmy_0061 小时前
linux 多服务器下目录数据文件实时同步
linux·文件实时同步