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;
}
相关推荐
小白学大数据7 分钟前
未来趋势:AI 时代下 python 爬虫技术的发展方向
运维·人工智能·爬虫·python·自动化
goodlook01238 分钟前
监控平台搭建-日志-loki篇-最新版3.6.3(七)
服务器·grafana·prometheus
DD-WL10 分钟前
PKI&&数字证书相关概念
linux·网络协议
路溪非溪18 分钟前
Linux驱动中的红外遥控子系统
linux·arm开发·驱动开发
成都犀牛24 分钟前
Ubuntu配置nginx
linux·nginx·ubuntu
阿巴~阿巴~34 分钟前
从滑动窗口到拥塞控制:TCP高效可靠传输的三大支柱
服务器·网络·网络协议·tcp·滑动窗口·流量控制·拥塞控制
qdprobot1 小时前
开源的在线串口调试助手支持macOS苹果电脑Windows系统Linux 浏览器webSerial
linux·运维·服务器·人工智能·mixly·小智ai·webserial
wdfk_prog1 小时前
[Linux]学习笔记系列 -- [fs]namespace
linux·笔记·学习
无限大.1 小时前
为什么“DevOps“能提高软件开发效率?——从开发到运维的融合
linux·运维·devops
饭九钦vlog1 小时前
dns形式的floodddos命令
linux·运维·服务器