数组模拟循环队列

/*
 * =====================================================================================
 *
 *       Filename:  file_map.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012年07月19日 10时08分07秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <string>
#include <iostream>
using namespace std;
const char text[]="hello,world !";
#define MAX_MSG_TOTAL 5

typedef struct MSG_INFO{
int   msg_total;
char msg_info[MAX_MSG_TOTAL][128];
}_MSG_INFO_T;

typedef struct MSG_QUEUE
{
_MSG_INFO_T log;
int front;//log queue front
int rear;//log queue rear
}_MSG_QUEUE_T;

void init_log_gueue(_MSG_QUEUE_T *plog);
bool is_msg_queue_full(_MSG_QUEUE_T *plog);
bool delete_log_from_queue(_MSG_QUEUE_T *plog);
bool add_log_to_msg_queue(_MSG_QUEUE_T *plog,char *string);
void print_log_queue(_MSG_QUEUE_T *plog);

int main(void){
int fd;
struct stat buf;
int rc;
void *fdstart;
int select;
char msg_log[128];
_MSG_QUEUE_T log_info;
init_log_gueue(&log_info);

fd=open("./log.bin",O_CREAT|O_RDWR,777);
if(fd<0){
printf("open log.bin fail\n");
}
if(truncate("./log.bin",sizeof(_MSG_QUEUE_T)) < 0){
cout<< "truncate error !"<<endl;
}
rc=fstat(fd,&buf);
if(rc == -1){
printf("File Size %d \n",buf.st_size);
}
cout << "buf.st_size %d "<<buf.st_size<<endl;
if((fdstart=mmap(NULL,buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==MAP_FAILED){
cout<<"mmap error!"<<endl;
close(fd);
exit(0);
}
cout <<"text size %d "<< sizeof(text)<<endl;
while(1){
cout<<"Please Input your chocie,and Ether!"<<endl;
scanf("%d",&select);
switch(select){
case 1:
cin>>msg_log;
add_log_to_msg_queue(&log_info,msg_log);
break;
case 2:
print_log_queue(&log_info);
break;
default:break;
}
//cin>>msg_log;
//add_log_to_msg_queue(&log_info);

}
memcpy(fdstart,(void *)text,sizeof(text));
munmap(fdstart,buf.st_size);
close(fd);
return EXIT_SUCCESS;
}



void init_log_gueue(_MSG_QUEUE_T *plog){
int i;
plog->front=0;
plog->rear=0;
plog->log.msg_total=0;
for(i=0;i<MAX_MSG_TOTAL;i++){
bzero(&plog->log.msg_info[i][0],sizeof(plog->log.msg_info[i]));
}
}

bool is_msg_queue_full(_MSG_QUEUE_T *plog){
if(plog->log.msg_total==MAX_MSG_TOTAL)
return true;
else
return false;
}

bool delete_log_from_queue(_MSG_QUEUE_T *plog){
if(plog->log.msg_total==0){
//no meseage for delete
return false;
}
if(plog->front < (MAX_MSG_TOTAL-1)){
bzero(&plog->log.msg_info[plog->front][0],sizeof(plog->log.msg_info[plog->front]));
plog->front++;
}else{
//the front is pointer the queue rear,so goto front array
bzero(&plog->log.msg_info[plog->front][0],sizeof(plog->log.msg_info[plog->front]));
plog->front=0;
}
plog->log.msg_total--;
return true;

}


bool add_log_to_msg_queue(_MSG_QUEUE_T *plog,char *logstring){

if(plog->log.msg_total==MAX_MSG_TOTAL){
//check queue is full ?
delete_log_from_queue(plog);
}

printf("add info::%s\n",logstring);
strcpy((char*)&plog->log.msg_info[plog->rear][0],logstring);
plog->rear++;
plog->log.msg_total++;
printf("front %d rear %d msg_total %d \n",plog->front,plog->rear,plog->log.msg_total);
if(plog->rear == MAX_MSG_TOTAL){//end the array
plog->rear=0;//goto back frist array eletment
}
}

void print_log_queue(_MSG_QUEUE_T *plog){
int i;
int front;
front=plog->front;

for(i=0;i<plog->log.msg_total;i++){
if(front < MAX_MSG_TOTAL){
cout<<"log "<<i<<"::"<<plog->log.msg_info[front]<<endl;
front++;
}else{
front=0;
cout<<"log "<<i<<"::"<<plog->log.msg_info[front]<<endl;
front++;
}
}
}




<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(265) | 评论(2) | 转发(2) |
给主人留下些什么吧!~~

lubing5212012-08-02 14:11:28

内存映射和文件操作的应用和标题不冲突,只是在工程用到的时候采用。

3089389692012-07-25 16:15:00

晕,,,不知道你的程序中间的那个文件操作和内存映射是做什么用的,貌似你的数组队列也没有用到啊。

评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值