Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1742232
  • 博文数量: 782
  • 博客积分: 2455
  • 博客等级: 大尉
  • 技术积分: 4140
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-06 21:37
个人简介

Linux ,c/c++, web,前端,php,js

文章分类

全部博文(782)

文章存档

2015年(8)

2014年(28)

2013年(110)

2012年(307)

2011年(329)

分类:

2012-07-25 09:45:46

原文地址:数组模拟循环队列 作者:lubing521

/*
 * =====================================================================================
 *
 *       Filename:  file_map.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2012年07月19日 10时08分07秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *        Company:  
 *
 * =====================================================================================
 */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 !"<
}
rc=fstat(fd,&buf);
if(rc == -1){
printf("File Size %d \n",buf.st_size);
}
cout << "buf.st_size %d "<
if((fdstart=mmap(NULL,buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==MAP_FAILED){
cout<<"mmap error!"<
close(fd);
exit(0);
}
cout <<"text size %d "<< sizeof(text)<
while(1){
cout<<"Please Input your chocie,and Ether!"<
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
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;ilog.msg_total;i++){
if(front < MAX_MSG_TOTAL){
cout<<"log "<log.msg_info[front]<
front++;
}else{
front=0;
cout<<"log "<log.msg_info[front]<
front++;
}
}
}




阅读(894) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

3089389692012-07-25 16:03:42

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