linux多线程读写环缓存,linux下多线程循环队列读写

linux下多线程循环队列读写,一条线程从一个文件打开,将读取的数据放进循环队列,另一条线程再从循环队列的数据写入另一个文件。所要注意的地方是要用linux超级用户比较好,不要放在windos的共享目录中,也就是说不要放在linux下/mnt/hgfs/.../,其中/.../是自定义文件夹。

1.[代码][C/C++]代码

#include

#include

#include

#include

#include

#include

#include

#define FILESIZE 8*1024

#define FILESUM 16

#define MAXSIZE 1024*8

typedef char ElemType;

typedef struct Queue

{

char *base;

int wp;//дָÕë

int rp;//¶ÁÖ¸Õë

int queueCnt;//±ê¼Ç¶ÓÁÐÊÇ·ñÂú»ò¿Õ

} CommQueue;

//¶ÓÁÐ״̬£ºÂú¡¢¿Õ¡¢ÔËÐÐÕýÈ·¡¢ÔËÐдíÎó

typedef enum

{

QueueFull = 0,

QueueEmpty,

QueueOK,

QueueFail

} QueueStatus;

static int isFull(CommQueue *queue);

static int isEmpty(CommQueue *queue);

//³õʼ»¯¶ÓÁÐ

QueueStatus init_queue(CommQueue* queue);

//Èë¶ÓÁÐ

QueueStatus inQueue(ElemType val, CommQueue* queue);

//³ö¶ÓÁÐ

QueueStatus outQueue(CommQueue* queue, ElemType *val);

//¶ÁÈ¡»º³åÇøÊý¾Ý

int readQueue(ElemType *buf, int size, CommQueue *queue);

//Íù»º³åÇøдÈëÊý¾Ý

int writeQueue(const ElemType *buf, int size, CommQueue *queue);

QueueStatus init_queue(CommQueue* queue)

{

queue->base =(char *)calloc(sizeof(char) * MAXSIZE, 1);

//·ÖÅäʧ°Ü

if (queue->base == NULL)

{

perror("malloc()");

return QueueFail;

}

//³õʼ»¯¶ÁдָÕë

queue->wp = queue->rp = 0;

//³õʼ»¯¶ÓÁÐÖÐÔªËظöÊý

queue->queueCnt = 0; // conuter

}

int isFull(CommQueue *queue)

{

return (queue->queueCnt == MAXSIZE - 1) ? 1 : 0;

}

int isEmpty(CommQueue *queue)

{

return (0==queue->queueCnt)? 1 : 0;

}

QueueStatus inQueue(ElemType val, CommQueue *queue)

{

if (1==isFull(queue))

{

//printf("queue is full .\n");

return QueueFull;

}

else

{

memcpy((queue->base + queue->wp),&val, sizeof(ElemType));

//»Øת

if (++(queue->wp) == MAXSIZE)

{

queue->wp = 0;

}

//ÔªËظöÊý¼Ó1

(queue->queueCnt)++;

}

return QueueOK;

}

QueueStatus outQueue(CommQueue *queue, ElemType *val)

{

if (1==isEmpty(queue))

{

//printf("queue is empty .\n");

return QueueEmpty;

}

else {

int index = queue->rp;

if (++(queue->rp) == MAXSIZE)

{

queue->rp = 0;

}

(queue->queueCnt)--;

memcpy(val ,(queue->base + index), sizeof(ElemType));

}

return QueueOK;

}

int readQueue(ElemType *buf, int len, CommQueue * queue)

{

int i ;

ElemType val;

for (i=0; i < len; i++)

{

QueueStatus ret = outQueue(queue, &val);

if (ret != QueueEmpty)

{

memcpy(buf+i, &val, sizeof(ElemType));

} else

{

break;

}

}

return i;

}

int writeQueue(const ElemType *buf, int len, CommQueue *queue)

{

int i ;

for (i=0; i < len; i++)

{

QueueStatus ret = inQueue(buf[i], queue);

if (ret == QueueFull)

{

break;

}

}

}

typedef struct read_write_0

{

char ID;

char fdname_buffer_0[8*1024];

int real_read_0;

int real_write_0;

}BUFFER_0;

BUFFER_0 buffer_0;

void *readDataFromFile(CommQueue *queue)

{

int read_fd;

int ret ;

char read_buf[8*1024] = {0};

read_fd = open("/home/lzm/lzm/1.c", O_RDWR|O_CREAT);

if(-1 == read_fd)

{

printf("open file err.\n");

close(read_fd);

return;

}

ret = read(read_fd,read_buf,sizeof(read_buf));

if(-1 == ret)

{

printf("read error.\n");

close(read_fd);

return ;

}

//printf("in %d\n",ret);

//printf("read buff %s\n",read_buf);

writeQueue(read_buf,ret,queue);

close(read_fd);

return NULL;

}

void *writeDataToFile(CommQueue *queue)

{

sleep(1);

int write_fd;

write_fd = open("/home/lzm/lzm/2.c", O_RDWR|O_CREAT);

if(-1 == write_fd)

{

printf("open file err.\n");

close(write_fd);

return;

}

char write_buf[8*1024] = {0};

readQueue(write_buf,8*1024,queue);

write(write_fd,write_buf,strlen(write_buf));

if(-1 == write_fd)

{

printf("write file err.\n");

close(write_fd);

return;

}

return NULL;

}

int main(int argc , char **argv)

{

int ret;

CommQueue queue;

init_queue(&queue);

pthread_t id1,id2;

ret = pthread_create(&id1, NULL, (void *)writeDataToFile,&queue);

ret = pthread_create(&id2, NULL, (void *)readDataFromFile,&queue);

pthread_join(id1, NULL);

pthread_join(id2, NULL);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值