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;
}
相关推荐
搬码临时工5 小时前
电脑同时连接内网和外网的方法,附外网连接局域网的操作设置
运维·服务器·网络
藥瓿亭5 小时前
K8S认证|CKS题库+答案| 3. 默认网络策略
运维·ubuntu·docker·云原生·容器·kubernetes·cks
Gaoithe5 小时前
ubuntu 端口复用
linux·运维·ubuntu
德先生&赛先生6 小时前
Linux编程:1、文件编程
linux
程序猿小D6 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
gsls2008087 小时前
ocrapi服务docker镜像使用
运维·docker·容器
多多*7 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap
文牧之7 小时前
PostgreSQL 的扩展pg_freespacemap
运维·数据库·postgresql
AWS官方合作商8 小时前
基于AWS Serverless架构:零运维构建自动化SEO内容生成系统
运维·serverless·aws
whp4048 小时前
windows server2019 不成功的部署docker经历
运维·docker·容器