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;
}
相关推荐
花果山~~程序猿1 小时前
高级I/O知识分享【5种IO模型 || select || poll】
运维·服务器·网络
Pakho love1 小时前
Linux:软件包管理器 yum和编辑器-vim使用
linux·编辑器·vim
吴半杯1 小时前
Linux-mysql5.7-mysql8.0安装包下载及安装教程,二合一
linux·运维·服务器
默行默致1 小时前
Linux 常用命令
linux·运维
魏 无羡1 小时前
pgsql 分组查询方法
java·服务器·数据库
码哝小鱼2 小时前
firewalld实现NAT端口转发
linux·网络
江凡心2 小时前
Qt 每日面试题 -1
服务器·数据库·qt
RememberLey2 小时前
【VitualBox】VitualBox的网络模式+网络配置
linux·网络·virtualbox
卡戎-caryon2 小时前
【Linux】09.Linux 下的调试器——gdb/cgdb
linux·运维·服务器·开发语言·笔记