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;
}
相关推荐
乌托邦的逃亡者35 分钟前
Docker的/var/lib/docker/目录占用100%的处理方法
运维·docker·容器
ldj202040 分钟前
Jenkins 流水线配置
运维·jenkins
古希腊数通小白(ip在学)3 小时前
stp拓扑变化分类
运维·服务器·网络·智能路由器
Muxiyale4 小时前
使用spring发送邮件,部署ECS服务器
java·服务器·spring
l1x1n05 小时前
Vim 编辑器常用操作详解(新手快速上手指南)
linux·编辑器·vim
12点一刻5 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务5 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观
FreeBuf_6 小时前
微软365 PDF导出功能存在本地文件包含漏洞,可泄露敏感服务器数据
服务器·microsoft·pdf
lixzest6 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法
ajassi20006 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化