int RingQueueInit(RingQueue_t *queue)
{
if(NULL == queue)return -1;
queue->front = 0;
queue->rear = 0;
memset(queue->buffer,0,sizeof(queue->buffer));
return 0;
}
int IsQueueFull(RingQueue_t *queue)
{
if(NULL == queue)return -1;
if((queue->rear+1)%MAX_QUEUE_LEN == queue->front)
return QUEUE_FULL;
else
return QUEUE_NOT_FULL;
}
int IsQueueEmpty(RingQueue_t *queue)
{
if(NULL == queue)return -1;
if(queue->front == queue->rear)
return QUEUE_EMPTY;
else
return QUEUE_NOT_EMPTY;
}
int EnQueue(RingQueue_t *queue,char ch)
{
if(NULL == queue)return -1;
if(QUEUE_FULL == IsQueueFull(queue))return QUEUE_FULL;//插入前先判断队列是否已满
queue->buffer[queue->rear] = ch;
queue->rear = (queue->rear+1)%MAX_QUEUE_LEN;
return 0;
}
int PopQueue(RingQueue_t *queue,char *ch)
{
if(NULL == queue)return -1;
if(QUEUE_EMPTY == IsQueueEmpty(queue))return QUEUE_EMPTY;
*ch = queue->buffer[queue->front];
queue->front = (queue->front+1)%MAX_QUEUE_LEN;
return 0;
}
int QueueLength(RingQueue_t *queue)
{
if(queue->front == queue->rear)
return 0;
else
return (queue->rear - queue->front);
}
int QueueFreeSpace(RingQueue_t *queue)
{
if((queue->rear+1)%MAX_QUEUE_LEN == queue->front)
return 0;
else
return (MAX_QUEUE_LEN - (queue->rear - queue->front) -1);
}
RingQueue.h
#ifndef _RINGQUEUE_H
#define _RINGQUEUE_H
#define MAX_QUEUE_LEN 1024
typedef enum{
QUEUE_FULL = -1,
QUEUE_EMPTY = -2,
QUEUE_NOT_FULL = 1,
QUEUE_NOT_EMPTY = 2,
};
typedef struct _queue{
char buffer[MAX_QUEUE_LEN];
int front;
int rear;
}RingQueue_t;
int RingQueueInit(RingQueue_t *queue);
int IsQueueFull(RingQueue_t *queue);
int IsQueueEmpty(RingQueue_t *queue);
int QueueLength(RingQueue_t *queue);
int EnQueue(RingQueue_t *queue,char ch);
int PopQueue(RingQueue_t *queue,char *ch);
#endif
RingQueue.c
/*
* @Copyright (c) 2020 zBlackShadow
*/
/*
* @Copyright (c) 2020 zBlackShadow
*/
#include
#include
#include "RingQueue.h"
int RingQueueInit(RingQueue_t *queue)
{
if(NULL == queue)return -1;
queue->front = 0;
queue->rear = 0;
memset(queue->buffer,0,sizeof(queue->buffer));
return 0;
}
int IsQueueFull(RingQueue_t *queue)
{
if(NULL == queue)return -1;
if((queue->rear+1)%MAX_QUEUE_LEN == queue->front)
return QUEUE_FULL;
else
return QUEUE_NOT_FULL;
}
int IsQueueEmpty(RingQueue_t *queue)
{
if(NULL == queue)return -1;
if(queue->front == queue->rear)
return QUEUE_EMPTY;
else
return QUEUE_NOT_EMPTY;
}
//获取对列有效数据的长度
int QueueLength(RingQueue_t *queue)
{
if(queue->front == queue->rear)
return 0;
else
return (queue->rear - queue->front);
}
//获取对列的剩余空间
int QueueFreeSpace(RingQueue_t *queue)
{
if((queue->rear+1)%MAX_QUEUE_LEN == queue->front)
return 0;
else
return (MAX_QUEUE_LEN - (queue->rear - queue->front) -1);
}
int EnQueue(RingQueue_t *queue,char ch)
{
if(NULL == queue)return -1;
if(QUEUE_FULL == IsQueueFull(queue))return QUEUE_FULL;
queue->buffer[queue->rear] = ch;
queue->rear = (queue->rear+1)%MAX_QUEUE_LEN;
return 0;
}
int PopQueue(RingQueue_t *queue,char *ch)
{
if(NULL == queue)return -1;
if(QUEUE_EMPTY == IsQueueEmpty(queue))return QUEUE_EMPTY;
*ch = queue->buffer[queue->front];
queue->front = (queue->front+1)%MAX_QUEUE_LEN;
return 0;
}
测试代码:
#include
#include
#include
#include
#include
#include
#include "RingQueue.h"
typedef struct{
int flag;
RingQueue_t ring_queue;
}mem_data_t;
/*将对列放在共享内存中,根据传入的argv参数判断启动入队或者出队的进程
*/
int main(int arcv,char *argv[])
{
char path[]=".";
int proj_id = 's';
key_t key;
int shmid = -1;
int pid = -1;
int ret = -1;
int count = 0;
mem_data_t *data;
char buf[1024];
//创建共享内存
key = ftok(path,proj_id);
if(key < 0)
{
perror("key create error\r\n");
return -1;
}
shmid = shmget(key,1,0666|IPC_CREAT);
if(shmid < 0)
{
perror("sem get error\r\n");
return -1;
}
data = shmat(shmid,NULL,0);
if(NULL == data)
{
perror("shmat error\r\n");
return -1;
}
memset(data,0,sizeof(mem_data_t));
RingQueueInit(&data->ring_queue);
if(0 == strncmp(argv[1],"read",4))//出队
{
char ch;
int len = 0;
while(1)
{
if(0 == QueueLength(&data->ring_queue))continue;
printf("queue len:%d\r\n",QueueLength(&data->ring_queue));
while(QUEUE_EMPTY != PopQueue(&data->ring_queue,&ch))
{
printf("%c",ch);
}
sleep(1);
}
}
else
{
char ch;
while(1)//入队
{
printf("write something:\r\n");
fgets(buf,1024,stdin);
for(int k=0;kring_queue,buf[k]);
}
printf("free space:%d\r\n",QueueFreeSpace(&data->ring_queue));
}
}
return 0;
}